【问题标题】:Update textbox from a different class从不同的类更新文本框
【发布时间】:2014-11-07 21:04:25
【问题描述】:

我对 C# 比较陌生,有点卡住了。

我在表单上有一个富文本框,我想将它从不同的类更新到表单本身。

我第一次尝试

Form1.outputTextbox.AppendText(string);  

但文本框无法访问,这是有道理的。因此,我尝试创建一个函数。在 Form1 上我创建了函数

public void updateTextBox(string new_text)
    {
        outputTextBox.AppendText(new_text);
    }

在我使用的课程中。

Form1.updateTextBox("apple");

我遇到的问题是我的类可以看到该函数的唯一方法是,如果我将其设为静态函数,但是当我这样做时会出现错误“非静态字段、方法或属性“成员””

我是完全关闭还是走错了路?如有任何帮助,将不胜感激。

【问题讨论】:

  • 如果您不想使用静态方法,则需要一个表单实例。例如:Form1 myform; Form1 是类型,myform 是实例。

标签: c# .net winforms


【解决方案1】:

或者,您可以执行以下操作。这利用了自定义参数和事件。

namespace WindowsFormsApplication3
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    public partial class Form1 : Form
    {
        TextBox textBox;
        SomeClass someClass;

        public Form1()
        {
            InitializeComponent();
            Initialize();
            BindComponents();
        }

        private void BindComponents()
        {
            //EventHandlers
            this.Load += new EventHandler(Form1_Load);
            this.someClass.TextUpdatedEvent += new EventHandler(someClass_TextUpdatedEvent);
        }

        void someClass_TextUpdatedEvent(object sender, EventArgs e)
        {
            this.textBox.Text = (e as FormArgs).Text;
        }

        private void Initialize()
        {
            this.textBox = new TextBox();
            this.someClass = new SomeClass();
        }

        void Form1_Load(object sender, EventArgs e)
        {
            this.Controls.Add(textBox);
        }
    }

    public class SomeClass
    {
        public event EventHandler TextUpdatedEvent = delegate { };

        public void UpdateText(string text)
        {
            if (TextUpdatedEvent != null)
            {
                TextUpdatedEvent(this, new FormArgs() { Text = text });
            }
        }
    }

    public class FormArgs : EventArgs
    {
        public string Text { get; set; }
    }
}

如果你这样做,你可以像这样更新表单文本:

someClass.UpdateText("changing the text on the form");

【讨论】:

    【解决方案2】:

    您正试图从类而不是对象访问函数。 使用

    Form1 myForm = new Form1();
    ...
    myForm.updateTextBox("whatever");
    

    还要注意这里的线程问题。什么触发了外部代码?是否是另一个ui操作,那么一切都很好。它是否来自另一个线程,那么您必须处理它。

    【讨论】:

    • 我想我现在可能在这里遇到线程问题。使用您提供的代码,程序现在可以正常运行,但文本实际上并未出现在文本框中。我很快尝试从与文本框相同的表单中调用该函数,并且它起作用了。到目前为止,感谢您的帮助,如果您能指出正确的方向,那就太好了。
    • 所以像 public void updateTextBox(String value) { outputTextbox.Invoke((MethodInvoker)delegate { outputTextbox.Text = value; }); }
    【解决方案3】:

    通过构造函数或属性将 Form1 实例传递给另一个类。然后您可以从其他类访问 outputTextbox。

    public class OtherClass
    {
        private Form1 form1;
        public OtherClass(Form1 form1)
        {
            this.form1 = form1;
        }
    
        private void ChangeText()
        {
            form1.outputTextBox.AppendText("hello world");
        }
    }
    

    从 Form1.cs 实例化 OtherClass 并传递其实例。 在 Form1.cs 中:

    OtherClass obj = new OtherClass(this);
    

    【讨论】:

      【解决方案4】:

      我知道已经很晚了,但也许有人需要解决方案... 您可以通过将其作为参数传递给构造函数来从另一个表单访问所有控制器而无需创建对象...

      举例

          public partial class Form2 : Form
          {
              MainForm mainForm;
      
              public Form2(MainForm mainForm)
              {
                  InitializeComponent();
                 this.mainForm = mainForm;
                  txtRecive00.TextChanged += new EventHandler(txtRecive8changed);
              }
      
              void txtRecive8changed(object sender, EventArgs e)
              {
                  mainForm.txtRecive1.Text += txtRecive00.Text;
              }
      

      就我而言,我可以从 Form2 更新 mainForm.txtRecive1.Text 中的文本...

      在 MineForm 中,我们像这样从 Form2 创建对象:

      Form2 f2 = new FormMeasure(this);
      

      欲了解更多信息,请观看此短视频https://www.youtube.com/watch?v=CdH8z_JNi_U

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-10
        • 2012-01-15
        • 1970-01-01
        • 1970-01-01
        • 2023-03-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多