0

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). 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.)

If you're wondering why I went with this, it's because I want to use 1 form only for those 3 functions.

My attempt would be to use another constructor to pass the data from the textboxes of frm1 to 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

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

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;
    }
}
ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
  • *How would I go about passing 2 different constructors?* you can have an overloaded constructor (ie two methods with the same name with different arguments in the parameter). – Jeremy Thompson May 30 '18 at 01:49
  • @JeremyThompson How do I go about passing my identifier1.text and textboxes at the same time? Every help I searched only shows 1 data in transferring. –  May 30 '18 at 02:10
  • @J.Avery you can create as many constructor as you want, and passing different values while creating object (here form) appropriate constructor will be called. That is called Constructor Overloading. – Amit May 30 '18 at 02:23
  • This sounds like you need to do more reading to understand what constructors are and how to use them, and to at least use the right terminology https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors – TheGeneral May 30 '18 at 02:36

2 Answers2

0

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;
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • is there any way, to call default constructor form overloaded constructor ? :D so your two lines `InitializeComponent();` and `identifierlbl.Text = identifier;` will be at one place only. – Amit May 30 '18 at 02:24
  • @Amit Yes, the feature you're looking for is "constructor chaining," see https://stackoverflow.com/questions/1814953/c-sharp-constructor-chaining-how-to-do-it – Preston Guillot May 30 '18 at 02:31
  • You mean like this, Supplier(string identifier, string identifierTwo) : Supplier(identifier) { ........... } – Swamy May 30 '18 at 02:32
  • @PrestonGuillot I know that feature, that comment only meant to add that too in answer. it will be fun for OP to know that – Amit May 30 '18 at 02:32
  • @PrestonGuillot Your suggestions are very appreciated. I've been stuck in this thing since yesterday. I will try it later. :) –  May 30 '18 at 02:57
0

[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. 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.

ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
  • This will work in my project if there aren't any if else statements involve IMO. But I'll definitely consider this in other functions. Thanks for the insight! –  May 30 '18 at 05:25