How to make a Label using only code? - vb.net

I want to add a label to a form with the click of a button. When I use the code here it only adds 1 label, but I want to add an unlimited amount. Every time I click the button it only adds 1 label even if I change the name.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lbl As New label
lbl.Size = New System.Drawing.Size(159, 23) 'set your size
lbl.Location = New System.Drawing.Point(12, 180) 'set your location
lbl.Text = (TextBox1.Text) 'set your name
Me.Controls.Add(lbl) 'add your new control to your forms control collection
End Sub

Something like this:
Dim lbl As New label
lbl.Size = New System.Drawing.Size(159, 23) 'set your size (if required)
lbl.Location = New System.Drawing.Point(12, 180) 'set your location
lbl.Text = "label text goes here" 'set the text for your label
Me.Controls.Add(lbl) 'add your new control to your forms control collection
I'll leave you to programatically set the location. You could use something like a global variable to keep count of how many labels have been created...

adds a new linklabel for each item in a listbox i add 24 to the updown integer
each time it creates a new linklabel so my linklabel ends up in a different location.
Dim rightleft as integer =1018
Dim updown as integer= 56
Dim LLCount as integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Do
Dim lklabel As New LinkLabel
lklabel.Size = New System.Drawing.Size(65, 13)
lklabel.Location = New System.Drawing.Point(rightleft, updown)
lklabel.Text = ListBox1.Items(LLCount)
lklabel.Name = "lklabel" & LLCount
Me.Controls.Add(lklabel)
updown += 24
LLCount += 1
Loop Until lnklbl = ListBox1.Items.Count
end sub

Related

VB.net adding multiple controls on button click

I'm trying to make a userinterface that generates itself on request (button click)
Private Sub Body_new_part_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Body_new_part_add.Click
So when i add a Combobox as first it's no problem it generates the box & places it on the right position etc.
Dim oTypeBox As New ComboBox
oTypeBox.Name = "Body_type_" & oBodyPartsNumber
oTypeBox.Location = New System.Drawing.Point(7, 78)
Body_parts.Controls.Add(oTypeBox)
Now i want to add another control, a textbox next to the Combobox.
Dim oTypeBox As New ComboBox
oTypeBox.Name = "Body_type_" & oBodyPartsNumber
oTypeBox.Location = New System.Drawing.Point(7, 78)
Body_parts.Controls.Add(oTypeBox)
Dim oTextbox As New TextBox
oTextbox.name = "test"
oTextbox.Location = New System.Drawing.Point(50, 78)
Body_parts.Controls.Add(oTextbox)
This gives me this error.
'New' cannot be used on an interface.
What do i need to change in order to get this done? I need to add +- 10 controls on each button click event.
Try this one
Public Class Form1
Dim cLeft As Integer = 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddNewTextBox()
End Sub
Public Function AddNewTextBox() As System.Windows.Forms.TextBox
Dim txt As New System.Windows.Forms.TextBox()
Me.Controls.Add(txt)
txt.Top = cLeft * 25
txt.Left = 100
txt.Text = "TextBox " & Me.cLeft.ToString
cLeft = cLeft + 1
Return txt
End Function
End Class

How to make a Label using vb code by clicking button

I want to add a label to the form with a click of the button.
When I use that code here, it only adds 1 label but I want to add unlimited amount every time I click the button; it even adds 1 label even if I change the name.
Thank you every one.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lbl As New label
lbl.Size = New System.Drawing.Size(159, 23) 'set your size
lbl.Location = New System.Drawing.Point(12, 180) 'set your location
lbl.Text = (TextBox1.Text) 'set your name
Me.Controls.Add(lbl) 'add your new control to your forms control collection
End Sub
Just a rehash of the previous 2 perfectly good answers. Here it tries to make clear that at least the location and text must be set based on logic provided by the user
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' YOU decide where EACH new label goes and pass the X,Y for each
' new label; YOU decide on the text and pass it. you can make them
' variables, but YOU have to do some of the thinking....
' <...> == information YOU provide
Private x As integer = <where you want the new label>
Private y as integer = <where you want the new label>
Private txt as String = <Text for this new label>
' EXAMPLEs
' a) set text from a textbox
' txt = txtLblText.Text
' b) Set X position from another code integer variable
' x = thisX
' c) Set Y position from textbox input
' y = Integer.Parse(txtLblYPos.Text)
Dim lbl as Label = MakeNewLabel(x, y, txt As string)
Me.Controls.Add(lbl)
End Sub
Friend function MakeNewLabel(x as integer, y as Integer, txt As String) as label
Dim lbl As New label
' add other label props here as needed
lbl.Size = New System.Drawing.Size(159, 23) 'set your size
lbl.Location = New System.Drawing.Point(x, y) 'set your location
lbl.Text = txt
Return lbl
End Function
As correctly pointed out by #jlvaquero, you are overlapping your labels. The reason for this is that you're not changing the Point where these labels are being added to the Form.
One solution is to have field variables that can adjust the Point.
Private x As Integer = 12
Private y As Integer = 180
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lbl As New label
lbl.Size = New System.Drawing.Size(159, 23) 'set your size
lbl.Location = New System.Drawing.Point(x, y) 'set your location
lbl.Text = (TextBox1.Text) 'set your name
Me.Controls.Add(lbl) 'add your new control to your forms control collection
x += 10 'arbitrary value, you could adjust y, too
End Sub
hello and thank you everyone for your great help i would no got that far without you her si the code that i used
Public Class Form1
Dim counter As Integer = 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lbl As New Label
lbl.Name = "Label" & counter
lbl.Size = New Size(150, 20)
lbl.Location = New Point(200, counter * 22)
lbl.Text = TextBox1.Text 'text on label
Me.Controls.Add(lbl)
counter += 1
End Sub
End Class

VB Tab control in draw.mode

I am trying to make the tabs show horizontally on the right side of my form. I can't use tabcontrol from tool box because of how the text displays.
I am using a code that I found to help me. But after exhausting all of my resources I can't seem to get the code to point to the tabPages collection. I have entries in there but the tabs show up blank.
Public Sub New()
tabControl1 = New TabControl()
Dim tabPage1 As New TabPage()
' Sets the tabs to be drawn by the parent window Form1.
' OwnerDrawFixed allows access to DrawItem.
tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed
tabControl1.Controls.Add(tabPage1)
tabControl1.Location = New Point(25, 25)
tabControl1.Size = New Size(250, 250)
tabPage1.TabIndex = 0
myTabRect = tabControl1.GetTabRect(0)
ClientSize = New Size(300, 300)
Controls.Add(tabControl1)
AddHandler tabControl1.DrawItem, AddressOf OnDrawItem
End Sub!
tab Example
You can set the .Alignment property of the TabControl to Left to use horizontal tabs.
If you don't like that, try a FlowLayoutPanel with a separate TabControl for each tab, e.g.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim flp As New FlowLayoutPanel
flp.Dock = DockStyle.Left
flp.AutoSize = True
flp.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowOnly
Me.Controls.Add(flp)
For i As Integer = 0 To 5
Dim tbc As New TabControl
Dim tbp As New TabPage("Tab" & i.ToString)
tbc.TabPages.Add(tbp)
flp.Controls.Add(tbc)
Next i
End Sub
End Class
I ended up compiling this code from different sources to get this working,
Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
Dim g As Graphics
Dim sText As String
Dim iX As Integer
Dim iY As Integer
Dim sizeText As SizeF
Dim ctlTab As TabControl
Dim r As New RectangleF(e.Bounds.X, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2)
ctlTab = CType(sender, TabControl)
g = e.Graphics
sText = ctlTab.TabPages(e.Index).Text
sizeText = g.MeasureString(sText, ctlTab.Font)
iX = e.Bounds.Left + 6
iY = e.Bounds.Top + (e.Bounds.Height - sizeText.Height) / 2
g.DrawString(sText, ctlTab.Font, Brushes.Black, iX, iY)
End Sub
The text doesn't show up in the RAD but it does when I debug/run it.
Many thanks to LUC001 # http://www.dreamincode.net/forums/topic/125792-how-to-make-vertical-tabs/

Dynamic control in VB.NET

This is my code for dynamic textbox controls in button click event. The code is working well. If i click the button 3 times, it is generated 3 text boxes. But I have no idea to assign text box values to a variable. I dont know the names of dynamic generated controls. if i want to add value to 3rd text box, how to do it?
Dim txtBx As TextBox
Static x As Integer
Static i As Integer
txtBx = New TextBox
txtBx.Location = New Point(10, 10 + x)
txtBx.Size = New Size(100, 20)
i = i + 1
x = x + 20
Me.Controls.Add(txtBx)
if i create normal textbox i can do it with,
TextBox3.Text = "Some value"
But I dont know to do this for dynamic controls.
Here's an example, storing the references in a List(Of Textbox):
Public Class Form1
Private tbList As New List(Of TextBox)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim tb As TextBox
Dim n As Integer
n = tbList.Count + 1
tb = New TextBox
With tb
.Location = New Point(10, 10 + (n * 20))
.Name = "dynTB" & n.ToString
.Size = New Size(100, 20)
End With
Me.tbList.Add(tb)
Me.Controls.Add(tb)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
' Testing:
If Me.tbList.Count >= 3 Then Me.tbList(2).Text = "This is textbox 3"
End Sub
End Class

Creating buttons in runtime from Database field

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