0

I have a total of 4 forms(Form1,form2,form3,form4).
3 buttons(Form 1 has 2(Button1,Button2),form 2 has 1(Button3))

My situation is as follows:

Both buttons in Form1 lead to form2 but with different label inputs. What I want to do is when my button 3 is clicked, and if else loop would determine what form the button leads to(form3,form4). If button1 was clicked, button 3 would lead to form3 else button3 would lead to form4.

im not sure where and what part of my code I should include while asking this question so I assume I should just put the button codes.

This is button1 code:

public void DIModuleButton_Click(object sender, System.EventArgs e)
{
    //  MessageBox.Show("TEST");
    alloDI();
    FormSerial frm = new FormSerial();
    frm.MyProperty = ALL;
    frm.Show();
}

public void alloDI()
{
    ALL = "DI";
}

this is button2 code:

public void DOModuleButton_Click(object sender, EventArgs e)
{
    alloDO();
    FormSerial frm = new FormSerial();
    frm.MyProperty = ALL;
    frm.Show();
}

public void alloDO()
{
    ALL = "DO";
}
2
  • you know that you can rename your buttons? in designer select a control, in properties change its name to something meaningful. it doesn't have to be button with numbers Commented Jan 4, 2017 at 9:11
  • @M.kazemAkhgary they all have their own names, i stated them as button1 and button2 in the questions for ease of understanding :) Commented Jan 4, 2017 at 9:14

1 Answer 1

1

A simple solution could be to use a Boolean flag ,since you have only to distinguish between 2 buttons.

Put a new bool property into your second form like you did already with MyProperty.

in Form2:

public bool Button1_pressed { get; set; }

In the Button click event you would set it accordingly:

public void DIModuleButton_Click(object sender, System.EventArgs e)
{
    //  MessageBox.Show("TEST");
    alloDI();
    FormSerial frm = new FormSerial();
    frm.MyProperty = ALL;
    frm.Button1_pressed = true;
    frm.Show();
}

in the second you would set it to false

and when your third button is pressen you can check this variable.

If you have more then 2 buttons, I would suggest to use an enum and a switch case to check the different cases:

The property would look like this:

public ButtonSource MyButton_Clicked { get; set; }

// here your enum
public enum ButtonSource
{
    button1,
    button2,
    button3,
    button4
}

and the switch case:

switch (MyButton_Clicked)
{
    case ButtonSource.button1:
        break;
    case ButtonSource.button2:
        break;
    case ButtonSource.button3:
        break;
    case ButtonSource.button4:
        break;
    default:
        break;
}
Sign up to request clarification or add additional context in comments.

5 Comments

would this still work if i needed to distinguish between 3 buttons instead of 2? button 1 would lead to form 3, button 2 would lead to form 4 and another additional button"button3"(which is in form 1) , lead to an aditional form5.
@MuhammedNizamBinKamarudeen unfortunately not, since bool can only be true of false, for more buttons I have written a different suggestion. Check my edit
ill try this out and get back to you! Thanks alot!
Hi there, im encountering some problems with the case, currently even tho i stated my cases, any button that i pressed form 1 would lead to case 1 only and not case 2 or 3. how do i pm you with my code?
@MuhammedNizamBinKamarudeen You could ask a new question and post your code, and we take a new look onto it. Did you choose the solution with the ENUM ? You could also share your code online and post the link in a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.