Jump to content

Visual Basic - Rename Button Without Input Box, Just Click and Type

Go to solution Solved by madknight3,

Looks like you're close. Here's the button/textbox code to help you out.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        // When button is clicked make the textbox over the button visible and set it's focus        TextBox1.Visible = True        TextBox1.Focus()    End Sub    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown        // Fired when a key is pressed. Check that the pressed key is the Enter key        If e.KeyCode = Keys.Enter Then            // Set the button text to what is in the textbox            Button1.Text = TextBox1.Text            // Hide the text box and clear it so it is ready for next time            TextBox1.Visible = False            TextBox1.Clear()        End If    End Sub

Hi all,

I am trying to change the text of a button with a click in Visual Studio Express 2013 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. 

Thank you for any help you can provide in advance!

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

You could have a text box on top of the button with default visibility to false. On button click make it visible and in focus. Design it so the text box isn't distinguishable from the button. On lose focus, enter key, or something, set button text to be same as text box text and hide the text box again.

Link to comment
Share on other sites

Link to post
Share on other sites

You could have a text box on top of the button with default visibility to false. On button click make it visible and in focus. Design it so the text box isn't distinguishable from the button. On lose focus, enter key, or something, set button text to be same as text box text and hide the text box again.

 

That sounds like a great idea!  I am a complete beginner but I will try and fumble around with this

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

Generally a custom control might be the optimal way forward. Alternatively using WPF this kind of thing is very easy to achieve, look at the ControlTemplate for example. Also some other links that may be helpful; here, here and here (talks about deriving a control).

 

If I had more time I would knock up an example of this for you, alas I do not sadly.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Generally a custom control might be the optimal way forward. Alternatively using WPF this kind of thing is very easy to achieve, look at the ControlTemplate for example. Also some other links that may be helpful; here, here and here (talks about deriving a control).

 

If I had more time I would knock up an example of this for you, alas I do not sadly.

 

 

That's ok!  Thank you!!

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks again for your help, you all.  

 

I am actually back at it again after going through several Visual Basic tutorials.  I have the text box set over the button.  I will make the text visible after the button is clicked.  I have the text box text equal to the button text.

 

The only problem is that the text box will not let me enter more than 1 character at a time before it accepts it.

 

How exactly would I code something like this in Visual Basic?:

 

If user presses Enter then

Accept text box string

 

 

This is what I have so far:

 

Private Sub btnClassNameA_Click(sender As Object, e As EventArgs) Handles btnClassNameA.Click
       
        btnTextBox.Visible = True
 
End Sub
 
 
Private Sub btnTextBox_TextChanged(sender As Object, e As EventArgs) Handles btnTextBox.TextChanged
       
        btnClassNameA.Text = btnTextBox.Text
        btnTextBox.Visible = False
 
End Sub

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

Generally a custom control might be the optimal way forward. Alternatively using WPF this kind of thing is very easy to achieve, look at the ControlTemplate for example. Also some other links that may be helpful; here, here and here (talks about deriving a control).

 

If I had more time I would knock up an example of this for you, alas I do not sadly.

Also, I should try and look into WPF now.

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

--snip--

Instead of overriding the TextChanged event use the Leave and KeyPressed events. In Leave you can do what you're already doing. In the KeyPressed event check if the key pressed was enter, if it was then do the same thing but you'll also need to tell the textbox to ignore the keypress by setting e.Handled = True

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Instead of overriding the TextChanged event use the Leave and KeyPressed events. In Leave you can do what you're already doing. In the KeyPressed event check if the key pressed was enter, if it was then do the same thing but you'll also need to tell the textbox to ignore the keypress by setting e.Handled = True

Thank you - I will look into this.  I am unfamiliar with that.

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

Instead of overriding the TextChanged event use the Leave and KeyPressed events. In Leave you can do what you're already doing. In the KeyPressed event check if the key pressed was enter, if it was then do the same thing but you'll also need to tell the textbox to ignore the keypress by setting e.Handled = True

 

I am almost a complete beginner so stop me if it is apparent that I just need to go through more tutorials.

 

However, here is what I have so far:

 

Public Class Form1
 
    Private Sub btnClassNameA_Click(sender As Object, e As EventArgs) Handles btnClassNameA.Click
        btnTextBox.Visible = True
 
    End Sub
   
Private Sub btnClassNameB_Click(sender As Object, e As EventArgs) Handles btnClassNameB.Click
        Dim classNameB As String = Nothing
        classNameB = InputBox("", "Class Name", , , )
        btnClassNameB.Text = classNameB
        Form3.Show()
        Me.Hide()
   
End Sub
   
Private Sub btnClassNameC_Click(sender As Object, e As EventArgs) Handles btnClassNameC.Click
        Dim classNameC As String = Nothing
        classNameC = InputBox("", "Class Name", , , )
        btnClassNameC.Text = classNameC
        Form4.Show()
        Me.Hide()
   
End Sub
 
   
Private Sub btnTextBox_Leave(sender As Object, e As EventArgs) Handles btnTextBox.Leave
        btnClassNameA.Text = btnTextBox.Text
 
   
End Sub
 
 
   
Private Sub btnTextBox_KeyPressed(ByVal o As [Object], ByVal e As KeyPressEventArgs) Handles btnTextBox.KeyPress
       
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            e.Handled = True
       
End If
 
    
End Sub
 
End Class

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

This is all for fun really.  But I am also doing it to learn.  Here is a screenshot of what I have in Designer.  

PlannerB

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

Looks like you're close. Here's the button/textbox code to help you out.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        // When button is clicked make the textbox over the button visible and set it's focus        TextBox1.Visible = True        TextBox1.Focus()    End Sub    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown        // Fired when a key is pressed. Check that the pressed key is the Enter key        If e.KeyCode = Keys.Enter Then            // Set the button text to what is in the textbox            Button1.Text = TextBox1.Text            // Hide the text box and clear it so it is ready for next time            TextBox1.Visible = False            TextBox1.Clear()        End If    End Sub
Link to comment
Share on other sites

Link to post
Share on other sites

 

Looks like you're close. Here's the button/textbox code to help you out.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        // When button is clicked make the textbox over the button visible and set it's focus        TextBox1.Visible = True        TextBox1.Focus()    End Sub    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown        // Fired when a key is pressed. Check that the pressed key is the Enter key        If e.KeyCode = Keys.Enter Then            // Set the button text to what is in the textbox            Button1.Text = TextBox1.Text            // Hide the text box and clear it so it is ready for next time            TextBox1.Visible = False            TextBox1.Clear()        End If    End Sub

Thanks!

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×