CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Dec 2008
    Posts
    9

    Button functions

    Hi Everyone

    Basically I have created a music programme and need a button (Demo) that when pressed displays various notes and text in an edit box. This part I can do. However I need to, if it is possible, to be able to press the button again and this time it would display another set of notes/text. So basically how do you go about giving a button a multipurpose function

    many thanks

    Darren

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Button functions

    Well, you can do it in various ways. The idea is that you should keep track of the state.

    For instance, suppose m_showSet1 is a bool variable, member of the class.
    Code:
    void CYourDialog::OnButton()
    {
       if(m_showSet1)
       {
          // show the first set
       }
       else
       {
          // show the second set
       }
       m_showSet1 = !m_showSet1;
    }
    If you need to show more sets, you can adapt this, using an int instead of bool, or an enum. I hope you figured the idea.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Dec 2008
    Posts
    9

    Re: Button functions

    Quote Originally Posted by cilu View Post
    Well, you can do it in various ways. The idea is that you should keep track of the state.

    For instance, suppose m_showSet1 is a bool variable, member of the class.
    Code:
    void CYourDialog::OnButton()
    {
       if(m_showSet1)
       {
          // show the first set
       }
       else
       {
          // show the second set
       }
       m_showSet1 = !m_showSet1;
    }
    If you need to show more sets, you can adapt this, using an int instead of bool, or an enum. I hope you figured the idea.


    So if I wanted about 4 different options I would use int? would I use just one or would you need 4 of these? Also after the 4 if it was presses a 5th time how would you set it to go back to click 1 ( the first demo )

    thanks

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Button functions

    Well, Why not thinking a little bit for yourself? Think sequences: 0, 1, 2, 3, 4, 0, 1, 2, 3, 4... Does that ring any bells?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Nov 2008
    Posts
    13

    Re: Button functions

    If you want different contents at different clicks, why can't you consider (rand() % UB); Where 'UB' is the upper bound.

  6. #6
    Join Date
    Dec 2008
    Posts
    9

    Re: Button functions

    sort of. I am VERY new to C++. Only been using it for a few weeks, so all is very foreign to me. So any specfic help from anyone would be greatfully received

    Darren

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Button functions

    Quote Originally Posted by cilu View Post
    Well, Why not thinking a little bit for yourself? Think sequences: 0, 1, 2, 3, 4, 0, 1, 2, 3, 4... Does that ring any bells?
    Classic Waltz for a Mayan Indain????

    [The Olmec and Maya civilization used zero as a separate number as early as 1st century BC]
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Nov 2008
    Posts
    13

    Re: Button functions

    Code:
    switch(rand() % 5)
    {
    case 1: //// show the first set
    break;
    
    case 2: //// show the second set
    break;
    
    case 3: //// show the fourth set
    break;
    
    case4: //// show the fourth set
    break;
    
    
    }

  9. #9
    Join Date
    Dec 2008
    Posts
    9

    Re: Button functions

    Quote Originally Posted by unnamed View Post
    Code:
    switch(rand() % 5)
    {
    case 1: //// show the first set
    break;
    
    case 2: //// show the second set
    break;
    
    case 3: //// show the fourth set
    break;
    
    case4: //// show the fourth set
    break;
    
    
    }
    This does make more sense but how does one g oabout making a button have a switch statement?

  10. #10
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Button functions

    The button doesn't have a switch statement. But the button click event has a handler. You put the switch in the handler. Do you know how to create that?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  11. #11
    Join Date
    Dec 2008
    Posts
    9

    Re: Button functions

    Quote Originally Posted by cilu View Post
    The button doesn't have a switch statement. But the button click event has a handler. You put the switch in the handler. Do you know how to create that?
    um no not really...please do explain.....sorruy this must be very annoying...but like I said I am still getting used to it all.

  12. #12
    Join Date
    Nov 2008
    Posts
    13

    Re: Button functions

    Double click on the button and an event handler will be created by the Visual Studio.

  13. #13
    Join Date
    Dec 2008
    Posts
    9

    Re: Button functions

    void CU6b82Dlg::OnDemo()

    {

    switch(rand() % 5)

    {
    case 1: m_Sequence="BBB";
    break;

    case 2: m_Sequence="AAA";
    break;

    case 3: m_Sequence="CDEF";
    break;

    case 4: m_Sequence="abcde";
    break;


    }
    }

    I have done this...However it doesnt work..does the 'switch(rand() % 5)' have to be changed?

  14. #14
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Button functions

    Well, what should happen? What doesn't work? In this function you are setting the value of a variable, m_Sequence? Where is it supposed to be used? You you want to show it's content somewhere? You have to explain that.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  15. #15
    Join Date
    Dec 2008
    Posts
    9

    Re: Button functions

    basically i have 4 edit boxes. When I press demo I want the text i preset in the function to appear. For example clicked once i may wish for ABCD to appear in edit box one (m_Sequence).Then pressed again I may want DACB to appear in edit box one (m_Sequence) and DEFG in edit box two (m_Sequence2)...and so on

    this make sense?

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured