在单独的表单(C#)上写入文本框 [英] Writing to a textbox on a separate form (C#)
问题描述
假设我有两种形式:Form1和Form2. Form2有一个名为text1的文本控件
Suppose I have two forms: Form1 and Form2. Form2 has a text control named text1
在VB6中,我可以写到Form 2的文本框
In VB6 I could write to Form 2's textbox
控件:Form2.Text1.text =此处有一些文本"
control from Form1 by using: Form2.Text1.text = "Some text here"
如何使用C#进行这项工作?我已经尝试了一切!
How can I make this work in C#? I have tried everything!
我尝试过的事情:
Form2 Frm2 = new Form2();
Frm2.show();
Frm2.Activate(); // Trying everything to make sure it sees the form (it does).
Frm2.Text1 (Doesn't find the control)...
答案:
我最终在Form2中创建了一个公共例程,然后仅从Form1中调用了该例程.在这个Form2的公共例程中,我将调用文本框!
I ended up making a public routine in Form2, and then just calling this routine from form1. In this public routine of Form2 I would then call the textbox!
推荐答案
我的目标是将文本添加到另一种形式的文本框中.我有Form1和Form2. Form2有一个名为Text1的文本框控件.为了完成这项工作,我创建了一个子例程:
My goal was add text to a Text Box on another form. I had Form1 and Form2. Form2 has a text box control named Text1. In order to make this work, I created a Sub Routine:
public Void WriteToText(string sData)
{
// Here is where I wrote to my textbox
Text1.text = sData;
}
Form 1代码:
Form 1 code:
Form2 Frm2 = new Form2();
Frm2.WriteToText("My Data");
这篇关于在单独的表单(C#)上写入文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!