【问题标题】:How to access Winform textbox control from another class?如何从另一个类访问 Winform 文本框控件?
【发布时间】:2011-08-04 13:17:53
【问题描述】:

我有一个名为 Form1winform 和一个名为 textBox1

textbox

Form1 我可以通过键入来设置文本:

textBox1.text = "change text";

现在我创建了另一个类。如何在这个类中调用 textBox1? 所以我想在这个类中更改 textBox1 的文本。

如何从这个新类访问 Form1

【问题讨论】:

    标签: c# winforms class inheritance controls


    【解决方案1】:

    您需要对表单的实例有一些访问权限才能访问其Controls 集合,从而更改Text Box's 文本。

    其中一种方法可能是您可以将您的表单实例作为公共或更好地使用为您的第二个表单创建一个新的构造函数,并让它在初始化期间接收 Form1 的实例。

    【讨论】:

      【解决方案2】:

      如果您的其他类继承 Form1 并且您的 textBox1 被声明为 public,那么您可以通过简单地调用从其他类访问该文本框:

      otherClassInstance.textBox1.Text = "hello world";
      

      【讨论】:

        【解决方案3】:

        怎么样

        Form1.textBox1.text = "更改文本";

        注意: 1.您必须将 Form1 “包含”到您的第二个表单源文件中 使用Form1;

        1. textBox1 应该是公开的

        【讨论】:

        • 您假设 textBox1 是静态成员。您应该在回答中提及这一点。
        【解决方案4】:

        定义表单的属性,然后在其他地方使用它,它将与表单实例一起使用

        public string SetText
        {
          get { return textBox1.Text; }
          set { textBox1.Text = value; }
        }
        

        【讨论】:

        • 不错的方式,但是这个属性会在哪里居住.. 如果它在Form1 Class hen 你仍然需要Instance 来获得这个属性
        【解决方案5】:

        使用全局变量或属性为文本框赋值,为另一个类中的变量赋值,并将其赋值给表单类中的textbox.text。

        【讨论】:

          【解决方案6】:

          我建议你不要这样做。您真的想要一个依赖于文本编辑在表单中实现方式的类,还是想要一种允许您获取和设置文本的机制

          我建议后者。因此,在您的表单中,创建一个包含相关TextBox 控件的Text 属性的属性:

          public string FirstName
          {
              get { return firstNameTextBox.Text; }
              set { firstNameTextBox.Text = value; }
          }
          

          接下来,创建一些机制,通过它您的类可以获得对表单的引用(例如通过构造函数)。然后该类可以使用该属性来访问和修改文本:

          class SomeClass
          {
              private readonly YourFormClass form;
              public SomeClass(YourFormClass form)
              {
                  this.form = form;
              }
          
              private void SomeMethodDoingStuffWithText()
              {
                  string firstName = form.FirstName;
                  form.FirstName = "some name";
              }
          }
          

          更好的解决方案是在接口中定义可能的交互,并让该接口成为您的表单和其他类之间的契约。这样类就与表单完全解耦,并且可以使用任何实现接口的方法(这为更容易测试打开了大门):

          interface IYourForm
          {
              string FirstName { get; set; }
          }
          

          在您的表单类中:

          class YourFormClass : Form, IYourForm
          {
              // lots of other code here
          
              public string FirstName
              {
                  get { return firstNameTextBox.Text; }
                  set { firstNameTextBox.Text = value; }
              }
          }
          

          ...和班级:

          class SomeClass
          {
              private readonly IYourForm form;
              public SomeClass(IYourForm form)
              {
                  this.form = form;
              }
          
              // and so on
          
          }
          

          【讨论】:

          • +1 看起来很棒.. 这是My ownV4Vendetta's 答案的完美组合..
          • 我如何调用类SomeClass的构造函数并传递IYourForm接口?
          • 嗨!但是这种情况下你还是传递form对象,所以SomeClass代码不是和Form代码隔离的吗?这种模式是否允许将输出转发到控制台?或者我应该用什么来制作完全独立的记录器?
          • @GennadyG SomeClass 不直接依赖于表单代码:它依赖于IYourForm 接口,它可能是一个表单,或者完全不同的东西。
          • @GennadyG 这个讨论偏离了这个问题的主题,所以我建议将它作为一个单独的问题提出(在搜索可能已经被问和回答的类似问题之后):-)
          【解决方案7】:

          我找到了一个简单的方法来做到这一点,我已经测试过了,它可以正常工作。 首先,我创建了一个 Windows 项目,在表单上我插入了一个文本框并将其命名为 textBox1 然后我插入了一个名为 button1 的按钮,然后添加了一个名为 class1 的类。 在 class1 中,我创建了一个 TextBox:

          class class1
              {
               public static TextBox txt1=new TextBox();  //a global textbox to interfece with      form1
              public static void Hello()
                {
                 txt1.Text="Hello";
                }
              }
          

          现在在您的表单中执行以下操作:

          public partial class Form1 : Form
              {
               public Form1()
               {
                InitializeComponent();  
               }
               private void button1_Click(object sender, EventArgs e)
                {
                 class1.txt1=textBox1;
                 class1.Hello();
                 }
              }
          

          在 button1_Click 我将对象 textBox1 复制到 txt1 中,所以现在 txt1 具有属性 的 textBox1 和你可以改变 textBox1 其他形式或类的文本。

          【讨论】:

            【解决方案8】:

            我也遇到了同样的问题,我无法将文本附加到 Form 类的 RichTextBox 中。所以我创建了一个名为update 的方法,用于传递来自Class1 的消息。

            类:Form1.cs

            public partial class Form1 : Form
                {
                    public Form1()
                    {
                        InitializeComponent();
                        _Form1 = this;
                    }
                    public static Form1 _Form1;
            
                    public void update(string message)
                    {
                        textBox1.Text = message;
                    }
            
                    private void Form1_Load(object sender, EventArgs e)
                    {
                        Class1 sample = new Class1();            
                    }
                }
            

            类:Class1.cs

            public class Class1
                {        
                    public Class1()
                    {
                        Form1._Form1.update("change text");
                    }        
                }
            

            【讨论】:

              【解决方案9】:
              Form1 form = new Form1(); 
              form.textBox1.Text = "test";
              

              【讨论】:

              • 您好,欢迎来到 StackOverflow。请考虑在您的答案中详细说明(特别是,如果您有 4 年的时间想出一个答案),因为解释总是有助于从错误中吸取教训。
              【解决方案10】:

              您可以将Form1.Designer.cs 中生成的字段的访问修饰符从private 更改为public。改变这个

              private System.Windows.Forms.TextBox textBox1;
              

              通过这个

              public System.Windows.Forms.TextBox textBox1;
              

              您现在可以使用Form1.textBox1 形式的引用来处理它。

              如果您对控件属性进行任何更改,Visual Studio 将不会覆盖它,除非您将其删除并重新创建。

              如果您不习惯直接编辑代码,也可以从 UI 中更改它。查找 Modifiers 属性:

              【讨论】:

                【解决方案11】:

                我尝试了上面的示例,但没有一个按描述的那样工作。但是,我有一个结合了一些示例的解决方案:

                public static Form1 gui;
                public Form1()
                {
                    InitializeComponent();
                    gui = this;
                    comms = new Comms();
                
                }
                public Comms()
                {
                    Form1.gui.tsStatus.Text = "test";
                    Form1.gui.addLogLine("Hello from Comms class");
                    Form1.gui.bn_connect.Text = "Comms";
                }
                

                只要您不使用线程,它就可以工作。使用线程需要更多代码,而我的任务不需要。

                【讨论】:

                  【解决方案12】:

                  我使用这种方法更新标签,但您可以轻松地将其更改为文本框:

                  类:

                  public Class1
                  {
                      public Form_Class formToOutput;
                  
                      public Class1(Form_Class f){
                          formToOutput = f;
                      }
                  
                      // Then call this method and pass whatever string
                      private void Write(string s)
                      {
                          formToOutput.MethodToBeCalledByClass(s);
                      }
                  }
                  

                  进行更新的表单方法:

                  public Form_Class{
                  
                      // Methods that will do the updating
                      public void MethodToBeCalledByClass(string messageToSend)
                      {
                         if (InvokeRequired) { 
                             Invoke(new OutputDelegate(UpdateText),messageToSend); 
                         }
                      }
                  
                      public delegate void OutputDelegate(string messageToSend);
                      public void UpdateText(string messageToSend)
                      {
                         label1.Text = messageToSend;
                      }
                  }
                  

                  终于

                  只需通过构造函数传递表单:

                  Class1 c = new Class1(this);
                  

                  【讨论】:

                    【解决方案13】:
                    public partial class Form1 : Form
                    {
                    
                        public static Form1 gui;
                        public Form1()
                        {
                            InitializeComponent();
                            gui = this;
                    
                        }
                        public void WriteLog(string log)
                        {
                            this.Invoke(new Action(() => { txtbx_test1.Text += log; }));
                    
                        }
                    }
                    public class SomeAnotherClass
                    {
                        public void Test()
                        {
                            Form1.gui.WriteLog("1234");
                        }
                    }
                    

                    我喜欢这个解决方案。

                    【讨论】:

                      【解决方案14】:
                      Form frm1 = new Form1();
                      frm1.Controls.Find("control_name",true)[0].Text = "I changed this from another form";
                      

                      【讨论】:

                      • 嗨,克里斯,欢迎来到 StackOverflow!为了帮助其他人理解您的答案,请考虑对您的代码及其解决问题的方式进行简要说明。
                      【解决方案15】:

                      // 将 Active 表单转换为表单变量。

                      Form F1 = myForm1.ActiveForm;

                      //找出控件并更改属性

                      F1.Controls.Find("Textbox1", true).ElementAt(0).Text= "Whatever you want to write";

                      【讨论】:

                      猜你喜欢
                      • 2012-10-25
                      • 2023-04-04
                      • 2012-09-07
                      • 2017-06-15
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多