How to Pragmatically Access Click Event Which Toolstrip Also Defined Programmatically - vb.net

I defined toolstrip & toolstrip buttons within the form cording. Its working fine. I want to know how to access click event each & every button.
When I'm run the form, ToolStrip enable too. But when I click TooStripButtons it didn't access.
Variable Declaration
Dim tsToolStrip As ToolStrip
Dim tsbNew, _
tsbEdit, _
tsbDelete, _
tsbSave, _
tsbPrint, _
tsbCancel As ToolStripButton
Dim tssSepe01, _
tssSepe02, _
tssSepe03 As ToolStripSeparator
Tool Strip Setup
Sub ToolStripSetup()
' ToolStrip Inizializing
tsToolStrip = New ToolStrip
' ToolStrip Seperators Inizializing
tssSepe01 = New ToolStripSeparator
tssSepe02 = New ToolStripSeparator
tssSepe03 = New ToolStripSeparator
' ToolStrip Buttons Inizializing
tsbNew = New ToolStripButton
tsbEdit = New ToolStripButton
tsbDelete = New ToolStripButton
tsbSave = New ToolStripButton
tsbPrint = New ToolStripButton
tsbCancel = New ToolStripButton
With tsbNew
.Name = "tsbNew"
.Image = My.Resources.NewFile_16x
.Text = "&New"
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
.TextImageRelation = TextImageRelation.ImageBeforeText
.Enabled = False
End With
With tsbEdit
.Name = "tsbEdit"
.Image = My.Resources.Edit_16x
.Text = "&Edit"
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
.TextImageRelation = TextImageRelation.ImageBeforeText
.Enabled = False
End With
With tsbDelete
.Name = "tsbDelete"
.Image = My.Resources.DeleteDatabase_16x
.Text = "&Delete"
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
.TextImageRelation = TextImageRelation.ImageBeforeText
.Enabled = False
End With
With tsbSave
.Name = "tsbSave"
.Image = My.Resources.Save_16x
.Text = "&Save"
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
.TextImageRelation = TextImageRelation.ImageBeforeText
.Enabled = False
End With
With tsbPrint
.Name = "tsbPrint"
.Image = My.Resources.Print_16x
.Text = "&Print"
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
.TextImageRelation = TextImageRelation.ImageBeforeText
.Enabled = False
End With
With tsbCancel
.Name = "tsbCancel"
.Image = My.Resources.action_Cancel_16xLG
.Text = "&Cancel"
.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
.TextImageRelation = TextImageRelation.ImageBeforeText
End With
' Adding Buttons To ToolStrip
With tsToolStrip
.Name = "tsToolStrip"
.GripStyle = ToolStripGripStyle.Hidden
.RenderMode = ToolStripRenderMode.System
With .Items
.Add(tsbNew) ' New Button
.Add(tsbEdit) ' Edit Button
.Add(tsbDelete) ' Delete Button
.Add(tssSepe01)
.Add(tsbSave) ' Save Button
.Add(tssSepe02)
.Add(tsbPrint) ' Print Button
.Add(tssSepe03)
.Add(tsbCancel) ' Cancel Button
End With
End With
Controls.Add(tsToolStrip)
End Sub
I above posted my sample code. Can anyone please help me how to do this

Thank For Answer
This is how I done my coding
Dim WithEvents tsbNew, _
tsbEdit, _
tsbDelete, _
tsbSave, _
tsbPrint, _
tsbCancel As ToolStripButton
Private Sub tsbCancel_Click(sender As Object, e As EventArgs) Handles tsbCancel.Click
FormInizalization()
End Sub

Related

Create controls dinamically in a form got by a string value

In my current project I intend to do something like a file explorer. When I click with the right mouse button over the form it opens another form that simulates a MsgBox with 2 buttons, one to create a folder and another to create a text file. The program stores the name of the form that was open when clicked with the mouse and should create the desired option. The problem is that it does not create and does not give errors.
prevForm is a string which contains the previous form name.
Sorry for bad English
Private Sub pasta_Click(sender As Object, e As EventArgs) Handles pasta.Click
Dim nomePasta = InputBox("Nome da Pasta: ", "Nova Pasta", "Nova pasta")
Dim novoForm As New Form With {
.Name = nomePasta,
.Text = nomePasta,
.Size = New Point(816, 489),
.BackColor = Color.FromName("ActiveCaption")
}
Dim novaPicBox As PictureBox
novaPicBox = New PictureBox With {
.Size = New Point(60, 60),
.Location = New Point(720, 21)
}
System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & prevForm).Controls.Add(novaPicBox)
Dim novaLbl As Label
novaLbl = New Label With {
.Text = nomePasta,
.Location = New Point(720, 85)
}
System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & prevForm).Controls.Add(novaLbl)
Me.Close()
End Sub
Private Sub fichtexto_Click(sender As Object, e As EventArgs) Handles fichtexto.Click
Dim nomeFichTexto = InputBox("Nome do Ficheiro de Texto: ", "Novo Ficheiro de Texto", "Novo Ficheiro de Texto")
Dim novaPicBox As PictureBox
novaPicBox = New PictureBox With {
.Size = New Point(60, 60),
.Location = New Point(720, 111),
.Image = Image.FromFile(".\txt.png"),
.SizeMode = PictureBoxSizeMode.StretchImage
}
System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & prevForm).Controls.Add(novaPicBox)
Dim novaLbl As Label
novaLbl = New Label With {
.Text = nomeFichTexto,
.Location = New Point(720, 174)
}
System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & prevForm).Controls.Add(novaLbl)
Me.Close()
End Sub
You keep calling
System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(ProductName & "." & prevForm)
which keeps creating instances. You don't want to create an instance since there is surely already one. You need to find it, or, better yet, just set it when the message is shown.
Add a property to your class which can be set before it's shown, which is the actual previous Form.
Public Class MsgForm
Public Property PreviousForm As Form
Private Sub pasta_Click(sender As Object, e As EventArgs) Handles pasta.Click
Dim nomePasta = InputBox("Nome da Pasta: ", "Nova Pasta", "Nova pasta")
Dim novoForm As New Form With {
.Name = nomePasta,
.Text = nomePasta,
.Size = New Point(816, 489),
.BackColor = Color.FromName("ActiveCaption")
}
Dim novaPicBox = New PictureBox With {
.Size = New Point(60, 60),
.Location = New Point(720, 21)
}
PreviousForm.Controls.Add(novaPicBox)
Dim novaLbl = New Label With {
.Text = nomePasta,
.Location = New Point(720, 85)
}
PreviousForm.Controls.Add(novaLbl)
Me.Close()
End Sub
Private Sub fichtexto_Click(sender As Object, e As EventArgs) Handles fichtexto.Click
Dim nomeFichTexto = InputBox("Nome do Ficheiro de Texto: ", "Novo Ficheiro de Texto", "Novo Ficheiro de Texto")
Dim novaPicBox = New PictureBox With {
.Size = New Point(60, 60),
.Location = New Point(720, 111),
.Image = Image.FromFile(".\txt.png"),
.SizeMode = PictureBoxSizeMode.StretchImage
}
PreviousForm.Controls.Add(novaPicBox)
Dim novaLbl = New Label With {
.Text = nomeFichTexto,
.Location = New Point(720, 174)
}
PreviousForm.Controls.Add(novaLbl)
Me.Close()
End Sub
End Class
And when you call the message box form, set the property
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim m = New MsgForm With {.PreviousForm = Me}
m.Show()
End Sub
End Class

Why is it so difficult to set the location of Splitter?

The content of the codes given below;
3 panels and 2 Splitter are required. However, the 2nd splitter (Green) must be located between the gray and brown panel. But not in the right place. Are there any suggestions?
Thanks.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Show()
Dim tmpSplitter As Splitter
Dim tmpPanel As Panel
Dim MainPanel As Panel
' This is main panel
'
MainPanel = New Panel
With MainPanel
.Name = "MainPanel"
.Dock = DockStyle.Fill
.BackColor = Color.LightGray
.BorderStyle = BorderStyle.Fixed3D
.Visible = True
End With
Me.Controls.Add(MainPanel)
' 1: First Top Panel and Splitter
'
tmpPanel = New Panel
With tmpPanel
.Name = "Panel1"
.Dock = DockStyle.Top
.BackColor = Color.Red
.Visible = True
End With
tmpSplitter = New Splitter
With tmpSplitter
.Name = "Split1"
.Dock = DockStyle.Top
.BackColor = Color.Blue
.Cursor = Cursors.HSplit
End With
Me.Controls.Add(tmpSplitter)
Me.Controls.Add(tmpPanel)
' 2: Second Panel added to the left side of the main panel
'
MainPanel.Dock = DockStyle.Right
MainPanel.Size = New Size(MainPanel.Width * 0.5, MainPanel.Height)
Dim btn As New Button
btn.Size = New Size(10, 50)
btn.Location = New Point(MainPanel.Location.X - btn.Width, MainPanel.Location.Y)
Me.Controls.Add(btn)
tmpPanel = New Panel
With tmpPanel
.Size = New Size(10, MainPanel.Height)
.Location = New Point(MainPanel.Location.X - .Width - 5, MainPanel.Location.Y)
.Name = "Panel2"
.Dock = DockStyle.Fill
.BackColor = Color.Brown
End With
' THIS SPLITTER IS NOT IN THE RIGHT PLACE. Must be between brown and gray panel
'
tmpSplitter = New Splitter
With tmpSplitter
.Size = New Size(5, MainPanel.Height)
.Location = New Point(MainPanel.Location.X - .Width, MainPanel.Location.Y)
.Name = "Split2"
.Dock = DockStyle.Right
.BackColor = Color.Green
.Cursor = Cursors.VSplit
End With
Me.Controls.Add(tmpSplitter)
Me.Controls.Add(tmpPanel)
End Sub
End Class
a little more research and try to solve the solution
Me.Controls.SetChildIndex (tmpSplitter, 0)
is enough to write on the last line. Thank you for your contributions.

VB.NET Docking Form on Panel in Tab Page

how to add a form in a panel inside tab page ?
i use this code to do it, but when i resize my main form, the form inside tab page doesn't auto resize.
Public Sub showForm(ByVal frm As MetroForm)
Dim a As New Panel
For i As Integer = 0 To tabPage.TabCount - 1
If frm.Text = tabPage.TabPages(i).Text Then
tabPage.SelectedIndex = i
Exit Sub
End If
Next
a = New Panel
tabPage.TabPages.Add("")
tabPage.TabPages(tabPage.TabPages.Count - 1).Controls.Add(a)
a.Dock = DockStyle.Fill
frm.TopLevel = False
frm.MinimizeBox = False
frm.MaximizeBox = False
frm.DisplayHeader = False
frm.AutoSize = False
frm.Dock = DockStyle.Fill
tabPage.TabPages(tabPage.TabPages.Count - 1).Text = frm.Text
a.Visible = True
a.Name = "pnl" & frm.Name
a.Controls.Add(frm)
frm.Dock = DockStyle.Fill
frm.Visible = True
frm.BringToFront()
AddHandler a.ControlRemoved, AddressOf Me.removePanel
tabPage.SelectedIndex = tabPage.TabPages.Count - 1
tabPage.TabPages(tabPage.TabPages.Count - 1).Name = frm.Text
End Sub
and to remove the tab page when form is closed :
Private Sub removePanel(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim a As Panel = DirectCast(sender, Panel)
For Each obj As TabPage In tabPage.TabPages
If obj.Name = sender.Parent.Name Then
obj.Controls(0).Dispose() 'remove form
tabPage.TabPages.Remove(obj) 'remove tab page
RemoveHandler a.ControlRemoved, AddressOf Me.removePanel
End If
Next
End Sub
here's the screenshot :

Button created dynamically properties not working vb.net

I'm confused as to why this isn't working...
Here's a brief description of what I've declared my buttons dynamically as the form loads because the form resizes itself depending on whats selected. I've named the buttons button1 and button2. I've added a handler to button2 to handle when the button gets clicked. GetButtonColl is called after the buttons are created and I verified it's populated with the buttons on the form. Why doesn't a simple .enable property work on these dynamically created buttons? Is there something I need to do different?
Here's a condensed version of my code:
Public buttonColl As New List(Of Button)
'---------------------------------------------------------------------------------------------------------------
' Function: GetButtonColl
' Parameters: none
' Returns: none
' Description: Loops through all the controls on the form and searches for buttons that were created after the form has been resized
' and adds them to a List which will be used to quickly control the buttons properties throughout the session
'----------------------------------------------------------------------------------------------------------------
Public Sub GetButtonColl()
For x As Integer = 0 To Me.Controls.Count - 1
Dim b As Control = TryCast(Me.Controls(x), Control)
If b IsNot Nothing AndAlso b.Tag Is Nothing Then
If TypeOf (b) Is Button Then
buttonColl.Add(b)
End If
End If
Next
End Sub
Public Sub button1_Handler(ByVal sender As Object, ByVal e As System.EventArgs)
Try
For Each bttn As Button In buttonColl
If bttn.Name = "button2" Then
bttn.Enabled = False
End If
Next
Catch ex As Exception
End Try
End Sub
Here's how I'm creating my buttons through the code:
btn = New System.Windows.Forms.Button
With btn
.Enabled = true
.Location = New System.Drawing.Point(9, 332)
.Size = New System.Drawing.Size(122, 26)
.Name = "button1"
.Text = "TestButton"
.BackColor = System.Drawing.SystemColors.Control
.Cursor = System.Windows.Forms.Cursors.Default
.Font = New System.Drawing.Font("Arial", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
.ForeColor = System.Drawing.SystemColors.ControlText
.RightToLeft = System.Windows.Forms.RightToLeft.No
.TabIndex = TabIndex
.UseVisualStyleBackColor = False
End With
Me.Controls.Add(btn)
Simplify. The sender object is the button clicked on just cast it back.
Public Sub button1_Handler(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn As Button = DirectCast(sender, Button)
If btn.Name = "button2" Then
btn.Enabled = False
End If
End Sub
btn = New System.Windows.Forms.Button
With btn
.Enabled = true
.Location = New System.Drawing.Point(9, 332)
.Size = New System.Drawing.Size(122, 26)
.Name = "button2" 'wouldn't this be button2?
.Text = "TestButton"
.BackColor = System.Drawing.SystemColors.Control
.Cursor = System.Windows.Forms.Cursors.Default
.Font = New System.Drawing.Font("Arial", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
.ForeColor = System.Drawing.SystemColors.ControlText
.RightToLeft = System.Windows.Forms.RightToLeft.No
.TabIndex = TabIndex
.UseVisualStyleBackColor = False
Addhandler btn.Click, AddressOf button1_Handler 'add the delegate
End With
Me.Controls.Add(btn)
Also add the Button to the List when you create it, if you need it in a List.
Are the buttons directly contained within the form, or are they inside another panel? Looping through me.controls will not get grandchild controls, it isn't recursive.

vb.net disable runtime created buttons

Hey all i have created dynamic buttons at runtime and i would like to disable them when a user clicks on a form button.
This is the code i have for that button:
Dim intXX As Integer = 0
Do Until intXX = intX
userAvatar(intXX).Enabled = False
intXX = intXX + 1
Loop
The buttonNames is an array of all populated button names created at runtime. However, trying the .enabled = false at the end of that does not work. What other ways are there to do that with buttons created at runtime?
How i create the buttons are like this:
private sub createButton()
Dim personAvatar As New PictureBox
With personAvatar
.AutoSize = False '> ^
If intX = 7 Then
thePrviousAvatarImg = 0
End If
If intX <= 6 Then
.Location = New System.Drawing.Point(10 + thePrviousAvatarImg, 10)
ElseIf intX >= 7 And intX <= 14 Then
.Location = New System.Drawing.Point(10 + thePrviousAvatarImg, 150)
Else
Exit Sub
End If
.Name = "cmd" & nameOfPerson
.Size = New System.Drawing.Size(100, 100)
.TabStop = False
.Text = ""
.BorderStyle = BorderStyle.FixedSingle
.BackgroundImageLayout = ImageLayout.Center
.BackColor = Color.LightGray
.BackgroundImage = Image.FromFile(theAvatarDir)
.Tag = nameOfPerson
.BringToFront()
End With
AddHandler personAvatar.Click, AddressOf personAvatar_Click
Me.Controls.Add(personAvatar)
userAvatar(intX) = personAvatar
intX = intX + 1
End With
End Sub
Thanks for your time and help!
David
You can't refer to button objects using a string representation of their names. Instead of using buttonNames (which I assume is an array of strings), use an array of buttons and add each button to that. Then loop through that array (as you've done here) setting enabled = false on each one.
So before you create each picture box, declare an array to store them:
Dim MyPictureBoxes() as PictureBox
Then as you create each one, add them to the array. When you're done creating them, you can disable them like this:
For Each MyPictureBox In MyPictureBoxes
Debug.Print(MyPictureBox.Name)
MyPictureBox.enabled = False
Next
I've created a form with two buttons, cmdGo and cmdDisable, to demonstrate this more completely:
Public Class Form1
Dim MyPictureBoxes(4) As PictureBox
Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
Dim personAvatar As New PictureBox
Dim intX As Integer = 0
While intX < 5
personAvatar = New PictureBox
With personAvatar
.AutoSize = False
.Left = intX * 100
.Top = 100
.Name = "cmd" & intX
.Size = New System.Drawing.Size(100, 100)
.TabStop = False
.Text = ""
.BorderStyle = BorderStyle.FixedSingle
.BackgroundImageLayout = ImageLayout.Center
.BackColor = Color.LightGray
.BringToFront()
End With
Me.Controls.Add(personAvatar)
MyPictureBoxes(intX) = personAvatar
intX = intX + 1
End While
End Sub
Private Sub cmdDisable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisable.Click
For Each MyPictureBox In MyPictureBoxes
Debug.Print(MyPictureBox.Name)
MyPictureBox.Enabled = False
Next
End Sub
End Class
Notice how MyPictureBoxes is scoped for the whole form so it's accessible to both subs.