I want when I click on link label it give me the name of the textbox which in the same line in a variable named AA
`
Dim serial As Integer = 1
Public Function addnewline()
Dim lbl As New System.Windows.Forms.LinkLabel
Dim txt As New System.Windows.Forms.TextBox
' add label
Me.Controls.Add(lbl)
lbl.Top = serial * 27
lbl.Left = 100
lbl.Text = Me.serial
lbl.Name = "lbl" & Me.serial
' add textbox
Me.Controls.Add(txt)
txt.Top = serial * 27
txt.Left = 200
txt.Height = 500
txt.Width = 100
txt.TextAlign = HorizontalAlignment.Center
txt.Text = "text" & Me.serial
txt.Name = "txt" & Me.serial
serial += 1
Return lbl
Return txt
End Function
`
here is a gif for my code
We want two changes: set the textbox to the label's .Tag property, and use AddHandler for the click event. While I'm here I'm also going to fix a few other things that didn't make sense (only one return is allowed, and even that was not needed) and reduce flickering:
Dim serial As Integer = 1
Public Sub addnewline()
'Textbox
Dim txt As New System.Windows.Forms.TextBox
txt.Top = serial * 27
txt.Left = 200
txt.Height = 500
txt.Width = 100
txt.TextAlign = HorizontalAlignment.Center
txt.Text = "text" & Me.serial
txt.Name = "txt" & Me.serial
'Label
Dim lbl As New System.Windows.Forms.LinkLabel
lbl.Top = serial * 27
lbl.Left = 100
lbl.Text = Me.serial
lbl.Name = "lbl" & Me.serial.ToString()
' Next two lines are new
lbl.Tag = txt
AddHandler lbl.Click, AddressOf labelClick
Me.SuspendLayout()
Me.Controls.Add(lbl)
Me.Controls.Add(txt)
Me.ResumeLayout()
serial += 1
End Sub
Public Sub labelClick(sender As Control, e As EventHandler)
Dim txt As TextBox = TryCast(sender.Tag, TextBox)
If txt IsNot Nothing Then
AA.Text = txt.Name
End If
End Sub
While I'm here, you should really turn on Option Strict!
Try using a AddHandler, like this:
Dim serial As Integer = 1
Dim AA As String = ""
Public Function addnewline()
Dim lbl As New System.Windows.Forms.LinkLabel
Dim txt As New System.Windows.Forms.TextBox
' add label
Me.Controls.Add(lbl)
lbl.Top = serial * 27
lbl.Left = 100
lbl.Text = Me.serial
lbl.Name = "lbl" & Me.serial
' add textbox
Me.Controls.Add(txt)
txt.Top = serial * 27
txt.Left = 200
txt.Height = 500
txt.Width = 100
txt.TextAlign = HorizontalAlignment.Center
txt.Text = "text" & Me.serial
txt.Name = "txt" & Me.serial
AddHandler lbl.Click, Sub ()
AA = txt.Name
MessageBox.Show(AA)
End Sub
serial += 1
Return lbl
Return txt
End Function
I have modified a bit. Function is now a sub
Dim Serial As Integer = 1
Dim AA As String = ""
Public Sub AddNewline()
Dim lbl As New System.Windows.Forms.LinkLabel
Dim txt As New System.Windows.Forms.TextBox
' add label
Me.Controls.Add(lbl)
lbl.Top = Serial * 27
lbl.Left = 100
lbl.Text = Me.Serial
lbl.Name = "lbl" & Me.Serial
' add textbox
Me.Controls.Add(txt)
txt.Top = Serial * 27
txt.Left = 200
txt.Height = 500
txt.Width = 100
txt.TextAlign = HorizontalAlignment.Center
txt.Text = "text" & Me.Serial
txt.Name = "txt" & Me.Serial
AddHandler lbl.Click, AddressOf Label_Click
Serial += 1
'Return lbl
'Return txt
End Sub
Private Sub Label_Click(sender As Object, e As EventArgs)
AA = ""
Dim Lbl As Label = DirectCast(sender, Label)
If Lbl IsNot Nothing Then
Dim Idx As String = System.Text.RegularExpressions.Regex.Replace(Lbl.Name, "[^\d]", "")
If Idx <> "" Then
If Me.Controls.Find("txt" & Idx, True).Count = 1 Then
Dim T As TextBox = Me.Controls.Find("txt" & Idx, True)(0)
AA = T.Text
' or
TextBoxYellow.Text = AA
End If
End If
End If
End Sub
Related
I want get value 1 in all line for get path string, and programmatically add cover to flowlayoutpanel.
in Resource/Game List.ini (from drag n drop)
Apex Legends,Resource/Cover/Apex Legends.jpg,Resource/Game Info/Apex Legends.txt
Fortnite,Resource/Cover/Fortnite.jpg,Resource/Game Info/Fortnite.txt
PUBG,Resource/Cover/PUBG.jpg,Resource/Game Info/PUBG.txt
here my code :
Private Sub LabelSetting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LabelSetting.Click
FlpAddItem.Controls.Clear()
'I am confused in this part to get value 1 in all line for get path string
'Directory.GetFiles(Path) will be replace with streamreader from lines(i) value 1
Dim Path = '???
Dim ImageX As Image = Nothing
Dim x As Int32 = Directory.GetFiles(Path).Count - 1
Dim Img(x) As PictureBox
Dim ImgText(x) As Label
Dim ImgPanel As Panel
For i = 0 To Directory.GetFiles(Path).Count - 1
ImgPanel = New Panel
With ImgPanel
.Width = 96
.Height = 136
.BackColor = Color.Transparent
End With
FlpAddItem.Controls.Add(ImgPanel) 'Add panel to the flowlayoutpanel
ImgText(i) = New Label
With ImgText(i)
.Name = Directory.GetFiles(Path)(i).Replace(Path, "").Replace(".jpg", "").Replace(".png", "")
.FlatStyle = FlatStyle.Popup
.Width = 116
.Height = 40
.Padding = New Padding(0, 3, 0, 0)
.TextAlign = ContentAlignment.TopCenter
.Dock = DockStyle.Bottom
.BackColor = Color.Transparent
.ForeColor = Color.Black
End With
Img(i) = New PictureBox
With Img(i)
.Width = 96
.Height = 96
.Padding = New Padding(20, 20, 20, 20)
.BackColor = Color.Transparent
.BorderStyle = BorderStyle.FixedSingle
.SizeMode = PictureBoxSizeMode.StretchImage
End With
ImgPanel.Controls.Add(Img(i)) 'Add the picturebox to the panel
ImageX = Image.FromFile(Directory.GetFiles(Path)(i), True)
Img(i).Image = Image.FromFile(Directory.GetFiles(Path)(i))
ImgText(i).Text = Directory.GetFiles(Path)(i)
ImgPanel.Controls.Add(ImgText(i))
Next
End Sub
I suggest creating a class for the game name and path information
Public Class GamePath
Public Property GameName As String
Property Path As String
Public Overrides Function ToString() As String
Return GameName
End Function
End Class
I have overridden ToString, so that the game name will automatically be displayed in a ListBox.
When loading the form, I read this information from the INI-file and set it as data source of a listbox, where you will be able to select a game.
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim games =
From line In File.ReadLines(IniFilePath)
Let parts = line.Split(","c)
Select New GamePath With {.GameName = parts(0), .Path = parts(1)}
GameListBox.DataSource = games.ToList()
GameListBox.SelectedIndex = 0 'Select first game
End Sub
Note that it is easier to use File.ReadLines than a StreamReader. You will have to add Imports System.IO to the top of the code. Then we use the LINQ syntax to split each line at the comma and to create the game path information.
The user selects a game in the ListBox and then clicks a button. You can get the file path information from the ListBox like this:
Dim gamePath As GamePath = DirectCast(GameListBox.SelectedItem, GamePath)
Then read the files only once and assign the result to a variable
Dim files As String() = Directory.GetFiles(gamePath.Path)
Get the file count
Dim fileCount As Integer = files.Count
The whole Click method:
Private Sub StartGameButton_Click(sender As Object, e As EventArgs) Handles StartGameButton.Click
FlpAddItem.Controls.Clear()
Dim gamePath As GamePath = DirectCast(GameListBox.SelectedItem, GamePath)
Dim files As String() = Directory.GetFiles(gamePath.Path)
Dim fileCount As Integer = files.Count
Dim ImageX As Image = Nothing
Dim Img(fileCount) As PictureBox
Dim ImgText(fileCount) As Label
Dim ImgPanel As Panel
For i = 0 To fileCount - 1
Dim filePath = files(i)
ImgPanel = New Panel
With ImgPanel
.Width = 96
.Height = 136
.BackColor = Color.Transparent
End With
FlpAddItem.Controls.Add(ImgPanel) 'Add panel to the flowlayoutpanel
ImgText(i) = New Label
With ImgText(i)
.Name = System.IO.Path.GetFileNameWithoutExtension(filePath)
.FlatStyle = FlatStyle.Popup
.Width = 116
.Height = 40
.Padding = New Padding(0, 3, 0, 0)
.TextAlign = ContentAlignment.TopCenter
.Dock = DockStyle.Bottom
.BackColor = Color.Transparent
.ForeColor = Color.Black
End With
Img(i) = New PictureBox
With Img(i)
.Width = 96
.Height = 96
.Padding = New Padding(20, 20, 20, 20)
.BackColor = Color.Transparent
.BorderStyle = BorderStyle.FixedSingle
.SizeMode = PictureBoxSizeMode.StretchImage
End With
ImgPanel.Controls.Add(Img(i)) 'Add the picturebox to the panel
ImageX = Image.FromFile(filePath, True)
Img(i).Image = Image.FromFile(filePath)
ImgText(i).Text = filePath
ImgPanel.Controls.Add(ImgText(i))
Next
End Sub
Some details:
In the For-loop you can get the path of an image file with
Dim filePath = files(i)
You can get the name of the image with
.Name = System.IO.Path.GetFileNameWithoutExtension(filePath)
This automatically removes the directory name and the extension.
Later on, you don't call Directory.GetFiles again:
ImageX = Image.FromFile(filePath, True)
Img(i).Image = Image.FromFile(filePath)
ImgText(i).Text = filePath
If you only want to read the file paths into a list, you could write
Dim games =
(From line In File.ReadLines(IniFilePath)
Select line.Split(","c)(1)).ToList()
I have a code that makes small boxes at the side of the screen that when the mouse hovers over it, it grows and displays information in the label. How could I do this? Currently, the form does not register the label.
This is the button to make the form and the label.
Private Sub MakeForm()
Dim number As Integer = 1
Dim xaxis As Integer = 0
Dim yaxis As Integer = 0
Dim formlist As New List(Of Form)
Dim index As Integer = 0
For Each x In lstDate.Items
Dim frm As New Form
frm.Name = "frm" & number
frm.Text = "New Form"
frm.StartPosition = FormStartPosition.Manual
frm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
frm.TopMost = True
frm.Opacity = 0.4
Dim lbl As New Label
lbl.Text = x & vbNewLine & lstAssignments.Items.Item(index) & vbNewLine & lstAN.Items.Item(index)
lbl.ForeColor = Color.White
frm.Controls.Add(lbl)
lbl.Hide()
AddHandler frm.MouseEnter, AddressOf frm_MouseEnter
AddHandler frm.MouseLeave, AddressOf frm_MouseLeave
If DateDiff(DateInterval.Day, Now(), x) <= 1 Then
frm.BackColor = Color.Red
Else
frm.BackColor = Color.Black
End If
frm.AllowTransparency = True
formlist.Add(frm)
frm.Show()
number += 1
frm.Size = New Size(20, 50)
frm.Location = New Point(My.Computer.Screen.Bounds.Size.Width - frm.Width, yaxis)
yaxis += frm.Height + 10
index += 1
Next
End Sub
This is the code for mouse entry
Private Sub frm_MouseEnter(ByVal sender As System.Object, ByVal e As EventArgs)
Dim frm1 As Form = DirectCast(sender, Form)
Dim lbl As Label = New Label
lbl.Show()
frm1.Opacity = 1
frm1.BringToFront()
frm1.Size = New Size(200, 100)
Dim test As Integer = 1
Dim counter As Integer = 0
Dim yaxis As Integer = 0
Dim fin As Boolean = False
Do Until fin = True
If frm1.Name = "frm" & test Then
yaxis = counter
fin = True
Else
counter += 60
test += 1
End If
Loop
frm1.Location = New Point(My.Computer.Screen.Bounds.Size.Width - frm1.Width, yaxis)
End Sub
This is the code for mouse leave
Private Sub frm_MouseLeave(ByVal sender As System.Object, ByVal e As EventArgs)
Dim frm1 As Form = DirectCast(sender, Form)
frm1.Opacity = 0.4
frm1.BringToFront()
frm1.Size = New Size(20, 50)
Dim test As Integer = 1
Dim counter As Integer = 0
Dim yaxis As Integer = 0
Dim fin As Boolean = False
Do Until fin = True
If frm1.Name = "frm" & test Then
yaxis = counter
fin = True
Else
counter += 10 + frm1.Height
test += 1
End If
Loop
frm1.Location = New Point(My.Computer.Screen.Bounds.Size.Width - frm1.Width, yaxis)
End Sub
Thanks!
i'm currently doing a program that aims to sort values of dynamically created textboxes. how to do this in vb.net? This is my code for creating the textboxes:
For cnt = 0 To Val(TextBox1.Text) - 1
arrivalbox.Add(New TextBox)
With arrivalbox(cnt)
.Parent = Me
.Left = 0
.Height = 13
.Width = 65
.Top = .Height * cnt + 50
.Visible = True
.Tag = cnt
.Text = ""
.Name = "arrival" & cnt
.Location = New Point(380, 120 + (cnt * 25))
.TextAlign = HorizontalAlignment.Center
.Enabled = False
Me.Controls.Add(New TextBox)
End With
Something like this will loop through all the textboxes and add them to a sortedlist which will sort all the textboxes according to the values.
I'm not sure what you are trying to achieve here. But im guessing once you get these sorted then you can change the position based on their position in the sorted list
Dim listedboxes As SortedList(Of Double, TextBox)
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
listedboxes.add(ctrl.value,ctrl)
End if
Next
EDIT
maybe something like this.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim listedboxes As New SortedList(Of Double, TextBox)
For i As Integer = 0 To TextBox1.Text.Length - 1
Dim tb As New TextBox
With tb
.Parent = Me
.Left = 0
.Height = 13
.Width = 65
.Top = .Height * i + 50
.Visible = True
.Tag = i
.Text = TextBox1.Text.Substring(i, 1)
.Name = "arrival" & i
.TextAlign = HorizontalAlignment.Center
.Enabled = False
Me.Controls.Add(New TextBox)
End With
listedboxes.Add(TextBox1.Text.Substring(i, 1), tb)
Next
Dim j = 0
For Each kvp As KeyValuePair(Of Double, TextBox) In listedboxes
kvp.Value.Location = New Point(380, 120 + (j * 25))
j += 1
Next
End Sub
How can I implement shuffling of text on my button, i.e. text = chr(n+48) to shuffle text on each button.
Dim n As Integer = 0
For i As Integer = 0 To 10
' Initialize one variable
btnArray(i) = New Button
Next i
While n < 10
With btnArray(n)
.Tag = n + 1 ' Tag of button
.Width = 40 ' Width of button
.Height = 40
.Text = Chr(n + 48)
FlowLayoutPanel1.Controls.Add(btnArray(n))
AddHandler .Click, AddressOf Me.GenericClickHandler
n = n + 1
End With
End While
I don't know if there is a reason why you are doing that to two steps.
Try something like this:
Private btnArray As New List(Of Button)
For i As Integer = 0 To 10
Dim btn As New Button
With btn
.Tag = i ' Tag of button
.Width = 40 ' Width of button
.Height = 40
.Text = Chr(i + 48)
End With
btnArray.Insert(i, btn)
'FlowLayoutPanel1.Controls.Add(btnArray(i)) 'It works also
FlowLayoutPanel1.Controls.Add(btn)
AddHandler .Click, AddressOf Me.GenericClickHandler
Next i
Based on your comments:
Private btnArray As New List(Of Button)
Private btnShuffleArray As New List(Of Button)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 10
' Initialize one variable
Dim btn As New Button
With btn
.Tag = i ' Tag of button
.Width = 40 ' Width of button
.Height = 40
.Text = Chr(i + 48)
btnArray.Insert(i, btn)
' FlowLayoutPanel1.Controls.Add(btnArray(i))
'AddHandler .Click, AddressOf Me.GenericClickHandler
End With
Next i
'Randomize the list
Dim rand As New Random
Dim index As Integer
While btnArray.Count > 0
index = rand.Next(0, btnArray.Count)
btnShuffleArray.Add(btnArray(index))
btnArray.RemoveAt(index)
End While
For i = 0 To 10
FlowLayoutPanel1.Controls.Add(btnShuffleArray(i))
Next
End Sub
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.