enter key press

  • Thread starter Thread starter Marc
  • Start date Start date
M

Marc

Hi,


I want my user to be able to rename a button control by selcting rename



from a menu. This then opens a text box in which to enter the new name
in. I want the button control to inherit the name when the enter key is



pressed.


Below is my code so far....all I am missing is the code to accept the
changes on hitting enter. I have tried various things but cannot get it



working. I really want to control everything from within this private
sub.


Any ideas?


Private Sub RenameToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
RenameToolStripMenuItem.Click
Dim NewTxtBox As New TextBox
NewTxtBox.Location = ActiveControl.Location
Me.Controls.Add(NewTxtBox)
NewTxtBox.BringToFront()
NewTxtBox.Text = "Type and Hit Enter"
ActiveControl.Text = NewTxtBox.Text
End If
End Sub


Reply »
 
Try using an input control. It's built into MSVB

I'll use some of your code...

Private Sub RenameToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RenameToolStripMenuItem.Click
Dim s As String = InputBox("Enter a new name", "Rename control")
If s <> "" Then
MyButton.Name = s
End If
End Sub

If you want to try another way let me know! Good luck.
Ryan
--
--
Thiele Enterprises - The Power Is In Your Hands Now!
--
Hi,


I want my user to be able to rename a button control by selcting rename



from a menu. This then opens a text box in which to enter the new name
in. I want the button control to inherit the name when the enter key is



pressed.


Below is my code so far....all I am missing is the code to accept the
changes on hitting enter. I have tried various things but cannot get it



working. I really want to control everything from within this private
sub.


Any ideas?


Private Sub RenameToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
RenameToolStripMenuItem.Click
Dim NewTxtBox As New TextBox
NewTxtBox.Location = ActiveControl.Location
Me.Controls.Add(NewTxtBox)
NewTxtBox.BringToFront()
NewTxtBox.Text = "Type and Hit Enter"
ActiveControl.Text = NewTxtBox.Text
End If
End Sub


Reply »
 
thanks,

actaully thre part i am struggling with is how to hit enter to accept
the changes?
 
well, you will have to go outside of your sub. You have to add handlers for
your textbox.

Private Sub RenameToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RenameToolStripMenuItem.Click
Dim NewTxtBox As New TextBox
NewTxtBox.Location = ActiveControl.Location

'Added code
AddHandler NewTxtBox, AddessOf TxtBickClickEvent

Me.Controls.Add(NewTxtBox)
NewTxtBox.BringToFront()
NewTxtBox.Text = "Type and Hit Enter"
ActiveControl.Text = NewTxtBox.Text
End Sub

Private sub TxtBickClickEvent(Sender as object, e as System.Eventargs)
'Processing from the enter key
End Sub


I would take this approach....
 
great!

i shall try that
Ryan said:
well, you will have to go outside of your sub. You have to add handlers for
your textbox.

Private Sub RenameToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RenameToolStripMenuItem.Click
Dim NewTxtBox As New TextBox
NewTxtBox.Location = ActiveControl.Location

'Added code
AddHandler NewTxtBox, AddessOf TxtBickClickEvent

Me.Controls.Add(NewTxtBox)
NewTxtBox.BringToFront()
NewTxtBox.Text = "Type and Hit Enter"
ActiveControl.Text = NewTxtBox.Text
End Sub

Private sub TxtBickClickEvent(Sender as object, e as System.Eventargs)
'Processing from the enter key
End Sub


I would take this approach....

-----

Public Class AClass
.
.
'Declare the Generic TextBox in the class...
Private WithEvents RenameTextBox as new TextBox

'A Variable that will hold the Name
dim CName as string = ""

'A sub for the clickevent
Private sub RenameTextBox_Click(Sender as object, e as System.Eventargs)
Handles RenameTextBox.Click
'Processing from the enter key
CName = RenameTextBox.Text
End Sub

'Now your code...
Private Sub RenameToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RenameToolStripMenuItem.Click
RenameTextBox = new TextBox
RenameTextBox.Location = ActiveControl.Location

'Added code
AddHandler NewTxtBox, AddessOf TxtBickClickEvent

Me.Controls.Add(RenameTextBox)
NewTxtBox.BringToFront()
NewTxtBox.Text = "Type and Hit Enter"

'When the user clicks enter.. it will rise the RenameTextBox_Click
event

ActiveControl.Text = CName
End Sub
End Class

Give this a try, it's a little lengthy, but it should work.
-----

--
--
Thiele Enterprises - The Power Is In Your Hands Now!
--
thanks,

actaully thre part i am struggling with is how to hit enter to accept
the changes?
 
Hi,

The code below works fine....however i am trying to find a way of
renaming the active button control with the text entered in the
'newtxtbox' ,instead of opening a message box! -see MsgBox("test
message") when enter is clicked).

any ideas?

-----------------------------------------------------------------------------------

Private Sub RenameToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
RenameToolStripMenuItem.Click
Dim NewTxtBox As New TextBox
NewTxtBox.Location = ContextMenuStrip1.SourceControl.Location
Me.Controls.Add(NewTxtBox)
NewTxtBox.BringToFront()
NewTxtBox.Text = "Type and Hit Enter"
AddHandler NewTxtBox.KeyPress, AddressOf TextBox1_KeyPress
End Sub

---------------------------------------------------------------------------------------------------------------------------------

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
If e.KeyChar = Chr(Keys.Enter) Then
MsgBox("test message")
End If
End Sub
 
Place a holder in the class.
Dim MyActiveButton as Button

I would place 2 sub to control the activeness of the control.

Private Sub Buttons_MouseEnter(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyButton.MouseEnter 'Add more if needed
MyActiveButton = DirectCast(Sender,Button)
End Sub

Private Sub Buttons_MouseLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyButton.MouseLeave 'Add more if needed
MyActiveButton = Nothing
End Sub
----------------------------------------------------------------------------------

Private Sub RenameToolStripMenuItem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RenameToolStripMenuItem.Click
Dim NewTxtBox As New TextBox
NewTxtBox.Location = ContextMenuStrip1.SourceControl.Location
Me.Controls.Add(NewTxtBox)
NewTxtBox.BringToFront()
NewTxtBox.Text = "Type and Hit Enter"
AddHandler NewTxtBox.KeyPress, AddressOf TextBox1_KeyPress
End Sub

---------------------------------------------------------------------------------------------------------------------------------

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
If e.KeyChar = Chr(Keys.Enter) Then
If not (MyActiveButton is Nothing) then
MyActiveButton = Name
End If
End If
End Sub
 
Marc,

Why aren't you using the MenuStrips built in textbox? It only took me one
line of code to do what you want to do.
 
Back
Top