Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using buttons on a form.

Using buttons on a form.

Scheduled Pinned Locked Moved C#
questioncsharp
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Brian_TheLion
    wrote on last edited by
    #1

    A lot of books deal with the console side of c# more than the form side. Question. I have a form that has a start, yes, no button. When I click on start a question is displayed on the form and the user click on either the yes or the no button. How do I tell the main program what button has been pressed? When I say 'main program' I mean the part of the program that's ran when the start button is pressed. At the start the program YN = 0. If the No button is pressed then YN = 1 and if the Yes button is pressed then YN = 2. What happens next depends on what button is pressed so I need to get this information but to the main program.

    OriginalGriffO G B B 5 Replies Last reply
    0
    • B Brian_TheLion

      A lot of books deal with the console side of c# more than the form side. Question. I have a form that has a start, yes, no button. When I click on start a question is displayed on the form and the user click on either the yes or the no button. How do I tell the main program what button has been pressed? When I say 'main program' I mean the part of the program that's ran when the start button is pressed. At the start the program YN = 0. If the No button is pressed then YN = 1 and if the Yes button is pressed then YN = 2. What happens next depends on what button is pressed so I need to get this information but to the main program.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Brian_TheLion wrote:

      A lot of books deal with the console side of c# more than the form side.

      I'd disagree with that, quite strongly: most "beginner" books start with the Console, because it's a lot easier to get your head round - but quickly move away to WinForms or WPF because that's what gets used in the real world. Certainly, all the Addison Wesley, Wrox, and Microsoft Press books that I've read do! But it's pretty easy - it all works via Events. When you click the button, it generates an Event called "Click" which the Form (or another container like a UserControl perhaps) handles. Open your form in the designer. Double click the button. That will add a Click event handler to the form and open the code window to let you type code into the handler method. Type your code:

              MessageBox.Show("The Yes button was clicked", "My form event handler", MessageBoxButtons.OK, MessageBoxIcon.Information);
      

      And run your app. Click the button, and the message will appear! You code will look like this:

          private void YesButton\_Click(object sender, EventArgs e)
              {
              MessageBox.Show("The Yes button was clicked", "My form event handler", MessageBoxButtons.OK, MessageBoxIcon.Information);
              }
      

      Because the system will add the handler method framework for you:

      private So that it is only usable inside the form.
      void Because it doesn't return a value
      YesButton_Click Name of the handler method: YesButton Click event
      object sender "sender" is object that raised the event (useful if several buttons share the same handler)
      EventArgs e Provides extra information on the event if you need it.

      Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • B Brian_TheLion

        A lot of books deal with the console side of c# more than the form side. Question. I have a form that has a start, yes, no button. When I click on start a question is displayed on the form and the user click on either the yes or the no button. How do I tell the main program what button has been pressed? When I say 'main program' I mean the part of the program that's ran when the start button is pressed. At the start the program YN = 0. If the No button is pressed then YN = 1 and if the Yes button is pressed then YN = 2. What happens next depends on what button is pressed so I need to get this information but to the main program.

        G Offline
        G Offline
        GenJerDan
        wrote on last edited by
        #3

        I think what you're asking is how to get the result of the click back to the main form. When you open the form that has the buttons, use .ShowDialog, rather than just .Show, and the results of the button click (as OrginalGriff showed) can be returned to the calling form easily.

        We won't sit down. We won't shut up. We won't go quietly away. YouTube, VidMe and My Mu[sic], Films and Windows Programs, etc. and FB

        B 1 Reply Last reply
        0
        • B Brian_TheLion

          A lot of books deal with the console side of c# more than the form side. Question. I have a form that has a start, yes, no button. When I click on start a question is displayed on the form and the user click on either the yes or the no button. How do I tell the main program what button has been pressed? When I say 'main program' I mean the part of the program that's ran when the start button is pressed. At the start the program YN = 0. If the No button is pressed then YN = 1 and if the Yes button is pressed then YN = 2. What happens next depends on what button is pressed so I need to get this information but to the main program.

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #4

          First, are you using one Form for all this, or more than one Form ?

          «One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

          1 Reply Last reply
          0
          • G GenJerDan

            I think what you're asking is how to get the result of the click back to the main form. When you open the form that has the buttons, use .ShowDialog, rather than just .Show, and the results of the button click (as OrginalGriff showed) can be returned to the calling form easily.

            We won't sit down. We won't shut up. We won't go quietly away. YouTube, VidMe and My Mu[sic], Films and Windows Programs, etc. and FB

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #5

            From what the OP describes, I cannot tell if he is using a separated Form to display the quesstion.

            «One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

            1 Reply Last reply
            0
            • B Brian_TheLion

              A lot of books deal with the console side of c# more than the form side. Question. I have a form that has a start, yes, no button. When I click on start a question is displayed on the form and the user click on either the yes or the no button. How do I tell the main program what button has been pressed? When I say 'main program' I mean the part of the program that's ran when the start button is pressed. At the start the program YN = 0. If the No button is pressed then YN = 1 and if the Yes button is pressed then YN = 2. What happens next depends on what button is pressed so I need to get this information but to the main program.

              B Offline
              B Offline
              Brian_TheLion
              wrote on last edited by
              #6

              Thanks Bill and RenJerDan for your replies. To go more into detail. Once the user has pressed the start button a d the question is displayed to the user then the user has a choice of clicking on the YES or the NO buttons. For a forms program once the program has displayed the question it is waiting for the user to press a button on the form, but if the user pressed the YES button then control switches to the Yes_Click code which is just YN=1 but then I need to return to the main code to make a decision depending on what button was pressed. It's not like the main code will wait for the user to make a choice before continuing. I tried to create a loop that continues until YN is equal to anything other than zero but while in the loop it is not checking on the buttons on the form, which Makes me wonder how arcade games work as they are looping to provide animation.

              B 1 Reply Last reply
              0
              • B Brian_TheLion

                Thanks Bill and RenJerDan for your replies. To go more into detail. Once the user has pressed the start button a d the question is displayed to the user then the user has a choice of clicking on the YES or the NO buttons. For a forms program once the program has displayed the question it is waiting for the user to press a button on the form, but if the user pressed the YES button then control switches to the Yes_Click code which is just YN=1 but then I need to return to the main code to make a decision depending on what button was pressed. It's not like the main code will wait for the user to make a choice before continuing. I tried to create a loop that continues until YN is equal to anything other than zero but while in the loop it is not checking on the buttons on the form, which Makes me wonder how arcade games work as they are looping to provide animation.

                B Offline
                B Offline
                BillWoodruff
                wrote on last edited by
                #7

                @Brian_TheLion Hi Brian, I still don't know if the question is displayed in its own Form, rather than on a "Main Form." That's important to know in order to assist you. cheers, Bill

                «One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

                B 1 Reply Last reply
                0
                • B BillWoodruff

                  @Brian_TheLion Hi Brian, I still don't know if the question is displayed in its own Form, rather than on a "Main Form." That's important to know in order to assist you. cheers, Bill

                  «One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

                  B Offline
                  B Offline
                  Brian_TheLion
                  wrote on last edited by
                  #8

                  I'm using one form only Bill. Brian

                  1 Reply Last reply
                  0
                  • B Brian_TheLion

                    A lot of books deal with the console side of c# more than the form side. Question. I have a form that has a start, yes, no button. When I click on start a question is displayed on the form and the user click on either the yes or the no button. How do I tell the main program what button has been pressed? When I say 'main program' I mean the part of the program that's ran when the start button is pressed. At the start the program YN = 0. If the No button is pressed then YN = 1 and if the Yes button is pressed then YN = 2. What happens next depends on what button is pressed so I need to get this information but to the main program.

                    B Offline
                    B Offline
                    BillWoodruff
                    wrote on last edited by
                    #9

                    Here's how I would approach this: flow-of-control: a GroupBox is shown with a current question; the user must click one of two radio-buttons to enable a Button that moves to the next question. Next questions are presented until the list of questions is enumerated. 0 a Button that will start the quiz 'btnShowQuiz: visible on the Main Form 1 a GroupBox 'grpbxQuiz: set not visible containing:

                     a TextBox 'tbxQuizQuestion 
                        
                     two RadioButtons 'rbtnYes 'rbtnNo : the 'CheckChanged for both these RadioButtons is set to 'rbtn\_CheckedChanged
                         
                     a Button 'btnNextQuestion
                    

                    // used to reset RadioButton state
                    private bool ignoreRBCheckEvent = true;

                    private List questions = new List
                    {
                    "who", "when", "where", "how"
                    };

                    // holds results
                    private Dictionary QA = new Dictionary();

                    // question counter
                    private int qcnt = 0;

                    // show the quiz
                    private void btnShowQuiz_Click(object sender, EventArgs e)
                    {
                    QA.Clear();
                    qcnt = 0;

                    tbxQuizQuestion.Text = questions\[0\];
                    
                    btnNextQuestion.Enabled = false;
                    
                    rbtnYes.Enabled = true;
                    rbtnYes.CheckedChanged += rbtn\_CheckedChanged;
                    
                    rbtnNo.Enabled = true;
                    rbtnNo.CheckedChanged += rbtn\_CheckedChanged;
                    
                    ignoreRBCheckEvent = false;
                    
                    grpbxQuiz.Visible = true;
                    
                    // set the clip
                    Cursor.Clip = grpbxQuiz.RectangleToScreen(grpbxQuiz.ClientRectangle);
                    

                    }

                    private void BtnNextQuestion_Click(object sender, EventArgs e)
                    {
                    ignoreRBCheckEvent = true;

                    rbtnYes.Checked = false;
                    rbtnNo.Checked = false;
                    
                    btnNextQuestion.Enabled = false;
                    
                    tbxQuizQuestion.Text = questions\[qcnt\];
                    
                    ignoreRBCheckEvent = false;
                    

                    }

                    private void rbtn_CheckedChanged(object sender, EventArgs e)
                    {
                    if (ignoreRBCheckEvent) return;

                    QA.Add(qcnt++, rbtnYes.Checked);
                    
                    if (qcnt < questions.Count)
                    {
                        btnNextQuestion.Enabled = true;
                    }
                    else
                    {
                        // release the clip
                        Cursor.Clip = Rectangle.Empty;
                        grpbxQuiz.Visible = false;
                        OnQuizFinished();
                    
                    }
                    

                    }

                    // inspect the results
                    public void OnQuizFinished()
                    {
                    foreach (var kvp in QA)
                    {
                    Console.WriteLine($"({kvp.Key}) \"{questions[kvp.Key]}\" | {kvp.Value}");
                    }
                    }

                    note the use of clipping the Cursor to restrict mouse input to the GroupBox.

                    «One day it will have to be officially admitted that what we

                    B 1 Reply Last reply
                    0
                    • B BillWoodruff

                      Here's how I would approach this: flow-of-control: a GroupBox is shown with a current question; the user must click one of two radio-buttons to enable a Button that moves to the next question. Next questions are presented until the list of questions is enumerated. 0 a Button that will start the quiz 'btnShowQuiz: visible on the Main Form 1 a GroupBox 'grpbxQuiz: set not visible containing:

                       a TextBox 'tbxQuizQuestion 
                          
                       two RadioButtons 'rbtnYes 'rbtnNo : the 'CheckChanged for both these RadioButtons is set to 'rbtn\_CheckedChanged
                           
                       a Button 'btnNextQuestion
                      

                      // used to reset RadioButton state
                      private bool ignoreRBCheckEvent = true;

                      private List questions = new List
                      {
                      "who", "when", "where", "how"
                      };

                      // holds results
                      private Dictionary QA = new Dictionary();

                      // question counter
                      private int qcnt = 0;

                      // show the quiz
                      private void btnShowQuiz_Click(object sender, EventArgs e)
                      {
                      QA.Clear();
                      qcnt = 0;

                      tbxQuizQuestion.Text = questions\[0\];
                      
                      btnNextQuestion.Enabled = false;
                      
                      rbtnYes.Enabled = true;
                      rbtnYes.CheckedChanged += rbtn\_CheckedChanged;
                      
                      rbtnNo.Enabled = true;
                      rbtnNo.CheckedChanged += rbtn\_CheckedChanged;
                      
                      ignoreRBCheckEvent = false;
                      
                      grpbxQuiz.Visible = true;
                      
                      // set the clip
                      Cursor.Clip = grpbxQuiz.RectangleToScreen(grpbxQuiz.ClientRectangle);
                      

                      }

                      private void BtnNextQuestion_Click(object sender, EventArgs e)
                      {
                      ignoreRBCheckEvent = true;

                      rbtnYes.Checked = false;
                      rbtnNo.Checked = false;
                      
                      btnNextQuestion.Enabled = false;
                      
                      tbxQuizQuestion.Text = questions\[qcnt\];
                      
                      ignoreRBCheckEvent = false;
                      

                      }

                      private void rbtn_CheckedChanged(object sender, EventArgs e)
                      {
                      if (ignoreRBCheckEvent) return;

                      QA.Add(qcnt++, rbtnYes.Checked);
                      
                      if (qcnt < questions.Count)
                      {
                          btnNextQuestion.Enabled = true;
                      }
                      else
                      {
                          // release the clip
                          Cursor.Clip = Rectangle.Empty;
                          grpbxQuiz.Visible = false;
                          OnQuizFinished();
                      
                      }
                      

                      }

                      // inspect the results
                      public void OnQuizFinished()
                      {
                      foreach (var kvp in QA)
                      {
                      Console.WriteLine($"({kvp.Key}) \"{questions[kvp.Key]}\" | {kvp.Value}");
                      }
                      }

                      note the use of clipping the Cursor to restrict mouse input to the GroupBox.

                      «One day it will have to be officially admitted that what we

                      B Offline
                      B Offline
                      Brian_TheLion
                      wrote on last edited by
                      #10

                      Thanks Bill for the example code you provided. I'll study it and try it out. Brian

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups