Creating buttons in runtime from Database field - vb.net

I have a single columns table which holds 50 records. I would like to create 50 buttons from those records. How can I do that in Vb.Net?
Any help will be appreciated.

Assuming you mean Winforms:
Use your Datasource(f.e. a DataTable) and loop its RowCollection. For example:
Private Sub BtnLoadButtons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLoadButtons.Click
Dim source As New DataTable("MyButtonTable")
source.Columns.Add(New DataColumn("MyButtonColumn", GetType(String)))
For i As Int32 = 1 To 50
Dim newRow As DataRow = source.NewRow
newRow("MyButtonColumn") = "Button_" & i
source.Rows.Add(newRow)
Next
'you are loading the above DataTable from SQL-Server, now iterate the rows...'
For Each row As DataRow In source.Rows
Dim btn As New Button()
btn.Name = DirectCast(row("MyButtonColumn"), String)
btn.Text = btn.Name
btn.Location = New Point(0, Me.Panel1.Controls.Count * btn.Height)
AddHandler btn.Click, AddressOf handleButton
Me.Panel1.Controls.Add(btn)
Next
End Sub
Private Sub handleButton(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
'do something ...'
End Sub

If you are working in a Winforms application you can use the FlowLayoutPanel because it automatically orders the button layouts for you:
dim i as integer=1
for each record in Table
dim btn as new Button
btn.id = "btn" & i
i+=1
Panel1.Controls.add(btn)
next

Related

VB.NET get control name from button created at run time

I have this code to create 3 buttons at run time, which seems to be working ok.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim rawData As String
Dim FILE_NAME As String = "C:\temp\test.txt"
Dim data() As String
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
rawData = objReader.ReadLine() ' & vbNewLine
data = Split(rawData, ",")
'data 0 = X loc, data 1 = Y loc, data 2 = Part Num, data 3 = Reference Des
Dim dynamicButton As New Button
dynamicButton.Location = New Point(data(0), data(1))
dynamicButton.Height = 20
dynamicButton.Width = 20
dynamicButton.FlatStyle = FlatStyle.Flat
dynamicButton.BackColor = Color.Transparent
dynamicButton.ForeColor = Color.FromArgb(10, Color.Transparent)
'dynamicButton.Text = "+"
dynamicButton.Name = data(2)
dynamicButton.FlatAppearance.BorderColor = Color.White
'dynamicButton.Font = New Font("Georgia", 6)
AddHandler dynamicButton.Click, AddressOf DynamicButton_Click
Controls.Add(dynamicButton)
Dim myToolTipText = data(3)
ToolTip1.SetToolTip(dynamicButton, myToolTipText)
Loop
End Sub
I want to get the control name that was created when I click a button, but I cannot seem to get what I need. I have been doing this on the dynamicButton_click event.
Private Sub DynamicButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
For Each cntrl In Me.Controls
If TypeOf cntrl Is Button Then
MsgBox("")
Exit Sub
End If
Next
End Sub
All you need to do is cast the sender variable to button. It will tell you which control was the source of the event:
Private Sub DynamicButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn As Button = DirectCast(sender, Button)
MessageBox.Show("You clicked: " & btn.Name)
End Sub
You attached an event handler to the dynamic button and that is hitting?
Couple ways you can do this:
This will give you the button you just clicked without needing to loop, the sender is the button. You need to cast it from the object to button:
MessageBox.Show(DirectCast(sender, Button).Name)
Otherwise, inside your loop, cast cntrl (the generic Control object) to the specific Button control object and then get the name.
Dim btn as Button = DirectCast(cntrl, Button)
MessageBox.Show(btn.Name)

How do I add buttons with event handlers to a form dynamically?

Is it possible to do this dynamically? How?
Code:
Public Class Form1
Dim c(40) As Integer
Dim IMG As PictureBox
Dim lbl_n(40) As Label
Dim lbl_pr(40) As Label
Dim lbl_ref(40) As Label
Dim lbl_dim(40) As Label
Dim lbl_col(40) As Label
Dim btn_add(40) As Button
Dim btn_rmv(40) As Button
Dim tb_qt(40) As TextBox
AddHandler btn_add(0).Click, AddressOf btn_add0_Click
AddHandler btn_add(1).Click, AddressOf btn_add1_Click
[...]
AddHandler btn_add(40).Click, AddressOf btn_add40_Click
Public Sub btn_add0_Click(ByVal sender As Object, ByVal e As System.EventArgs)
c(0) = c(0) + 1
tb_qt(0).Text = c(0)
End Sub
Public Sub btn_add1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
c(1) = c(1) + 1
tb_qt(1).Text = c(1)
End Sub
[...]
Public Sub btn_add40_Click(ByVal sender As Object, ByVal e As System.EventArgs)
c(40) = c(40) + 1
tb_qt(40).Text = c(40)
End Sub
These are the images with program running (they are edited):
Form1
Form2
I want to dynamically, because I could use more than 40 products! And I need to do 40 addhandlers, so that more 40 to remove!
How can I do that?
Yes, You can do that dynamically.
In this example, I put those buttons and textboxes into Panel.
There is example with comments which line is for what :
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'dynamically adding buttons (so You don't need create every button separately)
For x = 1 To 40
Dim btn As New Button 'create new button
btn.Name = "btn_add" & x.ToString 'set ID for button (example: btn_add1, btn_add2, ... btn_add40)
btn.Text = "+" 'set button text
btn.Tag = x.ToString 'set button NO. for finding corrent textbox whos belong to this button (for example: tb_qt1 belong to buttons btn_add1 and btn_rem1)
btn.Width = 24 'this is for styling
btn.Top = (x * btn.Height) + 3 'for styling, too. Top poistion of button
btn.Left = 0 'for styling, too. Left position of button
AddHandler btn.Click, AddressOf btnAdd_Click 'creating sub called btnAdd_Click (this sub will handle all, in this case 40, buttons)
Panel1.Controls.Add(btn) 'for this example I put buttons and textboxes into panel (autoscroll set to True)
btn = New Button 'same thing just for remove button
btn.Name = "btn_rem" & x.ToString
btn.Text = "-"
btn.Tag = x.ToString
btn.Width = 24
btn.Top = (x * btn.Height) + 3
btn.Left = btn.Width + 3
AddHandler btn.Click, AddressOf btnRem_Click 'creating sub called btnRem_Click (this sub will handle all, in this case 40, buttons)
Panel1.Controls.Add(btn)
Dim txt As New TextBox 'same thing for textboxes
txt.Name = "tb_qt" & x.ToString
txt.Text = "0"
txt.Tag = x.ToString
txt.Top = (x * btn.Height) + 3
txt.Left = btn.Left + btn.Width + 3
txt.TextAlign = HorizontalAlignment.Right
Panel1.Controls.Add(txt)
Next
End Sub
Public Sub btnAdd_Click(sender As Object, e As EventArgs)
Dim btn As Button = DirectCast(sender, Button) 'detect which button clicked. You can add line: MsgBox(btn.Name) to see what happening
Dim txt As TextBox = Panel1.Controls.Find("tb_qt" & btn.Tag.ToString, True)(0) 'find textbox which belong to this button. this is why set .Tag into buttons
txt.Text = CInt(txt.Text) + 1 'just do math and change value, by adding one(1), in textbox (by this way I avoid using Your Dim c(40) As Integer)
End Sub
Public Sub btnRem_Click(sender As Object, e As EventArgs)
Dim btn As Button = DirectCast(sender, Button) 'same thing like for btnAdd_Click, but we doing subtract for one(1) value in textbox
Dim txt As TextBox = Panel1.Controls.Find("tb_qt" & btn.Tag.ToString, True)(0)
txt.Text = CInt(txt.Text) - 1
End Sub
Because I use panel like container for all those buttons and textboxes, You have to set AutoScroll=True for panel.
I hope so You will understand how it's work for start.

How to catch cellmouseclicks in dynamically created datagridviews on TabPages

Sub Tabs()
Dim shtWork1 As Short
Dim dbpath As String = "db\... "
Dim conn As New OleDbConnection(dbpath ... )
For shtWork1 = 1 To shtMaxTabs
Dim TabPageAll As New TabPage
Try
'define DataGridViews
Dim DataGridView1 As New DataGridView
DataGridView1.Parent = TabPageAll
.
.
.
DataGridView1.AutoGenerateColumns = True
TabControl1.TabPages.Add(TabPageAll)
Catch ex as ...
Message
Finally
Close()
End Try
Next shtWork1
End Sub
' The Grids are created on the TabPages and work fine.
But if I click on a cell it should give me the possibility to execute some other code i.e.
filling textboxes on that specific TabPage.
Any idea would be apreciated.
TIA Frank
Just add an event handler when adding your DataGridView
AddHandler DataGridView1.CellClick, AddressOf cellClickHandler
This is an example handler code
Private Sub cellClickHandler(sender As Object, e As DataGridViewCellEventArgs)
MessageBox.Show(String.Format("Row '{0}', Col '{1}'", e.RowIndex, e.ColumnIndex))
End Sub
Edit:
Complete solution
Add a tab control to a new winforms app. Just add this code to Form1 class.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim tabPageAll As New TabPage()
Dim dataGridView1 As New DataGridView()
Dim d As New Dictionary(Of String, String)() _
From {{"A", "1"}, {"B", "2"}, {"C", "3"}}
dataGridView1.AutoGenerateColumns = True
dataGridView1.DataSource = _
(From ds In d
Select New With {.Key = ds.Key, .Value = ds.Value}).ToArray()
AddHandler dataGridView1.CellClick, AddressOf cellClickHandler
tabPageAll.Controls.Add(dataGridView1)
TabControl1.TabPages.Add(tabPageAll)
TabControl1.SelectedTab = tabPageAll
End Sub
Private Sub cellClickHandler(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs)
If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
' DataGridView.Rows() works just fine here
Dim selectedRow = CType(sender, DataGridView).Rows(e.RowIndex)
End If
End Sub

Dynamic TableLayout and Control

created dynamic table layout and add some controls in to that table layout.my code
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
dynamicTable.ColumnCount = 5
dynamicTable.RowCount = 1
For i = 1 To 5
Dim button= New button()
button.Text = i.ToString()
dynamicTable.SetColumn(button, i)
dynamicTable.Controls.Add(button)
Next
Next
End Sub
Now i added 5 buttons to the table layout.
Now i am going to click on the button. How can i know ,on which button i clicked ?
Try like this
Dim dynamicTable As New TableLayoutPanel
dynamicTable.ColumnCount = 5
dynamicTable.RowCount = 1
For i = 1 To 5
Dim button = New Button()
button.Name = "button" & i
AddHandler button.Click, AddressOf Click1
Button.Text = i.ToString()
dynamicTable.SetColumn(button, i)
dynamicTable.Controls.Add(button)
Next
Me.Controls.Add(dynamicTable)
Private Sub Click1(ByVal sender As System.Object, ByVal e As System.EventArgs)
If sender.Name = "button1" Then
MsgBox("Hi")
End If
End Sub

Identify source chart/control from ContextMenuStrip Click

I have a form with numerous charts and have added a ContextMenuStrip when a chart is right clicked so the user can copy the chart image to the clipboard
Public Sub Chart_Click(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseClick, _
Chart2.MouseClick, Chart3.MouseClick, Chart4.MouseClick
If e.Button = MouseButtons.Right Then
Dim cmus As ContextMenuStrip = New ContextMenuStrip
Dim cms1 As ToolStripMenuItem = New ToolStripMenuItem("Copy as Image")
cms1.Tag = 0
cmus.Items.Add(cms1)
For Each c As ToolStripMenuItem In cmus.Items
AddHandler c.Click, AddressOf Chart_cMenu_Click
Next
cmus.Show(New Point(Control.MousePosition.X, Control.MousePosition.Y))
End If
End Sub
Then I would like to identify which chart was clicked on so that I can copy that chart to the clipboard.
This seems like a simple problem to me but I cannot figure out why no matter what I do trying to identify the Owner,Parent,SourceControl of the right click menu always returns me a Nothing value.
Public Sub Chart_cMenu_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim menuItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim cms As ContextMenuStrip = CType(menuItem.Owner, ContextMenuStrip)
Dim _owner As Control = CType(cms.SourceControl, Chart)
Select Case menuItem.Text
Case "Copy as Image"
Dim ms As New System.IO.MemoryStream(100)
_owner.SaveImage(ms, ChartImageFormat.Bmp)
Dim bm As Bitmap = New Bitmap(ms)
Clipboard.SetImage(bm)
End Select
End Sub
Any ideas how to identify the source chart?
Have a messy solution. Going to leave this open in case a more elegant solution can be formed.
Add a name to your new ContextMenuStrip that reflects the Sender. This can then be used in the MouseClick method to find the origin
Public Sub Chart_Click(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseClick, _
Chart2.MouseClick, Chart3.MouseClick, Chart4.MouseClick
If e.Button = MouseButtons.Right Then
Dim cmus As ContextMenuStrip = New ContextMenuStrip
'Add the name to menu
cmus.Name = sender.Name & "_CMS"
Dim cms1 As ToolStripMenuItem = New ToolStripMenuItem("Copy as Image")
cms1.Tag = 0
cmus.Items.Add(cms1)
For Each c As ToolStripMenuItem In cmus.Items
AddHandler c.Click, AddressOf Chart_cMenu_Click
Next
cmus.Show(New Point(Control.MousePosition.X, Control.MousePosition.Y))
End If
End Sub
Now find the control that corresponds to the new name
Public Sub Chart_cMenu_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim cms As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim _owner As ContextMenuStrip = CType(cms.Owner, ContextMenuStrip)
'This is where you use the name attached to the ContextMenuStrip
Dim _chartname As String = Replace(_owner.Name, "_CMS", "")
Dim parentObject As Chart = Nothing
Try
parentObject = CType(Me.Controls.Find(_chartname, True)(0), Chart)
Catch ex As Exception
End Try
If Not parentObject Is Nothing Then
Select Case cms.Text
Case "Copy as Image"
Dim ms As New System.IO.MemoryStream(100)
parentObject.SaveImage(ms, ChartImageFormat.Bmp)
Dim bm As Bitmap = New Bitmap(ms)
Clipboard.SetImage(bm)
End Select
End If
End Sub
Still think there should be an easier solution than this using the likes of Parent,Owner and SourceControl
My VB is rusty, but the below code works fine for me.
Dim menu As New ContextMenuStrip()
menu.ItemClicked += New ToolStripItemClickedEventHandler(menu_ItemClicked)
menu.Items.Add("Save As Image")
chart1.ContextMenuStrip = menu
chart2.ContextMenuStrip = menu;
Private Sub menu_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs)
If e.ClickedItem.ToString() = "Save As Image" Then
Dim menu As ContextMenuStrip = TryCast(sender, ContextMenuStrip)
If menu IsNot Nothing AndAlso menu.SourceControl IsNot Nothing Then
Dim chart As Chart = TryCast(menu.SourceControl, Chart)
Dim dlg As New SaveFileDialog()
If chart IsNot Nothing AndAlso dlg.ShowDialog() = DialogResult.OK Then
chart.SaveImage(dlg.FileName, ChartImageFormat.Jpeg)
End If
End If
End If
End Sub