CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2011
    Posts
    20

    How to make a textbox show the text of a clicked button

    Hi! I have just started visual c++ and I am experimenting with a textbox and a bunch of buttons. What I am trying to accomplish is when the user clicks a button the text of the button to be shown in the textbox.

    Just like a virtual keyboard so when button "a" is pressed "a" is shown on the textbox. If then button "b" is pressed the textbox will read "ab" etc.

    So what's the code I should put on the button click event?
    Thanks!

    PS. I am using visual studio 2010 c++ windows forms application

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: How to make a textbox show the text of a clicked button

    In Windows Forms you would use a single Click event handler for all those buttons (see http://www.codeguru.com/forum/showthread.php?t=517485 on how to set that up), and that handler would look somewhat like this:

    Code:
    void Form1::AllButtonsClickHandler(Object ^sender, EventArgs ^e)
    {
      textBox1->Text += safe_cast<Button ^>(sender)->Text;
    }
    BTW, this is not the correct forum section here to post questions about Windows Forms in. The thread I linked to above is in the correct section (C++/CLI). Please post further questions about C++/CLI there.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Nov 2011
    Posts
    20

    Re: How to make a textbox show the text of a clicked button

    Thank you very much! That's exactly the code I was looking for.
    Excuse my luck of knowledge but although I have read your instructions I don't understand how to "attach the handler to the controls" where exactly is the entry field you are talking about?
    Thanks again!

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Arrow Re: How to make a textbox show the text of a clicked button

    To avoid posting even more off-topic stuff in the section here, I've posted my reply to the thread I linked to above, that's already related anyway. Here's the direct link to the reply: http://www.codeguru.com/forum/showthread.php?p=2043268. CU over there...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

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