vb.net matching textbox.text with contents of listbox data - vb.net

What I want to do is type a number in textbox1 and then match the word that corresponds to it for example
if textbox1.text = 6
then textbox2.text = searchable
but this list could be 10000 items long, so don't want to hard code it.
listbox contains the following data.I am open to changing the layout slightly if needed.
1 example
2 word
3 to
4 find
5 by
6 searchable
7 numbers
then upon button2 click my textbox2 would contain searchable (but not the number)
Thanks

Add this code in your button's click event
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Val(TextBox1.Text) <= ListBox1.Items.Count - 1 Then '<--- check the textbox contains a number less than the item count of the list box
TextBox2.Text = ListBox1.Items(TextBox1.Text) '<---- replace the number with carasponding item in the listbox
End If
End Sub

Maybe use Listbox.Findstring(textbox1.text.trim & " ") or Listbox.FindString(Val(TextBox1.Text))
These return the index to the item you are looking for...

Related

how to enumerate listbox?

Hi I'm new to Visual Studio and I am in need of a help in Visual Studio on a Windows Form App. also I don't really know if this is vb.net
I would like to make an application where if a user inputs something in the TextBox, it is presented in the ListBox whilst having some sort of an enumeration.
like for example.
If I type "Wow amazing" in the TextBox and confirm it. Then type another text like "I love you" in the TextBox and confirm it again
It should show up in the Listbox as "1. Wow Amazing" and "2. I love you".
Here's my code. I am not able to get it right and I don't really know how. I tried doing the for Loops and Do While but it would just duplicate the texts or am I doing something wrong?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Integer = 0
ListBox1.Items.Add(i + 1 & ". " & TextBox1.Text)
End Sub
You are so close!
On every button click you need to:
Take number of elements in list, to determine the number.
Insert text concatenated with number.
So, you should use this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Step 1
Dim i As Integer = ListBox1.Items.Count + 1
'Step 2
ListBox1.Items.Add(i & ". " & TextBox1.Text)
End Sub

Auto formatting textbox

I am trying to add multiple texts from a datagridview to a textbox.My multiple texts i mean, the textbox will contain the value of cell(12) of all the rows of the dgvw.Here's my code :
Private Sub Button6_Click_1(sender As Object, e As EventArgs) Handles Button6.Click
For Each row In Selected.dg2.Rows
SendMail.totxt.Text &= row.Cells(12).Value
SendMail.Show()
Next
Now, cell 12 contains E-MAILS.My application is also an EMAIL app. What I want is, in the textbox, a comma(,)will be added automatically after every email address/every row's cell value. Any solution?
Private Sub Button6_Click_1(sender As Object, e As EventArgs) Handles Button6.Click
For Each row In Selected.dg2.Rows
SendMail.totxt.Text &= row.Cells(12).Value
SendMail.totxt.Text &= ", "
Next
totxt.Text = totxt.Text.Remove(totxt.TextLength - 2, 2)
You might also want to have a look at giving your email column a name, so that you can refer to it with row.cells("email").value instead of refering to it by index. This way, if you the number of columns in your datagrid ever change, your program wont break.

One row selector in all Datagridview

Based on what you see below is the sample of a datagridview that an item has been selected.
Here is my question what if I have a more than 1 Datagridview? I mean 5 Datagridview like this.
All of them contains 1 column only. Based on the 1st image, the row selector or the blue one select an item.
My question is how can I make all of the datagridview only have one row selector?
What happens is when i selected each of them all of it has a row selected 5 selections.
How can I make 1 row selector for all of them.
Thinking of changing the Selection Color but I think that's not applicable.
TYSM for future help.
If you're looking for an alternative, you can also try this approach:
Private Sub DataGridView_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles _
DataGridView1.CellEnter, DataGridView2.CellEnter, DataGridView3.CellEnter, DataGridView4.CellEnter, DataGridView5.CellEnter
Dim MyDataGrids() As DataGridView = {DataGridView1, DataGridView2, DataGridView3, DataGridView4, DataGridView5}
For i = 0 To MyDataGrids.Count - 1
If MyDataGrids(i).Name = sender.Name Then
Continue For
Else
MyDataGrids(i).ClearSelection()
End If
Next
End Sub
MyDataGrids() is an array of DataGridViews. If for example, the controls you need to check increases, just add the name of the DataGridView in this array and it will be included in the checking and clearing of selections. Don't also forget the Handles event. As you can see here, all of the five grids .CellEnter event are included so you don't have to copy-paste it to five separate events.
Try this maybe it is more easy to edit if you add more grid
Private Sub ClearSelectedCells(ByVal Identifier As Integer)
If Identifier = 1 Then 'for datagrid 1
dg2.ClearSelection()
dg3.ClearSelection()
ElseIf Identifier = 2 Then 'for datagrid 2
dg1.ClearSelection()
dg3.ClearSelection()
'and so on
.
.
End If
End Sub
Private Sub dg1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dg1.CellClick
ClearSelectedCells(1)
End Sub
'and other gridcellclick
.
.

Take Text from Textbox and used it in a C

Visual Basics 2010:
Two numbers are generated automatically. For example if the Random numbers generated are 2 and 3, you should press the button in 2nd row and 3rd column. I have created the random number generator:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To 5
TextBox1.Text = (Convert.ToString(random.Next(1, 5)))
TextBox2.Text = (Convert.ToString(random.Next(1, 5)))
Next
End Sub
But I can't use it to press the correct button. If the correct button is pressed the color changes. If not there are no changes. How can I do this. Please help me...
Note:
It is a board game. The game is played on a 4 by 4 grid To play the game, a player generates two random numbers and colors in an area on the grid indicated by the numbers. For example if the generated number is a 2 and a 3 the player clicks in the 2x3 square which results in the color of the square being changed. A player should only be able to click and change the color of the square represented by the random numbers only.
You should create a class-level variable for both the row and column chosen:
Private randomRow as Integer
Private randomCol as Integer
Then in your code, assign the values:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
randomCol = random.Next(1, 5)
randomRow = random.Next(1, 5)
TextBox1.Text = randomCol.ToString()
TextBox2.Text = randomRow.ToString()
End Sub
Now in your code the sees the user click a box, check to see if the box column and row are the same as the randomCol and randomRow variables.

[VB.NET]How to add a string item, returned from another form, to a list box at a specific spot in place of another?

The idea is that there's a list-box with items, and say you want to modify an item in the middle of the list. You select that item and click "Modify" button and a new form appears with the previously selected item data from first form ready to be modified in a text-box. After modifying and clicking Ok the second form suppose to return that modified string to the first form and insert the modified string into the same spot instead of the originally selected item, so it looks like it was edited to the user.
Edit:
Translated the pseudo code to actual VB.NET code to refresh my own memory :D
string = InputBox("Enter text")
// Do whatever you want with the string
x = listBox.SelectedIndex
listBox.Items(x) = string
You can try Content in place of Text too.
Make sure that the form that pops up is Modal. Here is a simple example of what you can do. (This assumes your listbox items are strings and is an example for editing up to three listbox items only. If the list is going to be much larger you will want to pursue a different architecture.)
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim intTextboxCounter As Integer = 0
For Each i As Integer In Form1.ListBox1.SelectedIndices
Select Case intTextboxCounter
Case 0
TextBox1.Text = Form1.ListBox1.Items(i)
Case 1
TextBox2.Text = Form1.ListBox1.Items(i)
Case 3
TextBox3.Text = Form1.ListBox1.Items(i)
End Select
intTextboxCounter += 1
Next
End Sub
When this loads it will spin through the selected list items and put its value in a textbox. To update the values...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim intTextboxCounter As Integer = 0
For Each i As Integer In Form1.ListBox1.SelectedIndices
Select Case intTextboxCounter
Case 0
Form1.ListBox1.Items(i) = TextBox1.Text
Case 1
Form1.ListBox1.Items(i) = TextBox2.Text
Case 2
Form1.ListBox1.Items(i) = TextBox3.Text
End Select
Next
End Sub