如何在 Visual C# 中更改另一种形式的文本框中的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1806795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change text in a textbox on another form in Visual C#?
提问by neuromancer
In Visual C# when I click a button, I want to load another form. But before that form loads, I want to fill the textboxes with some text. I tried to put some commands to do this before showing the form, but I get an error saying the textbox is inaccessible due to its protection level.
在 Visual C# 中,当我单击一个按钮时,我想加载另一个表单。但在该表单加载之前,我想用一些文本填充文本框。我试图在显示表单之前放置一些命令来执行此操作,但是我收到一条错误消息,指出文本框由于其保护级别而无法访问。
How can I set the textbox in a form before I show it?
如何在显示之前在表单中设置文本框?
private void button2_Click(object sender, EventArgs e)
{
fixgame changeCards = new fixgame();
changeCards.p1c1val.text = "3";
changeCards.Show();
}
回答by David Hall
When you create the new form in the button click event handler, you instantiate a new form object and then call its show method.
当您在按钮单击事件处理程序中创建新表单时,您将实例化一个新表单对象,然后调用其 show 方法。
Once you have the form object you can also call any other methods or properties that are present on that class, including a property that sets the value of the textbox.
拥有表单对象后,您还可以调用该类中存在的任何其他方法或属性,包括设置文本框值的属性。
So, the code below adds a property to the Form2 class that sets your textbox (where textbox1 is the name of your textbox). I prefer this method method over making the TextBox itself visible by modifying its access modifier because it gives you better encapsulation, ensuring you have control over how the textbox is used.
因此,下面的代码向 Form2 类添加了一个属性来设置您的文本框(其中 textbox1 是您的文本框的名称)。我更喜欢这种方法,而不是通过修改其访问修饰符来使 TextBox 本身可见,因为它为您提供了更好的封装,确保您可以控制文本框的使用方式。
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string TextBoxValue
{
get { return textBox1.Text;}
set { textBox1.Text = value;}
}
}
And in the button click event of the first form you can just have code like:
在第一个表单的按钮单击事件中,您可以使用以下代码:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.TextBoxValue = "SomeValue";
form2.Show();
}
回答by Laserson
You can set "Modifiers" property of TextBox control to "Public"
您可以将 TextBox 控件的“Modifiers”属性设置为“Public”
回答by pFrenchie
In the designer code-behind file simply change the declaration of the text box from the default:
在设计器代码隐藏文件中,只需更改文本框的默认声明即可:
private System.Windows.Forms.TextBox textBox1;
to:
到:
protected System.Windows.Forms.TextBox textBox1;
The protected keyword is a member access modifier. A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member (for more info, see this link).
protected 关键字是成员访问修饰符。受保护的成员可以从声明它的类中访问,也可以从声明此成员的类派生的任何类中访问(有关更多信息,请参阅此链接)。
回答by Salahu92
I also had the same doubt, So I searched on internet and found a good way to pass variable values in between forms in C#, It is simple that I expected. It is nothing, but to assign a variable in the first Form and you can access that variable from any form. I have created a video tutorial on 'How to pass values to a form'
我也有同样的疑问,所以我在互联网上搜索并找到了一种在 C# 中的表单之间传递变量值的好方法,这很简单,如我所料。没什么,只是在第一个表单中分配一个变量,您可以从任何表单访问该变量。我创建了一个关于“如何将值传递给表单”的视频教程
Go to the below link to see the Video Tutorial.
转到以下链接查看视频教程。
回答by Mustafa Burak Kalkan
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
TextBox txt = (TextBox)frm.Controls.Find("p1c1val", true)[0];
txt.Text = "foo";
}
回答by ib11
I know this was long time ago, but seems to be a pretty popular subject with many duplicate questions. Now I had a similar situation where I had a class that was called from other classes with many separate threads and I had to update one specific form from all these other threads. So creating a delegate event handler was the answer.
我知道这是很久以前的事了,但似乎是一个非常受欢迎的主题,有很多重复的问题。现在我遇到了类似的情况,我有一个从具有许多单独线程的其他类调用的类,我必须从所有这些其他线程更新一个特定的表单。所以创建一个委托事件处理程序就是答案。
The solution that worked for me:
对我有用的解决方案:
I created an event in the class I wanted to do the update on another form. (First of course I instantiated the form (called
SubAsstToolTipWindow
) in the class.)Then I used this event (
ToolTipShow
) to create an event handler on the form I wanted to update the label on. Worked like a charm.
我在课堂上创建了一个事件,我想在另一个表单上进行更新。(当然,我首先
SubAsstToolTipWindow
在类中实例化了表单(称为)。)然后我使用这个事件 (
ToolTipShow
) 在我想要更新标签的表单上创建一个事件处理程序。像魅力一样工作。
I used this descriptionto devise my own code below in the class that does the update:
我使用此描述在执行更新的类中设计我自己的以下代码:
public static class SubAsstToolTip
{
private static SubAsstToolTipWindow ttip = new SubAsstToolTipWindow();
public delegate void ToolTipShowEventHandler();
public static event ToolTipShowEventHandler ToolTipShow;
public static void Show()
{
// This is a static boolean that I set here but is accessible from the form.
Vars.MyToolTipIsOn = true;
if (ToolTipShow != null)
{
ToolTipShow();
}
}
public static void Hide()
{
// This is a static boolean that I set here but is accessible from the form.
Vars.MyToolTipIsOn = false;
if (ToolTipShow != null)
{
ToolTipShow();
}
}
}
Then the code in my form that was updated:
然后更新了我表单中的代码:
public partial class SubAsstToolTipWindow : Form
{
public SubAsstToolTipWindow()
{
InitializeComponent();
// Right after initializing create the event handler that
// traps the event in the class
SubAsstToolTip.ToolTipShow += SubAsstToolTip_ToolTipShow;
}
private void SubAsstToolTip_ToolTipShow()
{
if (Vars.MyToolTipIsOn) // This boolean is a static one that I set in the other class.
{
// Call other private method on the form or do whatever
ShowToolTip(Vars.MyToolTipText, Vars.MyToolTipX, Vars.MyToolTipY);
}
else
{
HideToolTip();
}
}
I hope this helps many of you still running into the same situation.
我希望这可以帮助你们中的许多人仍然遇到同样的情况。
回答by cinemaputar.com
Try this.. :)
尝试这个.. :)
Form1 f1 = (Form1)Application.OpenForms["Form1"];
TextBox tb = (TextBox)f1.Controls["TextBox1"];
tb.Text = "Value";