How to program a button made in code - vb.net

For example, if on form load Button1 was created, how would I make this button function?

Public WithEvents newButton As Windows.Forms.Button
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim i As Integer
For i = 1 To 5
newButton = New Windows.Forms.Button
newButton.Name = "btnButton" & i
newButton.Text = "Button " & i
newButton.Top = 20 + i * 30
newButton.Left = 40
AddHandler newButton.Click, AddressOf ButtonClicked
Me.Controls.Add(newButton)
Next
End Sub
Private Sub ButtonClicked(ByVal sender As Object, ByVal e As EventArgs)
MsgBox("You clicked: " & sender.name & vbCrLf & "Button name: " & sender.Text)
End Sub
Reference

If you double click the button in the designer view you will be taken to the code-behind for the Button_Click event. There you can add any functionality you want to have happen when the button is clicked by the user.

Related

A problem on right-click menu using Vb.Net

I want to add a right click menu to a dynamic added button by using ContextMenuStr. My code are as follows:
Public Class Form1
Private mybutton As New Button
Private i As Integer
Private mybutton_cms As ContextMenuStrip = New ContextMenuStrip()
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
If e.Node.Text = "addbutton" Then
***Dim mybutton As New Button***
Me.Controls.Add(mybutton)
i = i + 1 'give a ID to the dynamic added button
mybutton.Left = 200
mybutton.Top = 200
mybutton.Name = "mybutton"
AddHandler mybutton.MouseMove, AddressOf mybutton_MouseMove
ElseIf e.Node.Text = "deletebutton" Then
Me.Controls.RemoveByKey("mybutton")
i = i - 1 'delete the the ID of button
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tsm As ToolStripMenuItem = New ToolStripMenuItem("change icon")
mybutton_cms.Items.Add(tsm)
mybutton.ContextMenuStrip = mybutton_cms
End Sub
Private Sub mybutton_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
OBJ_Resize.ResizeOBJ(sender, e, Me)
Dim file As New IO.StreamWriter("data2.xml", OpenMode.Append)
file.WriteLine("width=" & sender.width)
file.WriteLine("height=" & sender.height)
file.WriteLine("x=" & sender.left)
file.WriteLine("y=" & sender.top)
file.Close()
End Sub
Private Sub mybutton_changeicon(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
changeIcon.changeicon(sender, e)
End Sub
End Classa
there is a bug that if I delete the Code referenced with *,namely :
Dim mybutton As New Button
when I run the code, there is no right click menu on the button dynamiclly added.
And if I add this code,there is only one dynamical added button on the form1. But the rigtht click menu will show.
What I want to do is click “addbutton” once to get a button, and each button can have a right-click menu
What's wrong with my code?
Beside,could you plese tell me How to add left-click events to the right menu item?
Thanks!

Create timers dynamically and add unique names VB.net

Alright,
I know how to create buttons and give them each unique names to access them
I do it like this
Dim btnName As String
Dim x As Short
For i As Short = 1 To 3
btnName = "button" & CStr(i)
x += 3
Dim button1 As New Button
button1.Name = btnName
Me.Controls.Add(button1)
button1.Location = New Point(10, x * 10)
button1.Text = "Hello" & i
Next
When I try to create a timer, I cannot give it a name like I did above with the buttons
btnName = "button" & CStr(i)
button1.Name = btnName
So I don't know how to access them and/or activate them for instance. I want to create like three timers and name them like "timer1", "timer2", "timer3"
How do I achieve that?
'Here Is a form code that starts a timer on a button click
Public Class Form1
Dim t1 As Timer
Dim t2 As Timer
Dim t3 As Timer
Private Sub btnT1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnT1.Click
'on btn click start timer1
t1 = New Timer
t1.Tag = DateTime.Now
AddHandler t1.Tick, AddressOf MyTickHandler
t1.Start()
End Sub
Private Sub btnT2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnT2.Click
'on btn click start timer2
t2 = New Timer
t2.Tag = DateTime.Now
AddHandler t2.Tick, AddressOf MyTickHandler
t2.Start()
End Sub
Private Sub btnT2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnT3.Click
'on btn click start timer3
t3 = New Timer
t3.Tag = DateTime.Now
AddHandler t3.Tick, AddressOf MyTickHandler
t3.Start()
End Sub
Sub MyTickHandler(ByVal sender As Object, ByVal e As EventArgs)
dim t As Timer = DirectCast(sender, Timer)
dim timerString = "The timer started at " & t.Tag.ToString & " just ticked..."
End Sub
Private Sub btnStopT1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopT1.Click
'stop timer1
t1.Stop()
t1.Dispose()
End Sub
Private Sub btnStopT2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopT2.Click
'stop timer 2
t2.Stop()
t2.Dispose()
End Sub
Private Sub btnStopT3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopT3.Click
'stop timer 3
t3.Stop()
t3.Dispose()
End Sub
End Class
Timer Class
This tutorial should help you create a timer object.
Tutorial 2: Create a Timed Math Quiz
Tutorial link
In detail step 3 will do the trick.
Step 3: Add a Countdown Timer
Link to step 3
This piece of shows how you can do something with the tick event (copy from the MSDN site)
Private Sub Timer1_Tick() Handles Timer1.Tick
If timeLeft > 0 Then
' Display the new time left
' by updating the Time Left label.
timeLeft -= 1
timeLabel.Text = timeLeft & " seconds"
Else
' If the user ran out of time, stop the timer, show
' a MessageBox, and fill in the answers.
Timer1.Stop()
timeLabel.Text = "Time's up!"
MessageBox.Show("You didn't finish in time.", "Sorry!")
sum.Value = addend1 + addend2
startButton.Enabled = True
End If

vb2010 getting name values from multiple buttons

At the moment, I have a button that sends a value to another form and displays result in a label. The problem is, I have 20 buttons that are labeled a to w that need to be coded and I am stumped as to how I can pass values from multiple buttons. Would it be a case statement in the form being passed to? I am a new user to VB.Net and still finding my way so any help would be gratefully received. I have included code sample for the first button 'A'. Thanks
frmMain
Private Sub btnA_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnA.MouseDown
If (e.Button = MouseButtons.Right) Then
'Dim curButton As Button = DirectCast(sender, Button)
'frmRacks.buttonName = curButton.Name 'Dynamic alternative to frmRacks.buttonName = "A"
frmRacks.buttonName = "A"
frmRacks.Show()
ElseIf (e.Button = MouseButtons.Left) Then
MessageBox.Show("To be coded")
End If
End Sub
frmRacks
Public Class frmRacks
Public buttonName As String
Private Sub racksfrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblRacks.Text = buttonName
End Sub
EDIT: New project
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim button1 As Button = New Button
Dim button2 As Button = New Button
Dim button3 As Button = New Button
With button1
.Name = "button1"
.Left = 0
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button1
End With
With button2
.Name = "button2"
.Left = 100
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button2
End With
With button3
.Name = "button3"
.Left = 200
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button3
End With
Me.Controls.Add(button1)
Me.Controls.Add(button2)
Me.Controls.Add(button3)
End Sub
Private Sub btn_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim curButton As Button = DirectCast(sender, Button)
Dim curButtonName As String = curButton.Name 'This string would change on account of the button you have clicked
Form2.buttonName = curButtonName
Form2.Show()
'MessageBox.Show("You clicked the button called " & curButtonName.ToUpper)
End Sub
End Class
Public Class Form2
Public buttonName As String
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblRacks.Text = buttonName
End Sub
End Class
Here you have a sample code which hopefully will help you to get clearer ideas:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim button1 As Button = New Button
Dim button2 As Button = New Button
Dim button3 As Button = New Button
With button1
.Name = "button1"
.Left = 0
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button1
End With
With button2
.Name = "button2"
.Left = 100
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button2
End With
With button3
.Name = "button3"
.Left = 200
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button3
End With
Me.Controls.Add(button1)
Me.Controls.Add(button2)
Me.Controls.Add(button3)
End Sub
Private Sub btn_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim curButton As Button = DirectCast(sender, Button)
Dim curButtonName As String = curButton.Name 'This string would change on account of the button you have clicked
MessageBox.Show("You clicked the button called " & curButtonName.ToUpper)
End Sub

Dynamic UserControl AddHandler

I am trying to understand why my AddHandler isn't working.
I have found a workaround if buttons always on same form but they may not be in the future.
I am also creating these buttons so I can add several variables for later use
Any have a simple answer for me please?
Thanks
Mark
FORM
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For n = 0 To 3
Dim ctl As New item_button
AddHandler ctl.Click, AddressOf Me.ClickMe
ctl.Name = "btn" & n
ctl.btn.Text = "Button " & n
ctl.btnID = n
ctl.Location = New Point(10, n * 50)
Me.Controls.Add(ctl)
Next
End Sub
Public Sub ClickMe(ByVal s As Object, ByVal e As EventArgs)
'do something
Dim btn As item_button
btn = CType(s, item_button)
TextBox1.Text = "Button " & s.btnID & " was pressed"
End Sub
End Class
ITEM_BUTTON
Public Class item_button
Public btnID As Integer
Public btnColor As System.Drawing.Color
Public Function ClickIt() As Integer
Return btnID
End Function
End Class
Your "Button" does not inherit from Button:
Public Class ItemButton ' Naming-Conventions: http://msdn.microsoft.com/en-us/library/ms229040(v=vs.110).aspx
Inherits Button
Public Property BtnID As Integer
Public Property BtnColor As System.Drawing.Color
Public Function ClickIt() As Integer
Return btnID
End Function
End Class
Since i'm not sure what you're actually trying to achieve i show you an example with a custom event that is raised in the custom button and handled in the form:
Public Class ItemButton
Inherits Button
Public Property BtnID As Integer
Public Property BtnColor As System.Drawing.Color
Public Event ButtonClicked(sender As ItemButton, buttonID As Int32)
Private Sub clicked(sender As Object, e As EventArgs) Handles Me.Click
RaiseEvent ButtonClicked(Me, BtnID)
End Sub
End Class
in the form:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For n = 0 To 3
Dim ctl As New ItemButton
AddHandler ctl.ButtonClicked, AddressOf Me.ItemButtonClicked
ctl.Name = "btn" & n
ctl.Name = "Button " & n.ToString()
ctl.btnID = n
ctl.Location = New Point(10, n * 50)
Me.Controls.Add(ctl)
Next
End Sub
Public Sub ItemButtonClicked(ByVal btn As ItemButton, ByVal buttonID As Int32)
TextBox1.Text = "Button " & buttonID & " was pressed"
End Sub
Sorted
Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
RaiseEvent ButtonClicked(Me, btnID)
End Sub
Thanks Tim, your code helped

How to programmatically add controls to a form in VB.NET

I am working on an inventory in Visual Basic 2010 Express Edition. I don't know the number of fields that will be necessary for the inventory. My hope was that I could add textboxes/checkboxes/buttons using for loops in the program. Is there a way to add controls to a form without using the toolbox?
Can I add controls by instantiating them in the program?
Yes.
Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyTextbox as New Textbox
With MyTextbox
.Size = New Size(100,20)
.Location = New Point(20,20)
End With
AddHandler MyTextbox.TextChanged, AddressOf MyTextbox_Changed
Me.Controls.Add(MyTextbox)
'Without a help environment for an intelli sense substitution
'the address name and the methods name
'cannot be wrote in exchange for each other.
'Until an equality operation is prior for an exchange i have to work
'on an as is base substituted.
End Sub
Friend Sub MyTextbox_Changed(sender as Object, e as EventArgs)
'Write code here.
End Sub
Dim numberOfButtons As Integer
Dim buttons() as Button
Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Redim buttons(numberOfbuttons)
for counter as integer = 0 to numberOfbuttons
With buttons(counter)
.Size = (10, 10)
.Visible = False
.Location = (55, 33 + counter*13)
.Text = "Button "+(counter+1).ToString ' or some name from an array you pass from main
'any other property
End With
'
next
End Sub
If you want to check which of the textboxes have information, or which radio button was clicked, you can iterate through a loop in an OK button.
If you want to be able to click individual array items and have them respond to events, add in the Form_load loop the following:
AddHandler buttons(counter).Clicked AddressOf All_Buttons_Clicked
then create
Private Sub All_Buttons_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
'some code here, can check to see which checkbox was changed, which button was clicked, by number or text
End Sub
when you call: objectYouCall.numberOfButtons = initial_value_from_main_program
response_yes_or_no_or_other = objectYouCall.ShowDialog()
For radio buttons, textboxes, same story, different ending.
Public Class Form1
Private boxes(5) As TextBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim newbox As TextBox
For i As Integer = 1 To 5 'Create a new textbox and set its properties26.27.
newbox = New TextBox
newbox.Size = New Drawing.Size(100, 20)
newbox.Location = New Point(10, 10 + 25 * (i - 1))
newbox.Name = "TextBox" & i
newbox.Text = newbox.Name 'Connect it to a handler, save a reference to the array & add it to the form control.
AddHandler newbox.TextChanged, AddressOf TextBox_TextChanged
boxes(i) = newbox
Me.Controls.Add(newbox)
Next
End Sub
Private Sub TextBox_TextChanged(sender As System.Object, e As System.EventArgs)
'When you modify the contents of any textbox, the name of that textbox
'and its current contents will be displayed in the title bar
Dim box As TextBox = DirectCast(sender, TextBox)
Me.Text = box.Name & ": " & box.Text
End Sub
End Class
To add controls dynamically to the form, do the following code. Here we are creating textbox controls to add dynamically.
Public Class Form1
Private m_TextBoxes() As TextBox = {}
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
' Get the index for the new control.
Dim i As Integer = m_TextBoxes.Length
' Make room.
ReDim Preserve m_TextBoxes(i)
' Create and initialize the control.
m_TextBoxes(i) = New TextBox
With m_TextBoxes(i)
.Name = "TextBox" & i.ToString()
If m_TextBoxes.Length < 2 Then
' Position the first one.
.SetBounds(8, 8, 100, 20)
Else
' Position subsequent controls.
.Left = m_TextBoxes(i - 1).Left
.Top = m_TextBoxes(i - 1).Top + m_TextBoxes(i - _
1).Height + 4
.Size = m_TextBoxes(i - 1).Size
End If
' Save the control's index in the Tag property.
' (Or you can get this from the Name.)
.Tag = i
End With
' Give the control an event handler.
AddHandler m_TextBoxes(i).TextChanged, AddressOf TextBox_TextChanged
' Add the control to the form.
Me.Controls.Add(m_TextBoxes(i))
End Sub
'When you enter text in one of the TextBoxes, the TextBox_TextChanged event
'handler displays the control's name and its current text.
Private Sub TextBox_TextChanged(ByVal sender As _
System.Object, ByVal e As System.EventArgs)
' Display the current text.
Dim txt As TextBox = DirectCast(sender, TextBox)
Debug.WriteLine(txt.Name & ": [" & txt.Text & "]")
End Sub
End Class
You can get your Button1 location and than increase the Y value every time you click on it.
Public Class Form1
Dim BtnCoordinate As Point
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim btn As Button = New Button
BtnCoordinate.Y += Button1.Location.Y + 4
With btn
.Location = New Point(BtnCoordinate)
.Text = TextBox1.Text
.ForeColor = Color.Black
End With
Me.Controls.Add(btn)
Me.StartPosition = FormStartPosition.CenterScreen
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1Coordinate = Button1.Location
End Sub
End Class