无法从类的窗体中将值设置为文本框 [英] Cannot set value to Textbox in Form from class

查看:63
本文介绍了无法从类的窗体中将值设置为文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在文本框"中设置文本值存在一些问题.
我的.net项目包括一个类和一个Windows窗体.

我在表单内分配了textbox(Textbox1)和combobox(ComboBox1).
这是表格中的代码

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



因此,当我将selectedIndex设置为2时,触发combobox1是有效的(我可以看到消息框),但Textbox1不起作用意味着尽管我使用Form1.TextBox1.Text ="123",但我无法设置文本值.

如何使用class
设置TextBox1.Text值
请启发我.

干杯
df



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

推荐答案

我认为我看到了您的问题.首先,您需要记住Form1是表单的类名,并且不引用正在运行的特定实例.您需要将对Form1当前实例的引用传递到Class1实例中,以便该实例可以访问表单上的UI元素.其次,您不能直接从另一个线程访问UI元素.

为了解决这些问题,我首先要问这是否真的必要.我假设这是用于概念验证的示例代码.如果不是,则丢失线程并丢失类实例.至少不要期望从类实例访问UI.相反,让类封装所需要做的工作,然后让其从方法中返回所需的值,或者让它更新类中的属性,然后可以在以后在Form1后面的方法中访问该类中的属性.

如果必须执行线程处理并且必须从Class1实例访问Form1,则需要将类实例传递给Class1构造函数.另外,您将需要使用类似于此链接中的代码的代码来修改线程调用:

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


这篇关于无法从类的窗体中将值设置为文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆