c#:Doubt in eventhandler?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ssknov
    New Member
    • Dec 2007
    • 40

    c#:Doubt in eventhandler?

    hi all

    i have two forms FORM1 and FORM2.and form1 has two buttons.

    when buttons B1,B2 is clicked in FORM1, FORM2 should appear,
    but i need to trace which button is clicked .so that i want to display dfferent controls in FORM2 accordin to button click.

    Any body give some tips

    I am trying to trace the "event e" in the FORM2 's load event.

    pls help
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Simply you can declare a flag variable and set the flag to different values .
    ie
    1 for button1
    2 for button2

    you need to compare the value and display the group of controls accordingly in the target form.

    Comment

    • Torgg
      New Member
      • Dec 2007
      • 41

      #3
      Here is how I would handle this.

      On Form1 in button1_Click put the following

      Code:
       Form2 frm2 = new Form2();
      
      frm2.iMode = 1;
      frm2.Show();
      In button2_Click put the following

      Code:
                 Form2 frm2 = new Form2();
           
      frm2.iMode = 2;
      frm2.Show();
      In Form2 put the following
      Code:
          public partial class Form2 : Form
          {
              [B]public int iMode = 0;[/B]
              public Form2()
              {
                  InitializeComponent();
              }
      
              private void Form2_Load(object sender, EventArgs e)
              {
       [B]       if (iMode == 0)
                  {
                      //No value was passed... don't show the form
                      MessageBox.Show("Mode value not set");
                      this.Close();
                  }
                  else if (iMode == 1)
                  {
                      //Show some controls
                  }
                  else if (iMode == 2)
                  {
                      //Show some other controls
                  }[/B]      
             }
          }
      All your doing is setting the iMode value for Form2 depending on which button is pushed. So, if button1 is pushed then the iMode is set to 1 and if button2 is pushed then iMode is set to 2 and if iMode is not set then its defaults to 0 and Form2 will not show. I hope this helps

      Torgg

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by Torgg
        Here is how I would handle this.
        ...
        All your doing is setting the iMode value for Form2 depending on which button is pushed. So, if button1 is pushed then the iMode is set to 1 and if button2 is pushed then iMode is set to 2 and if iMode is not set then its defaults to 0 and Form2 will not show. I hope this helps

        Torgg
        That's a great example Torgg.
        Thanks a lot!

        I just wanted to add to Torgg's recommendation by reminding you that you have the Object sender, and EventArgs e variables at your disposal to. You can use the Object sender parameter to determine what object triggered the event.

        -Frinny

        Comment

        • Torgg
          New Member
          • Dec 2007
          • 41

          #5
          Originally posted by Frinavale
          That's a great example Torgg.
          Thanks a lot!

          I just wanted to add to Torgg's recommendation by reminding you that you have the Object sender, and EventArgs e variables at your disposal to. You can use the Object sender parameter to determine what object triggered the event.

          -Frinny
          Frinny
          Could you give me an example of what you mean? Maybe based on the sample above.

          Torgg

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            This strikes me more as a job for the constructor to me than a job for the Form load event.
            Just pass a different parameter to the constructor of Form2 depending on which button was clicked in Form1.

            Comment

            • ssknov
              New Member
              • Dec 2007
              • 40

              #7
              i am trying

              thank u very much torgg

              Comment

              Working...