use function keys to locate particular control in vb.net window application - vb.net

my vb.net application i have one toolstrip menu.while clicking tool strip.i am opening four fourms at a time..this all are my child forums.so i given code in
InvolveAllToolStripMenuItem_Click:
Private Sub InvolveAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InvolveAllToolStripMenuItem.Click
AddHandler Me.KeyUp, AddressOf HandleKeyPress
Dim frm As New frmKeyAssignBoard
frm.Location = New Point(625, 0)
frm.MdiParent = Me
AddHandler frm.KeyUp, AddressOf HandleKeyPress
frm.Show()
Dim frm1 As New FrmrecievedDelivaryRequest
frm1.Location = New Point(625, 225)
frm1.MdiParent = Me
AddHandler frm.KeyUp, AddressOf HandleKeyPress
frm1.Show()
Dim frm2 As New FrmDelivary
frm2.Location = New Point(965, 0)
frm2.MdiParent = Me
AddHandler frm.KeyUp, AddressOf HandleKeyPress
frm2.Show()
Dim frm3 As New frmCarCall
frm3.Location = New Point(0, 0)
frm3.MdiParent = Me
AddHandler frm.KeyUp, AddressOf HandleKeyPress
frm3.Show()
End Sub
I want to set some function keys here:
If i click f3, i want to locate cursor to particular textbox inside the frmKeyAssignBoard.
If i click f4, i want to locate cursor to particular textbox inside the FrmrecievedDelivaryRequest.
So where i can write program for this ?

To capture the key press event, normally we handle form's KeyPress or KeyUp event.
however at your case, you got 5 forms open (parent + 4 children). thus i would suggest you write the handler in the parent form, but register all children's keypress event to it, some pseudocode:
Sub ShowChildren()
' Handle own Key up event
AddHandler Me.KeyUp, AddressOf HandleKeyPress
' form 1
Dim frm As New frmKeyAssignBoard()
frm.Location = New Point(625, 0)
frm.MdiParent = Me
AddHandler frm.KeyUp, AddressOf HandleKeyPress
' similar for frm2, frm3, ...
'
End Sub
Private Sub HandleKeyPress(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.F2 Then
' do whatever necessary when F2 is pressed
ElseIf e.KeyCode = Keys.F3 Then
' do whatever necessary when F3 is pressed
' for example, if you want fo focus on frm1's particular control
' but have to expose TxtBox as a public readonly property beforehand
frm1.BringToFront()
frm1.TxtBox.Select()
frm1.TxtBox.Focus()
ElseIf e.KeyCode = Keys.F4 Then
' do whatever necessary when F4 is pressed
Else
'...
End If
End Sub

Related

Would like to add a mouseover handler to dynamically created menu buttons

VB.NET... not C please.
I have made a menu with dynamically created buttons consisting of a total of 18 buttons. I have need to change the background of any button when the mouse hover/mouseover event occurs but am at a loss as to how to add this feature.
My current code is as follows:
Dim btnBilling As New ToolStripButton
With btnBilling
'Set properties
.BackgroundImage = My.Resources.ToolBarBkGrd2
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
.TextImageRelation = TextImageRelation.ImageBeforeText
.Image = My.Resources.Billing
.Font = New Font("Calibri", 8.25, FontStyle.Bold)
.Text = "Billing" & vbNewLine & "Info"
End With
'Create Handle to Click Event
AddHandler btnBilling.Click, AddressOf BtnBilling_Click
'Add button to toolstrip
ToolStrip1.Items.Add(btnBilling)
'Billing Button Events
Private Sub BtnBilling_Click(sender As Object, e As EventArgs)
Bill()
End Sub
Public Sub Bill()
If ActiveMdiChild IsNot Nothing Then ActiveMdiChild.Close()
Billing.MdiParent = Me
Billing.Show()
End Sub
How and where would I add the mouse hover event handler. The only mouse hover event, planned for right now, would be to change the background image to My.Resources.ToolBarBkGrd3
The menu is on a PARENT form that stays visible at all times. Only the current child form closes and loads in the new child form.
This is how I ended up resolving my issue.
Thanks specifically to John for the nudge in the correct direction.
Private BkGrd = resources.GetObject("ToolBarBkGrd2")
Private Bkgrd1 = resources.GetObject("ToolBarBkGrd3")
Dim btnBilling As New ToolStripButton
With btnBilling
'Set properties
.BackgroundImage = CType(BkGrd, Drawing.Image)
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
.TextImageRelation = TextImageRelation.ImageBeforeText
.Image = My.Resources.Billing
.Font = New Font("Calibri", 8.25, FontStyle.Bold)
.Text = "Billing" & vbNewLine & "Info"
End With
'Create handle for MouseEnter Event
AddHandler btnBilling.MouseEnter, AddressOf BtnBilling_MouseEnter
'Create handle for MouseLeave Event
AddHandler btnBilling.MouseLeave, AddressOf BtnBilling_MouseLeave
'Create a Handle to a Click Event
AddHandler btnBilling.Click, AddressOf BtnBilling_Click
'Billing Button Events
Private Sub BtnBilling_MouseEnter(sender As Object, e As EventArgs)
With sender
'Set properties
.BackgroundImage = CType(BkGrd1, Drawing.Image)
End With
End Sub
Private Sub BtnBilling_MouseLeave(sender As Object, e As EventArgs)
With sender
'Set properties
.BackgroundImage = CType(BkGrd, Drawing.Image)
End With
End Sub
Private Sub BtnBilling_Click(sender As Object, e As EventArgs)
Bill()
End Sub
Public Sub Bill()
If ActiveMdiChild IsNot Nothing Then ActiveMdiChild.Close()
Billing.MdiParent = Me
Billing.Show()
End Sub

Refresh a panel when some controls deleted programmaticaly

I am using this part of code to add a Label, a TextBox a Button and a RadioButton into a Panel:
Private Sub AppsAdd_Button_Click(sender As Object, e As EventArgs) Handles AppsAdd_Button.Click
Dim lbl As New Label()
Dim count As Integer = Apps_Panel.Controls.OfType(Of Label)().ToList().Count
lbl.Name = "AppLabel_" & (count + 1)
lbl.Text = "Application #" & (count + 1) & ":"
lbl.Location = New Point(0, 28 * count)
lbl.AutoSize = False
lbl.Size = New Size(114, 21)
lbl.TextAlign = ContentAlignment.MiddleRight
lbl.Tag = count + 1
Apps_Panel.Controls.Add(lbl)
Dim txtbox As New TextBox()
count = Apps_Panel.Controls.OfType(Of TextBox)().ToList().Count
txtbox.Name = "AppTextbox_" & (count + 1)
txtbox.Text = "..."
txtbox.Location = New Point(120, 28 * count)
txtbox.Size = New Size(387, 21)
txtbox.Tag = count + 1
AddHandler txtbox.TextChanged, AddressOf TextBox_Changed
Apps_Panel.Controls.Add(txtbox)
Dim btn As New Button()
count = Apps_Panel.Controls.OfType(Of Button)().ToList().Count
btn.Name = "AppBrowseButton_" & (count + 1)
btn.Text = "Browse"
btn.Location = New Point(513, 28 * count)
btn.Size = New Size(75, 21)
btn.Tag = count + 1
AddHandler btn.Click, AddressOf Button_Click
Apps_Panel.Controls.Add(btn)
Dim radiobtn As New RadioButton()
count = Apps_Panel.Controls.OfType(Of RadioButton)().ToList().Count
radiobtn.Name = "AppRadio_" & (count + 1)
radiobtn.Text = ""
radiobtn.Location = New Point(590, 28 * count)
radiobtn.Size = New Size(14, 21)
radiobtn.Tag = count + 1
AddHandler radiobtn.Click, AddressOf RadioButton_Click
Apps_Panel.Controls.Add(radiobtn)
End Sub
Private Sub TextBox_Changed(sender As Object, e As EventArgs)
Dim textbox As TextBox = TryCast(sender, TextBox)
End Sub
Private Sub Button_Click(sender As Object, e As EventArgs)
Dim button As Button = TryCast(sender, Button)
End Sub
Private Sub RadioButton_Click(sender As Object, e As EventArgs)
Dim RadioButton As RadioButton = TryCast(sender, RadioButton)
AppToDel_Label.Text = RadioButton.Tag
End Sub
Then I use this to remove selected controls:
Private Sub AppsDel_Button_Click(sender As System.Object, e As System.EventArgs) Handles AppsDel_Button.Click
Dim Ctrl As Control
For controlIndex As Integer = Apps_Panel.Controls.Count - 1 To 0 Step -1
Ctrl = Apps_Panel.Controls(controlIndex)
If Ctrl.Name Like "*" & AppToDel_Label.Text Then
RemoveHandler Ctrl.TextChanged, AddressOf TextBox_Changed
RemoveHandler Ctrl.Click, AddressOf Button_Click
RemoveHandler Ctrl.Click, AddressOf RadioButton_Click
Apps_Panel.Controls.Remove(Ctrl)
Ctrl.Dispose()
End If
Next
End Sub
When selected controls are in the bottom of panel, panel is getting smaller in height when I delete them. But if I choose to delete from the top or the middle of panel "list", leaves an empty space and panel don't shrink! Any idea? There is anyway to refresh the panel so it can fill in the empty space?
Here is a video example.
Use a FlowLayoutPanel instead. The FlowLayoutPanel layouts the controls added to it automatically. It can arrange items horizontally or vertically.
You don't specify a location when adding them, instead you work with Margins and Paddings.
I also would combine all controls of one row (Label, TextBox, Button and RadioButton) into a UserControl. This allows you to design the rows visually instead of programmatically.
Walkthrough: Arranging Controls on Windows Forms Using a FlowLayoutPanel.
Working with Windows Forms FlowLayoutPanel (C# Corner).

Access Dynamic Panel on button click event

I have few pannels and button created dynamically now I need to access panel on button click event.
This is how i am creating dynamic button and Pannel
While rst.Read()
Dim panel As New Panel
Dim panel1 As New Panel
Dim mylink As New LinkLabel
Dim btn As New Button
Dim Tooltip As New ToolTip()
Tooltip.SetToolTip(mylink, rst!form_name)
mylink.Name = rst!form_name
AddHandler mylink.Click, AddressOf HandleDynamicLinkLabelClick
AddHandler btn.Click, AddressOf HandleDynamicButtonClick
mylink.Dock = DockStyle.Left
btn.Name = rst!form_name
btn.Tag = rst!form_name
panel1.Tag = rst!form_name
btn.Text = "<"
panel.Controls.Add(mylink)
panel.Controls.Add(btn)
panel1.Name = rst!form_name
panel1.Controls.Add(lbl)
Me.FlowLayoutPanel1.Controls.Add(panel)
Me.FlowLayoutPanel1.Controls.Add(panel1)
End While
rst.Close()
This is the code for dynamic button click event here i need to access pannel1 from the above procedure
Private Sub HandleDynamicButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
End Sub
You need to be able to actively 'know' what the names of your dynamically created controls are so try something like this:
Private Name1 As String
Private Name2 As String
while rst.read
Dim panel As New Panel
Dim panel1 As New Panel
Dim mylink As New LinkLabel
Dim btn As New Button
Dim Tooltip As New ToolTip()
Tooltip.SetToolTip(mylink, rst!form_name)
mylink.Name = rst!form_name
AddHandler mylink.Click, AddressOf HandleDynamicLinkLabelClick
AddHandler btn.Click, AddressOf HandleDynamicButtonClick
mylink.Dock = DockStyle.Left
Name1 = cstr(rst!form_name)
btn.Name = Name1
btn.Tag = rst!form_name
panel1.Tag = rst!form_name
btn.Text = "<"
panel.Controls.Add(mylink)
panel.Controls.Add(btn)
Name2 = Cstr(rst!form_name)
panel1.Name = Name2
panel1.Controls.Add(lbl)
Me.FlowLayoutPanel1.Controls.Add(panel)
Me.FlowLayoutPanel1.Controls.Add(panel1)
end while
Private Sub HandleDynamicButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
Select Case btn.Name
Case Name1
'do something here
End Select
End Sub
You're already storing "rst!form_name" in the Name of the Button:
btn.Name = rst!form_name
So why not store the Panel in the Tag instead?
btn.Tag = panel1
Then you can retrieve it in your handler:
Private Sub HandleDynamicButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
Dim panel1 As Panel = DirectCast(btn.Tag, Panel)
' ... use "panel1" somehow ...
End Sub

VB 2013 How to Convert Working Code to Dynamic

Hello I have a working code for a project i am making...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FolderDlg = FolderBrowserDialog1
'Checks if there is a value to the Selected folder
If FolderDlg.SelectedPath = "" Then
'If no value asks to set one
MsgBox("Please Set a Directory")
FolderDlg.ShowDialog()
If DialogResult.OK Then
Button1.Text = Path.GetFileName(FolderDlg.SelectedPath)
End If
Exit Sub
End If
Process.Start("explorer.exe", FolderDlg.SelectedPath)
End Sub
I want to be able to have "AddNew" Button that creates a a copy of this code. I know how to create dynamic buttons but they all call the same FolderBrowserDialog..
EDIT This is My Add New Button That Creates Buttons With Tags And Also Creates FolderBrowserDialogs with Tags.
Private Sub AddNew_Click(sender As Object, e As EventArgs) Handles AddNew.Click
Dim count As Integer = FloLay.Controls.OfType(Of Button)().ToList().Count
Dim button As New Button()
count = FloLay.Controls.OfType(Of Button)().ToList().Count
'button.Location = New System.Drawing.Point(150, 25 * count)
'button.Size = New System.Drawing.Size(60, 20)
button.Name = "button_" & (count + 1)
button.Text = "Button_ " & (count + 1)
button.Tag = "Button_" & (count + 1)
AddHandler button.Click, AddressOf Set_Dir
Dim FolderDlg As New FolderBrowserDialog
count = FloLay.Controls.OfType(Of FolderBrowserDialog)().ToList().Count
FolderDlg.Tag = "FolderDlg_" & (count + 1)
FloLay.Controls.Add(button)
End Sub
EDIT 3 This is My NEW NEW Event Handler
Private Sub Set_Dir(sender As Object, e As EventArgs)
Dim btn = DirectCast(sender, Button)
If btn.Tag Is Nothing Then
Using fbd As New FolderBrowserDialog
fbd.ShowDialog()
btn.Tag = fbd.SelectedPath
End Using
End If
Dim path = CStr(btn.Tag)
' MsgBox(path)
Process.Start("explorer.exe", path)
End Sub
Now only to Add the Button.Name = GetFileNAme and persistance after app restart or reboot
EDIT 4
Private Sub Set_Dir(sender As Object, e As EventArgs)
Dim btn = DirectCast(sender, Button)
If btn.Tag Is Nothing Then
Using fbd As New FolderBrowserDialog
fbd.ShowDialog()
btn.Tag = fbd.SelectedPath
btn.Text = path.GetFileName(fbd.SelectedPath)
End Using
Exit Sub
End If
Dim Folderpath = CStr(btn.Tag)
' MsgBox(Folderpath)
Process.Start("explorer.exe", Folderpath)
End Sub
Now It Works like a Charm!
You can't create a copy of the code. What you need to do is have the code in the event handler determine what to do based on the Button that was clicked. When you create a Button, you can store some data in its Tag property. In the event handler, the sender parameter is a reference to the object that raised the event, i.e. the Button that was clicked. You can then get that data back from its Tag property and use it. You would use that data to configure the FolderBrowserDialog in whatever manner is appropriate for that Button. E.g.
Creating the Button:
Dim btn As New Button
AddHandler btn.Click, AddressOf ButtonClicked
Me.Controls.Add(btn)
Inside the event handler:
Dim btn = DirectCast(sender, Button)
If btn.Tag Is Nothing Then
Using fbd As New FolderBrowserDialog
fbd.ShowDialog()
btn.Tag = fbd.SelectedPath
End Using
End If
Dim path = CStr(btn.Tag)
'Use path here.
Note that the Tag property is type Object so it can be anything you want. It can be as simple as an Integer or as complex as a DataSet. You simply cast it as the appropriate type in the event handler and then access all the data as normal.

vb.net deleting lots of dynamically created buttons

I'm a new programmer to vb.net, so apologise for what is likely to be ignorance.
I’m building a simple gui for a database interface, with many parent and child items within it. Upon a form I create buttons depending on how many items (parents/children). I've got the creation of the buttons thus:
For RowNumber As Integer = 0 To NoOfRows
Dim Buttoni As New Button
Buttoni.Location = New Point(LocationX, LocationY)
Buttoni.Width = 100
Buttoni.Height = 40
Buttoni.Visible = True
Buttoni.Text = DatasetA.Tables(0).Rows(RowNumber).Item("Name")
ButtonName = "Button" + RowNumber.ToString
If LocationX < FormWidth - (SpacePerButtonX * 2) Then
LocationX = LocationX + SpacePerButtonX
Else
LocationX = 50
LocationY = LocationY + SpacePerButtonY
End If
AddHandler Buttoni.Click, AddressOf DynamicButtonClick
Me.Controls.Add(Buttoni)
Buttoni.BringToFront() 'brings newest buttons to front!
Next
But I’m struggling with a way to delete the buttons to make way for a new set to replace them... I can delete a single one upon its click, but I’d like to delete all of the buttons that have been created in this way before re-creating them.
I hope that makes sense and there is a fairly simple way to accomplish this..?
I will add, in your creation loop, some value to the Tag property.
This will help to differentiate the buttons created dinamically from the buttons created statically in your form.
Buttoni.Tag = 1
Then, to delete a button, loop in reverse order on the Me.Controls collection,
check if you get a button and if the Tag property IsNot Nothing
For x as Integer = Me.Controls.Count - 1 to 0 step -1)
Dim b as Button = TryCast(Me.Controls(x), Button)
If b IsNot Nothing AndAlso b.Tag IsNot Nothing then
b.Dispose() '' NOTE: disposing the button also removes it
End If
Next
It's hard to know exactly what you want to do. I guess you could just use the same technique in reverse, something like
For i As Integer = Me.Controls.Count - 1 To 0 Step -1
Dim ctrl = Me.Controls(i)
If TypeOf (ctrl) Is Button Then
ctrl.Dispose() '' NOTE: disposing the control also removes it
End If
Next
Create a button and delete it with double click!
Easy Code :
Dim b As New Button
Private btn As Button ' this is a reference object
Private ptX, ptY As Integer
Private drag As Boolean
Private Sub nodebtn_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Clicks = 2 Then
b.Dispose()
End If
If e.Button = MouseButtons.Left Then
drag = True
btn = CType(sender, Button)
ptX = e.X : ptY = e.Y
End If
End Sub
Private Sub nodebtn_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If drag Then
btn.Location = New Point(btn.Location.X + e.X - ptX, btn.Location.Y + e.Y - ptY)
Me.Refresh()
End If
End Sub
Private Sub nodebtn_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
drag = False
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
b.Location = New Point(10, 10)
b.Size = New Size(110, 29)
b.BringToFront()
b.Text = "Button"
AddHandler b.MouseDown, AddressOf nodebtn_MouseDown
AddHandler b.MouseMove, AddressOf nodebtn_MouseMove
AddHandler b.MouseUp, AddressOf nodebtn_MouseUp
Me.Controls.Add(b)
End Sub