Click here to Skip to main content
13,201,451 members (72,514 online)
Rate this:
Please Sign up or sign in to 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
        ElseIf ComboBox1.Text = "ORANGE" Then
        ElseIf ComboBox1.Text = "STRAWBERRY" Then
        ElseIf ComboBox1.Text = "CELERAL" Then
        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()	
	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.

Posted 24-Aug-12 17:54pm
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...

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:[^]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171020.1 | Last Updated 25 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100