跨类操作表单文本

跨类操作表单文本

本文介绍了跨类操作表单文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个时间让这个工作的工作,我有两个类,form1.cs和logic.cs



在form1上,我有一个私有变量output,带有可以设置值的公共属性Output。我也有一个公共方法设置textbox1.Text的内容(保持textbox1私有)。

 命名空间myNamespace 
{
public partial class Form1:Form
{
私有字符串输出;

public string输出
{
get {return output; }
set
{
if(!String.IsNullOrEmpty(value))
输出+ = value;
}
}

public void SetTextbox1(string value)
{
textbox1.Text = value;
}
}
}

其中我试图更改form1上的变量来更新textbox1

 命名空间myNamespace 
{
类逻辑
{
public void Info(string imagePath,string mountDir)
{
mountDir =C:\\mount


{
DismImageInfoCollection imageInfo = DismApi.GetImageInfo(mountDir)
form1.Output = imageInfo.ToString();
form1.SetTxtOutput(form1.Output);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}



在尝试的最后两行中获得错误消息,说:



非静态字段,方法,或属性'form1.Output'



如果我将Output,SetTxtOutput()和output更改为static,则会提示,因为textbox1不是



在没有将文本框设置为public和直接操作的情况下,在不同类中操作textbox Text值的正确方法是什么?

解决方案

您需要将form1对象实际传递给逻辑类Ie

  //默认构造函数
逻辑(form1 Form)
{
Form.output;
}

你得到错误的原因是因为winforms创建一个新类每个表单,并访问一个类的元素,而不需要实例化它那些元素需要有static关键字。



注意如果你只是创建一个新的form1实例( form1 form = new form1())这将创建一个新的表单实例,而不是显示的表单。


I'm having a humdinger of a time getting this to work, and I feel like it's so simple...

I have 2 classes, form1.cs and logic.cs

On form1, I have a private variable "output", with public property Output that I can set the value. I also have a public method to set the contents of textbox1.Text (keeping textbox1 private).

namespace myNamespace
{
    public partial class Form1 : Form
    {
        private string output;

        public string Output
        {
            get { return output; }
            set
            {
                if (!String.IsNullOrEmpty(value))
                    output += value;
            }
        }

        public void SetTextbox1(string value)
        {
            textbox1.Text = value;
        }
    }
}

On logic, I have instances where I'm trying to change the variable on form1 to update the textbox1

namespace myNamespace
{
    class logic
    {
        public void Info(string imagePath, string mountDir)
        {
            mountDir = "C:\\mount"

            try
            {
                DismImageInfoCollection imageInfo = DismApi.GetImageInfo(mountDir)
                form1.Output = imageInfo.ToString();
                form1.SetTxtOutput(form1.Output);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}

I'm getting error messages in the last 2 lines of try that say:

An object reference is required for the non-static field, method, or property 'form1.Output'

If I change Output, SetTxtOutput(), and output to static, it complains because textbox1 isn't static as well.

What is the proper way of manipulating textbox Text values across classes without setting the textbox to public and manipulating it directly?

解决方案

You need to actually pass the form1 object to the logic class I.e.

 //default constructor
 Logic(form1 Form)
 {
     Form.output;
 }

The reason that you are getting the error is because winforms creates a new class for each form, and to access elements of a class without instantiating it those elements need to have the static keyword.

As a side note if you just create a new instance of form1 (form1 form = new form1()) this creates a new instance of the form and not the displayed form.

这篇关于跨类操作表单文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 02:19