[RESOLVED] Do While loop for Button
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] Do While loop for Button

  1. #1
    Join Date
    Jan 2013
    Posts
    9

    [RESOLVED] Do While loop for Button

    Hi all,

    I am using visual studio in form mode, where I am using a label and a button. The label is named as X. Now i want the label to change to Y while the button is pressed. When it is released, it will be re-named to X.

    I tried the coding below but it didn't work:

    Code:
     private void button4_Click(object sender, EventArgs e)
            {
    
                do
                {
                    label48.Text = "Y";     //when button pressed, change label to Y
                }
                while (button4 = false);    //repeat till the button is released
    
                label48.Text = "X";       //when released, use the label's original name i.e X
            }
    Can someone please help me solve this problem please ?

    Thanks in advance.

  2. #2
    Join Date
    Oct 2012
    Location
    england
    Posts
    7

    Re: Do While loop for Button

    You could use the MouseDown and MouseUp events.
    MouseDown would be for when the button is pressed, MouseUp would be for when the mouse button is released.

    MouseDown:
    http://msdn.microsoft.com/en-gb/libr...(v=vs.71).aspx

    MouseUp:
    http://msdn.microsoft.com/en-gb/libr...(v=vs.71).aspx

    For example:

    Add these 2 methods.

    public void Button4_MouseUp(object sender, MouseEventArgs e) {
    label48.Text = "X";

    }

    public void Button4_MouseDown(object sender, MouseEventArgs e) {
    label48.Text = "Y";
    }



    And then on the design form, click on the "button4" and select the list of events (the little lightening icon near the properties).
    scroll down to the events "MouseDown" and "MouseUp" and select the relevant methods created.


    Arrokai
    Last edited by arrokai; January 3rd, 2013 at 01:58 PM.

  3. #3
    Join Date
    Jan 2013
    Posts
    9

    Re: Do While loop for Button

    Thanks for the reply. It worked

  4. #4
    Join Date
    Jan 2013
    Posts
    9

    Re: [RESOLVED] Do While loop for Button

    Another question:

    I am using three checkboxes. I set them so that only one checkbox can be true at one time. Below is the coding for this:

    Code:
      
              private void checkBox1_CheckedChanged(object sender, EventArgs e)
                {
                    if (checkBox1.Checked == true)  //if checkBox1 is changed to true, set checkBox2 and 3 to false
                    {
                        checkBox2.Checked = false;
                        checkBox3.Checked = false;
                    }
                }
    Now I need to modify the coding so that the three checkboxes will not be set all false at the same time. A included the coding below but when running, the form is crashing:

    Code:
                private void checkBox1_CheckedChanged(object sender, EventArgs e)
                {
                    if (checkBox1.Checked == true)  //if checkBox1 is changed to true, set checkBox2 and 3 to false
                    {
                        checkBox2.Checked = false;
                        checkBox3.Checked = false;
                    }
    
                    if (checkBox1.Checked == false)
                    {
                        checkBox1.Checked = true;
                    }
                }
    any help of how this can be done please?

  5. #5
    Join Date
    Jan 2013
    Posts
    9

    Re: [RESOLVED] Do While loop for Button

    Ok problem solved. I used radiobuttons instead of checkboxes, therefore eliminating more coding and this problem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center