如何从嵌套结构的 ToString 方法中访问表单控件

发布于 2024-10-05 10:08:22 字数 492 浏览 4 评论 0原文

我正在用 C# 构建一个小型 Windows 窗体应用程序。在表单代码中,我使用 ToString 方法定义了一个公共结构,该方法必须从同一表单上的组合框中的项目构建其部分输出。这听起来并不困难,

public partial class Form1 : Form
{
 public struct OrderLineItem
    {
     string someString;
     int index;
     string ToString()
        {return someString + ActiveForm.sizeComboBox.Items[index].ToString();}  
    }
}

它抱怨它找不到 sizeComboBox 的定义。如果我明确使用表单的名称,则表示静态字段需要对象引用...... 我不太确定这是什么意思。使用 this.sizeComboBox 指的是结构,而不是表单。仅使用 sizeComboBox,同样需要对象引用。

I am building a small windows form application in C#. Within the form code I define a public struct with a ToString method that must build part of its output from items in comboBoxes on the same form. This doesn't sound like it should be difficult

public partial class Form1 : Form
{
 public struct OrderLineItem
    {
     string someString;
     int index;
     string ToString()
        {return someString + ActiveForm.sizeComboBox.Items[index].ToString();}  
    }
}

It complains that it cannot find a definition for sizeComboBox. If I explicitly use the name of the form, it says an object reference is required for the static field...
I'm not quite sure what it means by that. Using this.sizeComboBox refers to the struct, not the form. Using just sizeComboBox, again, an object reference is required.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄与心 2024-10-12 10:08:22

该结构知道其包含类 type,但它不知道该类型的任何特定实例,除非您告诉它。例如,您可以创建一个采用 Form1 对象的构造函数,并且它可以在成员变量中保存对该表单的引用。

public partial class Form1 : Form 
{ 
 public struct OrderLineItem 
    { 
     string someString; 
     int index; 
     Form1 parentForm;

     internal OrderLineItem(Form1 parentForm)
     {
         this = new OrderLineItem();
         this.parentForm = parentForm;
     }

     string ToString() 
     {
         if (parentForm == null)
             return string.Empty;
         else
             return someString + parentForm.sizeComboBox.Items[index].ToString();
     }   
    } 
} 

也就是说,这是一个严重值得怀疑的设计。

该应用程序的分层结构似乎是颠倒的。订单行项目对象应存在于比用户界面层更低的级别。 UI 有时可以看到业务对象(订单、订单行项目等),但业务对象不应该了解有关 UI 的任何信息。

如果你能反转这个结构,它会让代码变得更加简洁。

The struct knows about its containing class type, but it doesn't know about any particular instance of that type, unless you tell it. For example, you can create a constructor that takes a Form1 object, and it can save a reference to that form in a member variable.

public partial class Form1 : Form 
{ 
 public struct OrderLineItem 
    { 
     string someString; 
     int index; 
     Form1 parentForm;

     internal OrderLineItem(Form1 parentForm)
     {
         this = new OrderLineItem();
         this.parentForm = parentForm;
     }

     string ToString() 
     {
         if (parentForm == null)
             return string.Empty;
         else
             return someString + parentForm.sizeComboBox.Items[index].ToString();
     }   
    } 
} 

That said, this is a seriously questionable design.

The tiered structure of this application appears to be upside-down. The order-line-item objects should exist at a lower level than the user interface layer. The UI can sometimes see the business objects (order, order-line-item, etc.), but the business objects should not know anything about the UI.

If you can invert this structure, it will make the code much cleaner.

海螺姑娘 2024-10-12 10:08:22

假设另一个表单是 Form2,您可以将 ActiveForm 转换为 Form2

var form2 = ActiveForm as Form2;
if (form2 != null) // form2 == null if ActiveForm is not of type Form2.
{
    form2.sizeComboBox...
}

编辑:
两个笔记。

  1. 与其获取 ActiveForm,不如在创建 form2 时将 form2 存储在 form1 的成员变量中。
  2. 您应该将组合框值的获取封装在 Form2 中的属性后面,例如 SelectedFooValue

Suppose the other form is Form2 you can cast ActiveForm to Form2:

var form2 = ActiveForm as Form2;
if (form2 != null) // form2 == null if ActiveForm is not of type Form2.
{
    form2.sizeComboBox...
}

Edit:
Two notes.

  1. Instead of getting ActiveForm it is better store form2 in a member variable in form1 when form2 is created.
  2. You should encapsulate the getting of combobox values behind a property in Form2, like SelectedFooValue.
春花秋月 2024-10-12 10:08:22
    public partial class Form1 : Form
{
    internal static Form1 ActiveForm { get; set; }

    public Form1()
    {
        InitializeComponent();
        ActiveForm = this;
    }

    public struct OrderLineItem
    {
        public override string ToString()
        {
            return ActiveForm.sizeComboBox.Items[index].ToString();
        }
    }

但请注意,这不是正确的方法。也许您可以发布您想要完成的任务,我们可以提供帮助?

    public partial class Form1 : Form
{
    internal static Form1 ActiveForm { get; set; }

    public Form1()
    {
        InitializeComponent();
        ActiveForm = this;
    }

    public struct OrderLineItem
    {
        public override string ToString()
        {
            return ActiveForm.sizeComboBox.Items[index].ToString();
        }
    }

However note that this is not the correct approach. Maybe you can post what you are trying to accomplish and we can help?

吻安 2024-10-12 10:08:22

您需要将 ActiveForm 转换为 Form1

我认为是这样的(现在不要打开 VS 来检查):

return someString + ((Form1)ActiveForm).sizeComboBox.Items[index].ToString();

但是,这通常不是解决问题的好方法,您不应该让类和结构中的方法将目录引用到控件,因为您将它们紧紧地绑在一起。尝试将数据发送到结构中,或者在表单上创建一个方法以某种方式返回数据。

You need to cast ActiveForm to Form1.

Something like this I think (don't have VS open now to check):

return someString + ((Form1)ActiveForm).sizeComboBox.Items[index].ToString();

However, this is generally not a good way of going about things, you shouldn't make your methods in your classes and structs refer directory to controls since then you tie them together to closely. Try to send the data in to the struct instead or create a method on the form to return the data in some way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文