Marek
Marek

Reputation: 3575

Open form2 from two buttons (form1) and matching button that was clicked

I have a little issue, I have form1 in which I got button1 and button2 and I got form2 which I'm able to open with both buttons. Button1 serves as opening form2 and inserting details into SQL DB which can be seen in form1 datagridview. Button2 opens the same form2 but it selects data from form1 and automatically is filling them into textboxes in form2 - it is edit-like.

When I created the button2 (edit button) a problem occured, because form2 didn't knew from which button the was opened.

I thought that everytime I open the form2 I should pass integer so when form2 is loaded it should decide from which button it was opened and act according to that.

Would someone help me solve this thing out?

Thanks

Upvotes: 1

Views: 197

Answers (4)

NDJ
NDJ

Reputation: 5194

personally, rather than passing the button or text or a bool I would be explicit and create an enum - pass this to the constructor so you know if you're in editing or display mode. (This covers you if new 'modes' become a requirement) E.g.

 public enum EditingType
    {
        Display,
        Editing
    }

    public class Form2
     {
        private EditingType _editingType;

        public Form2(EditingType editingType)
        {
            _editingType = editingType;
        }

        public void DoSomething()
        {
            if (_editingType == EditingType.Display)
            {
                // display mode
            }

            if (_editingType == EditingType.Editing)
            {
                // editing mode
            }
        }
     }

and to call - Form2 form2 = new Form2(EditingType.Editing); (passing in editing or display depending on which button click you're handling)

Upvotes: 1

Maxime R.C
Maxime R.C

Reputation: 179

You need to change the constructor of your form 2 to open your form in a different "mode"

Just like this :

Form2.cs

    public Form2(bool fromButton2)
    {
        InitializeComponent();
        //Do whatever with that bool
    }

And you open your form like this :

Form1.cs

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2(false);
        frm.Show();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2(true);
        frm.Show();
    }

Then you can apply your logic with the fromButton2 bool

Upvotes: 2

gesus
gesus

Reputation: 471

Define a new constructor in form2 that takes a string, name of the calling button, as a paremeter and from buttons send the name of the button to the form2 as a parameter and in form2 check the name ButtonName pararmeter for detecting the caller button.

Upvotes: 1

icbytes
icbytes

Reputation: 1851

You should create a) a new constructor, which takes button reference ( better name, or whatever You can put into their unused property "Tag" to identify them ) or b ) a public method, which You call before opening the form ( but after instantiating it ) or c) a property in form2, which can take anything You would decide to use as "thing to diffentiate.

Ok ?

Upvotes: 1

Related Questions