Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# VB.NET
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 24-Aug-12 18:54pm
Comments
Sergey Alexandrovich Kryukov at 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

Rate this: bad
good
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:
 
http://stackoverflow.com/questions/661561/how-to-update-gui-from-another-thread-in-c[^]
  Permalink  

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

  Print Answers RSS
0 DamithSL 265
1 CPallini 235
2 OriginalGriff 233
3 Maciej Los 195
4 George Jonsson 170
0 OriginalGriff 5,305
1 DamithSL 4,382
2 Maciej Los 3,760
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,901


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 25 Aug 2012
Copyright © CodeProject, 1999-2014
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