Click here to Skip to main content
16,020,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all, i have some problem with setting text value in Textbox.
My .net project includes a class and one windows form.

i allocated textbox(Textbox1) and combobox(ComboBox1) inside the form.
Here is the code inside the form

Public Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.Text = "APPLE" Then
            MsgBox("APL")
        ElseIf ComboBox1.Text = "ORANGE" Then
            MsgBox("ORA")
        ElseIf ComboBox1.Text = "STRAWBERRY" Then
            MsgBox("STRA")
        ElseIf ComboBox1.Text = "CELERAL" Then
            MsgBox("CEL")
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim cls As New Class1()
    End Sub

The Following show the code of class.

Public Class Class1

	Dim thrd As New Thread(AddressOf StartProcess)

	Public Sub New()	
	       thrd.Start()
	End Sub

 	Public Sub StartProcess()        	
        	Form1.ComboBox1.SelectedIndex = 2
        	Form1.TextBox1.Text = "123"
    	End Sub

End Class


So triggering combobox1 is working when i put selectedIndex is 2 (i can see message box) but Textbox1 is not working means i cannot set text value although i use Form1.TextBox1.Text = "123".

How can i set TextBox1.Text value using class

pls enlighten me.

Cheers
df
Posted
Comments
Sergey Alexandrovich Kryukov 25-Aug-12 0:06am    
There is so many absurdities in this code, that it's pretty hard to help. More importantly, the code is incomplete, so it's hard to advise anything without knowing what you want to achieve. For example, how the variable Form1 is passed to the instance of Class1 and thrd start method? Does your code even compile? You cannot call any methods or properties of UI in any thread other then the thread of that UI. And so on... You need to start with something more simple...
--SA

1 solution

I think I see your problem. First, you need to remember that Form1 is the class name of your form and does not refer to the specific instance that is running. You would need to pass a reference to the current instance of Form1 into the Class1 instance in order for that instance to have access to the UI elements on the form. Second, you cannot access the UI elements from another thread directly.

To solve these issues, I would first ask if this is really necessary. I'm assuming this is sample code for proof of concept. If not, lose the thread and lose the class instance as well. At least don't expect to access the UI from the class instance. Instead, have the class encapsulate the work it needs to do and then have it either return the value you need from a method or have it update properties in the class that you can then access later in your methods behind Form1.

If you must do threading and you must access Form1 from the instance of Class1, you will need to pass your class instance into the Class1 constructor. Also, you will need to modify your threading call using code like what is found in this link:

http://stackoverflow.com/questions/661561/how-to-update-gui-from-another-thread-in-c[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900