从我的表单 class 之外的另一个 class 更新文本框 C#
Updating textbox from another class outside of my Form class C#
我已经看到了一些关于此尝试的链接,但我还没有找到解决方案。我正在尝试访问我的表单文本框并使用来自另一个 class 的文本更新它。我可以直接更新 DataOrganizerForm
class 中的文本,但是当我将文本传回 DataOrganizerForm
class 时,它不会在 GUI 上更新。这是我拥有的:
public partial class DataOrganizerForm : Form
{
//Default constructor
public DataOrganizerForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//Handle a Start/Stop button click
private void start_stop_button_Click(object sender, EventArgs e)
{
SerialNumberSearcher snsearch = new SerialNumberSearcher();
snsearch.searchSN();
}
//Allow simple access to update to notification textbox
public void setNotificationText(string text)
{
notification_textbox.Text = text;
}
}
public class SerialNumberSearcher
{
public void searchSN()
{
DataOrganizerForm formAccess = new DataOrganizerForm();
formAccess.setNotificationText("Updated text from different class");
}
}
嗯,它不会更新文本框,因为您正在实例化 DataOrganizerForm 的另一个对象 class。您可以做的是将表单对象的引用传递给 SerialNumberSearcher,如下所示:
public class SerialNumberSearcher
{
private readonly DataOrganizerForm _form;
public SerialNumberSearcher(DataOrganizerForm form)
{
_form = form;
}
public void searchSN()
{
_form.setNotificationText("Updated text from different class");
}
}
您需要了解您在哪个实例上操作。当您使用 new-eperator 时,您会创建一个 new 实例,就像该类型的新副本一样。
DataOrganizerForm formAccess = new DataOrganizerForm();
原始 Form
与您在 searchSN
方法中创建的实例不同。
您需要将该实例传递到方法中以对其进行操作:
public void searchSN(DataOrganizerForm formAccess )
{
formAccess.setNotificationText("Updated text from different class");
}
调用此方法时需要使用this
引用当前对象:
private void start_stop_button_Click(object sender, EventArgs e)
{
SerialNumberSearcher snsearch = new SerialNumberSearcher();
snsearch.searchSN(this);
}
this 将访问 Form
的当前实例,从而允许您操作您感兴趣的文本框。
When do you use the “this” keyword? 也可能有帮助
感谢您的帮助。这就是我能够使我的应用程序正常工作的方法。我通过引用我的另一个 class 传递了文本框对象,并且能够以这种方式显示我的信息。此外,我在让我的文本框不断更新时遇到了问题。我不得不添加
public partial class DataOrganizerForm : Form
{
//Default constructor
public DataOrganizerForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//Handle a Start/Stop button click
private void start_stop_button_Click(object sender, EventArgs e)
{
SerialNumberSearcher snsearch = new SerialNumberSearcher();
snsearch.searchSN(notification_textbox);
}
//Allow simple access to update to notification textbox
public void setNotificationText(string text)
{
notification_textbox.Text = text;
notification_textbox.Update();
}
}
public class SerialNumberSearcher
{
public void searchSN(Textbox notifyTextbox)
{
notifyTextbox.setNotificationText = "Updated text from different class";
notifyTextbox.Update();
}
}
我已经看到了一些关于此尝试的链接,但我还没有找到解决方案。我正在尝试访问我的表单文本框并使用来自另一个 class 的文本更新它。我可以直接更新 DataOrganizerForm
class 中的文本,但是当我将文本传回 DataOrganizerForm
class 时,它不会在 GUI 上更新。这是我拥有的:
public partial class DataOrganizerForm : Form
{
//Default constructor
public DataOrganizerForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//Handle a Start/Stop button click
private void start_stop_button_Click(object sender, EventArgs e)
{
SerialNumberSearcher snsearch = new SerialNumberSearcher();
snsearch.searchSN();
}
//Allow simple access to update to notification textbox
public void setNotificationText(string text)
{
notification_textbox.Text = text;
}
}
public class SerialNumberSearcher
{
public void searchSN()
{
DataOrganizerForm formAccess = new DataOrganizerForm();
formAccess.setNotificationText("Updated text from different class");
}
}
嗯,它不会更新文本框,因为您正在实例化 DataOrganizerForm 的另一个对象 class。您可以做的是将表单对象的引用传递给 SerialNumberSearcher,如下所示:
public class SerialNumberSearcher
{
private readonly DataOrganizerForm _form;
public SerialNumberSearcher(DataOrganizerForm form)
{
_form = form;
}
public void searchSN()
{
_form.setNotificationText("Updated text from different class");
}
}
您需要了解您在哪个实例上操作。当您使用 new-eperator 时,您会创建一个 new 实例,就像该类型的新副本一样。
DataOrganizerForm formAccess = new DataOrganizerForm();
原始 Form
与您在 searchSN
方法中创建的实例不同。
您需要将该实例传递到方法中以对其进行操作:
public void searchSN(DataOrganizerForm formAccess )
{
formAccess.setNotificationText("Updated text from different class");
}
调用此方法时需要使用this
引用当前对象:
private void start_stop_button_Click(object sender, EventArgs e)
{
SerialNumberSearcher snsearch = new SerialNumberSearcher();
snsearch.searchSN(this);
}
this 将访问 Form
的当前实例,从而允许您操作您感兴趣的文本框。
When do you use the “this” keyword? 也可能有帮助
感谢您的帮助。这就是我能够使我的应用程序正常工作的方法。我通过引用我的另一个 class 传递了文本框对象,并且能够以这种方式显示我的信息。此外,我在让我的文本框不断更新时遇到了问题。我不得不添加
public partial class DataOrganizerForm : Form
{
//Default constructor
public DataOrganizerForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//Handle a Start/Stop button click
private void start_stop_button_Click(object sender, EventArgs e)
{
SerialNumberSearcher snsearch = new SerialNumberSearcher();
snsearch.searchSN(notification_textbox);
}
//Allow simple access to update to notification textbox
public void setNotificationText(string text)
{
notification_textbox.Text = text;
notification_textbox.Update();
}
}
public class SerialNumberSearcher
{
public void searchSN(Textbox notifyTextbox)
{
notifyTextbox.setNotificationText = "Updated text from different class";
notifyTextbox.Update();
}
}