I'v created a program that dynamically creates text-boxes according to the number selected from the combobox. The problem is that when I change the number from a big number to a smaller one the text-boxes keep the same ex(10 then 3).
see this image
I tried entering a code to remove the text-boxes before the *for loop but it didn't work.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
num = ComboBox1.Text
Dim tp As TabPage = tabControl1.TabPages(0)
*For i = 1 To num
Dim TB As New TextBox
TB.Size = New System.Drawing.Size(60, 20)
TB.Font = New System.Drawing.Font("arial", 20)
TB.Location = New Point(10, i * 40)
TB.Name = "tbt" & i
Select Case i
Case 1 To 5
TB.Location = New Point(10, i * 45)
Case 6 To 10
Dim i2 As Integer = i - 5
TB.Location = New Point(80, i2 * 45)
End Select
tp.Controls.Add(TB)
Next
End Sub
note: I tried this method without tabcontrol it worked perfectly.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
_Textboxes.ForEach(New Action(Of TextBox)(AddressOf Controls.Remove))
_Textboxes.Clear()
num = ComboBox1.Text
...
End Select
Controls.Add(TB)
_Textboxes.Add(TB)
Next
Related
I have a Datatable with few Line of Data, my aim is to check whether there is any Duplicate value and if there a duplicate line, i that Duplicate Value & i need to remove both lines that created duplicate.
Input
my expected Output would be
Any Suggestion will be helpful
Thanks in Advance
Thanks & Regards
Harsha
You need some way of getting a unique value for each row which depends only on the data in the row, this is usually known as a hash value.
Then you can use whatever method you like to bin the data and choose only the bins with one item.
For example, with just a DataGridView and a Button on a Form:
Public Class Form1
Dim dt As New DataTable
Function RowHash(dr As DataRow) As Long
'TODO: Make a hash function which will not overflow a Long.
Dim h As Long = 0
For Each itm In dr.ItemArray
h = (31 * h) + itm.GetHashCode()
Next
Return h
End Function
Sub MakeTestData()
dt.Columns.Add("Name")
dt.Columns.Add("P")
For i = 0 To 9
Dim nr = dt.NewRow()
nr("Name") = Chr(65 + (i Mod 6))
nr("P") = i Mod 6
dt.Rows.Add(nr)
Next
DataGridView1.DataSource = dt
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim newDataTable = dt.Clone()
Dim uniqueData = dt.AsEnumerable().
GroupBy(Function(r) RowHash(r)).
Select(Function(s) New With {.n = s.Count, .data = s.First().ItemArray}).
Where(Function(t) t.n = 1)
For Each u In uniqueData
Dim nr = newDataTable.NewRow()
nr.ItemArray = u.data
newDataTable.Rows.Add(nr)
Next
DataGridView1.DataSource = newDataTable
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MakeTestData()
End Sub
End Class
Before and after clicking the button:
I am looking for a way to draw multiple custom lines in my checkbox. I have the following code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
ComboBox1.Size = New System.Drawing.Size(100, 20)
ComboBox1.DropDownWidth = 100
ComboBox1.TabIndex = 0
ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
ComboBox1.Items.Add("test1")
ComboBox1.Items.Add("test2")
ComboBox1.Items.Add("test3")
AddHandler ComboBox1.DrawItem, AddressOf ComboBox1_DrawItem
End Sub
Private Sub ComboBox1_DrawItem(sender As Object, e As System.Windows.Forms.DrawItemEventArgs)
e.DrawBackground()
Dim black_pen As System.Drawing.Pen = New System.Drawing.Pen(System.Drawing.Color.Black, 2)
black_pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot
black_pen.Width = 2
Dim point1 As System.Drawing.Point = New System.Drawing.Point(10, 10)
Dim point2 As System.Drawing.Point = New System.Drawing.Point(90, 10)
e.Graphics.DrawLine(black_pen, point1, point2)
End Sub
I am looking for a way to show three different styled lines in my checkbox, which would be selected by the user. My current result is that I can only see one line so far (it's drawn on the top one combobox item) and other combobox items are empty. I see no values there.
How can I add additional lines/drawn graphics into my checkbox and make them "selectable"?
I'm trying to get 10 different inputs in TextBoxes to be displayed in a randomized order.
I split 2 processes to make it easier to understand:
Button puts numbers in random orders from 1-10(works)
Button should replace the numbers with the contents of the TextBoxes(doesn't work)
Here is the GUI
/ Here is the code:
Public tblRandom As New DataTable
Private Sub btnRandom_Click(sender As Object, e As EventArgs) Handles btnGo.Click
Dim r As New Random
Dim tblRandom As New DataTable
tblRandom.Columns.Add("Order")
tblRandom.Constraints.Add("pk", tblRandom.Columns(0), True)
While tblRandom.Rows.Count < 10
Dim newrow As Object = r.Next(10) + 1
Try
tblRandom.Rows.Add(CStr(newrow))
Catch ex As Exception
End Try
End While
dgvRandom.DataSource = tblRandom
End Sub
Private Sub btnReplace_Click(sender As Object, e As EventArgs) Handles btnReplace.Click
For Each row As DataGridViewRow In dgvRandom.Rows
For Each cell As DataGridViewCell In row.Cells
If cell.Value IsNot Nothing Then
Dim i As Integer = 0
While i < 10
i += 1
If cell.Value.ToString = i Then
cell.Value = ActiveControl.Tag(i)
End If
End While
End If
Next
Next
dgvRandom.DataSource = tblRandom
End Sub
End Class
My understanding of your goal here is to take the text in 10 textboxes and shuffled their content. The DataGrid seems to be a temporary location that you're using during the shuffle.
There's a much easier way that avoids all that. Try this code:
Private _random = New Random()
Private Sub BtnRandom_Click(sender As Object, e As EventArgs) Handles btnRandom.Click
Dim tbs As TextBox() = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, TextBox8, TextBox9, TextBox10}
Dim text = tbs.Select(Function(tb) tb.Text).OrderBy(Function(t) _random.Next()).ToList()
For Each x In tbs.Zip(text, Function(tb, t) New With {.tb = tb, .t = t})
x.tb.Text = x.t
Next
End Sub
That's it. Job done.
If you still need the DataGrid it would be easy to add a further line just after the x.tb.Text = x.t that adds the content of x.t to the grid.
For the first part you can do:
Public tblRandom As DataTable
Private ReadOnly rand As New Random
Private Sub btnRandom_Click(sender As Object, e As EventArgs) Handles btnRandom.Click
tblRandom?.Dispose()
tblRandom = New DataTable
tblRandom.Columns.Add("Order")
tblRandom.Constraints.Add("pk", tblRandom.Columns(0), True)
Dim nums As New List(Of Integer)
While nums.Count < 10
Dim num = rand.Next(1, 11)
If Not nums.Contains(num) Then
nums.Add(num)
End If
End While
nums.ForEach(Sub(n) tblRandom.Rows.Add(n))
dgvRandom.DataSource = tblRandom
End Sub
As for the second part, you just need to do:
Private Sub btnReplace_Click(sender As Object, e As EventArgs) Handles btnReplace.Click
For Each row As DataRow In tblRandom.Rows
'Assuming the text boxes are hosted by the Form.
Dim txt = Me.Controls.OfType(Of TextBox).
Where(Function(x) x.Tag?.ToString = row(0).ToString).
FirstOrDefault
If txt IsNot Nothing Then
row(0) = txt.Text
End If
Next
tblRandom.AcceptChanges()
End Sub
You are getting the tag from the ActiveControl
cell.Value = ActiveControl.Tag(i)
but as you've just clicked the Replace button, isn't that the active control?
I have a tab in a form. On form load, I am getting text from a text file line by line and displaying them as labels on a form Tabcontrol Tabpage along with dynamically displaying buttons beside them. Now on those buttons click I want to copy the text in the associated labels. Can anyone suggest what to put in the Nextbtn_Click event?
Dim FILE_NAME As String = "D:\1.txt"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim i As Integer = 1
For Each line As String In System.IO.File.ReadAllLines(FILE_NAME)
Dim NextLabel As New Label
Dim Nextbtn As New Button
NextLabel.Text = line
Nextbtn.Text = "Copy"
NextLabel.Height = 22
Nextbtn.Width = 55
Nextbtn.Height = 22
NextLabel.BackColor = Color.Yellow
TabPage2.Controls.Add(NextLabel)
TabPage2.Controls.Add(Nextbtn)
NextLabel.Location = New Point(10, 10 * i + ((i - 1) * NextLabel.Height))
Nextbtn.Location = New Point(120, 10 * i + ((i - 1) * Nextbtn.Height))
AddHandler Nextbtn.Click, AddressOf Me.Nextbtn_Click
i += 1
Next
End Sub
Private Sub Nextbtn_Click(sender As Object, e As EventArgs)
End Sub
Store the assc. label in the tag property and you can cast it back when you click on the button. The sender object is the button that is currently clicked.
Dim FILE_NAME As String = "D:\1.txt"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim i As Integer = 1
For Each line As String In System.IO.File.ReadAllLines(FILE_NAME)
Dim NextLabel As New Label
Dim Nextbtn As New Button
Nextbtn.Tag = NextLabel
NextLabel.Text = line
Nextbtn.Text = "Copy"
NextLabel.Height = 22
Nextbtn.Width = 55
Nextbtn.Height = 22
NextLabel.BackColor = Color.Yellow
TabPage2.Controls.Add(NextLabel)
TabPage2.Controls.Add(Nextbtn)
NextLabel.Location = New Point(10, 10 * i + ((i - 1) * NextLabel.Height))
Nextbtn.Location = New Point(120, 10 * i + ((i - 1) * Nextbtn.Height))
AddHandler Nextbtn.Click, AddressOf Me.Nextbtn_Click
i += 1
Next
End Sub
Private Sub Nextbtn_Click(sender As Object, e As EventArgs)
Dim s As String = DirectCast(DirectCast(sender, Button).Tag, Label).Text
End Sub
Private Sub Clicked(ByVal sender As Object, ByVal e As EventArgs)
Dim b As Button = DirectCast(sender, Button)
TextBox2.Text = b.Name
Clipboard.SetText(b.Name)
End Sub
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