Results 1 to 11 of 11

Thread: Change Text of Button with Click/Input Box

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    13

    Change Text of Button with Click/Input Box

    Hi all,

    I am not sure if this is the place to post (new to this site), but I am trying to change the text of a button with a click followed by a prompt to enter the new text. Ideally, I would be able to type in the new text right into the button without a popup window, as I am trying to eliminate all extra windows that I can. I am using Visual Studio Express 2013.

    Thank you for any help you can provide in advance!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Change Text of Button with Click/Input Box

    Welcome to VBForums

    Thread moved from the 'Application Deployment' forum (which is for questions about installing/distributing your software) to the 'VB.Net' (VB2002 and later) forum

  3. #3
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Change Text of Button with Click/Input Box

    If you mean entering new text into a button then off hand without creating your own new button class or usercontrol I'd say no.

    To enter text you need to have somewhere to enter it, be that an inline inputbox() statement or a control that accepts text.

    Do you mean enter text into the button you are clicking? How do you imagine that would work? If the click event allows you to change the text how do you expect it to do anything else?
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    13

    Re: Change Text of Button with Click/Input Box

    Quote Originally Posted by Gruff View Post
    If you mean entering new text into a button then off hand without creating your own new button class or usercontrol I'd say no.

    To enter text you need to have somewhere to enter it, be that an inline inputbox() statement or a control that accepts text.

    Do you mean enter text into the button you are clicking? How do you imagine that would work? If the click event allows you to change the text how do you expect it to do anything else?
    Yes, I suppose I am not sure. I guess in my mind it would go:

    -Click button
    -Interface allows you to change text in button, say, from "Food" to "Apple."
    -You click off the button and it accepts the change
    -When you click the button again, it brings you to another form designated to information you would input about "Apple"

    I am just trying to avoid pop-up windows, but can accept if that isn't necessarily possible unless following the initials things you mentioned, like creating my own button class or usercontrol, but I am completely unfamiliar with those. I am almost a complete beginner with Visual Basic and Visual Studio.
    Last edited by BWilliams; Oct 20th, 2014 at 01:00 PM.

  5. #5
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Change Text of Button with Click/Input Box

    One Thought would be to place a textbox on top of your button. Offset it to the left so there is an area to click on the right side of the button. Type anything you want into the textbox at any time. Click on the right side to launch your form. Again this might me best done in a usercontrol where you can wrap both controls into one control.

    Same sort of thing but perhaps use the right mouse button on the button to allow editing of the button text.

    The example that follows would be best used if wrapped up as a user control.

    vb Code:
    1. Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    2.     txtButton1.BackColor = Button1.BackColor
    3.     txtButton1.TextAlign = HorizontalAlignment.Center
    4.     txtButton1.visible = false
    5.   End Sub
    6.  
    7.   Private Sub Button1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
    8.     If e.Button = Windows.Forms.MouseButtons.Right Then
    9.       txtButton1.Text = ""
    10.       Dim lt As Integer = Button1.Left + (Button1.Width \ 2) - (txtButton1.Width \ 2)
    11.       Dim tp As Integer = Button1.Top + (Button1.Height \ 2) - (txtButton1.Height \ 2)
    12.       txtButton1.Location = New Point(lt, tp)
    13.       txtButton1.Visible = True
    14.       txtButton1.Focus()
    15.     End If
    16.   End Sub
    17.  
    18.   Private Sub txtButton1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtButton1.KeyUp
    19.     Select Case e.KeyData
    20.       Case Keys.Escape
    21.         txtButton1.Visible = False
    22.       Case Keys.Return
    23.         Button1.Text = txtButton1.Text
    24.         txtButton1.Visible = False
    25.     End Select
    26.   End Sub
    27.  
    28.   Private Sub txtButton1_LostFocus(sender As Object, e As System.EventArgs) Handles txtButton1.LostFocus
    29.     txtButton1.Visible = False
    30.   End Sub
    Last edited by Gruff; Oct 20th, 2014 at 01:41 PM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    13

    Re: Change Text of Button with Click/Input Box

    Quote Originally Posted by Gruff View Post
    One Thought would be to place a textbox on top of your button. Offset it to the left so there is an area to click on the right side of the button. Type anything you want into the textbox at any time. Click on the right side to launch your form. Again this might me best done in a usercontrol where you can wrap both controls into one control.
    That sounds like a good idea!

  7. #7
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Change Text of Button with Click/Input Box

    New information in my previous post.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    13

    Re: Change Text of Button with Click/Input Box

    Quote Originally Posted by Gruff View Post
    New information in my previous post.
    Thank you!!! I am still getting my feet wet but I appreciate it - I was thinking about doing the right mouse click as well, I will have to pour over your code there and absorb

  9. #9
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Change Text of Button with Click/Input Box

    Glad to help. BTW txtButton1 is a textbox control.

    Added code to the form_load event to make the textbox invisible initially.

    When visible:
    Enter accepts the edit
    Escape cancels the edit
    Clicking any other control than the textbox cancels the edit.
    Last edited by Gruff; Oct 20th, 2014 at 01:43 PM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  10. #10

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    13

    Re: Change Text of Button with Click/Input Box

    It doesn't seem to want to accept txtButton1 for txtButton1.Text...

  11. #11

    Thread Starter
    New Member
    Join Date
    Oct 2014
    Posts
    13

    Re: Change Text of Button with Click/Input Box

    Whoops! Think I got it....told you I was a beginner, haha

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