简体   繁体   English

如何将两个或多个构造函数传递给另一种形式?

[英]How to pass two or more constructors to another form?

I have a list view with data in it. 我有一个包含数据的列表视图。 I wanted to transfer those data into a form called view/edit form. 我想将这些数据传输到称为查看/编辑表单的表单中。 What I did was when a row is clicked it would transfer the data to a set of hidden textbox that would store the data from the list view. 我所做的是,当单击一行时,它将数据传输到一组隐藏的文本框,该文本框将从列表视图存储数据。 My plan is to transfer it to another form. 我的计划是将其转换为另一种形式。

In my view & edit button, I have already used a constructor to pass a text (1,2 & 3). 在查看和编辑按钮中,我已经使用了构造函数来传递文本(1、2和3)。 The use of that text was to determine if the contents of that form will be disabled (1 - viewing, so everything except the close button is disabled, 2 - add, so everything is enabled & 3- edit, some textboxes are enabled and some are not.) 使用该文本是为了确定该表单的内容是否被禁用(1-查看,因此除关闭按钮之外的所有内容均被禁用,2-添加,因此所有内容均已启用&3-编辑,某些文本框已启用,另一些不是。)

If you're wondering why I went with this, it's because I want to use 1 form only for those 3 functions. 如果您想知道为什么要这样做,那是因为我只想对这3个函数使用一种形式。

My attempt would be to use another constructor to pass the data from the textboxes of frm1 to frm2. 我的尝试是使用另一个构造函数将数据从frm1的文本框传递到frm2。 How would I go about passing 2 different constructors? 我将如何传递两个不同的构造函数?

I have here a slice of the code to give you more info: 我在这里有一段代码可以为您提供更多信息:

Form 1 表格1

public string x = "1", y = "2", z = "3";

public void Viewbtn_Click(object sender, EventArgs e)
{
    Identifier1.Text = x;

    if (Mastercombo.Text == "Suppliers")
    {
        Supplier viewsupp = new Supplier(Identifier1.Text);
        viewsupp.Show();
    }
 }

Form 2 表格2

public Supplier(string identifier)
{
    InitializeComponent();
    identifierlbl.Text = identifier;
}

private void Supplier_Load(object sender, EventArgs e)
{
    if(identifierlbl.Text == "1")
    {
        SuppID.Enabled = false;
        SuppName.Enabled = false;
        SuppTIN.Enabled = false;
    }
    else if(identifierlbl.Text == "3")
    {
        SuppID.Enabled = false;
    }
}

Based on the comments I think what you're asking for is overloading the constructor, eg: 根据评论,我认为您要的是重载构造函数,例如:

public Supplier(string identifier)
{
     InitializeComponent();
     identifierlbl.Text = identifier;
}

public Supplier(string identifier, string identifierTwo)
{
     InitializeComponent();
     identifierlbl.Text = identifier;
}

[How] to pass two or more constructors to another form [如何]将两个或多个构造函数传递给另一种形式

That's probably not describing your needs accurately. 那可能无法准确描述您的需求。 It's reasonable enough to overload the constructor taking additional arguments as needed. 足够重载构造函数并根据需要使用其他参数是足够合理的。 But that leaves you dealing with knowing which overload you're trying to call which usually adds a different problem. 但这使您不必知道要尝试调用的重载,这通常会增加另一个问题。

You could provide a class FormArgs that takes all the data as a nicely packed object. 您可以提供一个FormArgs类, FormArgs将所有数据作为一个包装好的对象。 Then, when it gets to the other form, you just use it as needed. 然后,当它到达另一种形式时,您只需要使用它即可。

public class FormArgs
{
    private string identifier;
    // text boxes or values
    // the rest...
}

Then, 然后,

public Supplier(FormArgs args) {...}

However, that doesn't address binding these forms and data to each other. 但是,这不能解决将这些表单和数据相互绑定的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM