Set combo box by name - vba

When using VBA for automatic handling of Internet Explorer, you can select an item in a combo box, by its value. If our combo box HTML looks like this:
<select name="my_combo_box" id="fruits">
<option value="1">Apple</option>
<option value="2">Banana</option>
<option value="3">Strawberry</option>
</select>
Then you can select the option "Banana" using VBA, like this:
getElementById("fruits").value = 2
But is there any way to select it using its name (display member), i.e. Banana?

"Banana" is not a HTML property, but the text between tags.
You could loop through all elements and with option tag and then choose the one with desired innerText. In the next step, you could cut id from innerHTML. Like:
Sub MyMacro()
Dim opt As IHTMLElement
Dim iComboBox As IHTMLElement
Dim sID As String
For Each opt In iComboBox.getElementsByTagName("option")
If InStr(opt.innerHTML, "Banana") Then
sID = CutId(opt.innerHTML)
End If
Next opt
End Sub
Function CutId(s As String) As String
Dim s As String
s = Mid(s, InStr(s, "=") + 2, 1)
CutId = s
End Function
Then you can use sId for selecting item. I guess that your HTML is just example, so it is possible that your will need to adjust CutId function, this is just my proposal for solution. Especially, you will adjust my code if you expect id to have two digits.

Related

VB.Net , Finding data in a text file by ID number

I'm using the VB.net forms application for this project.
so I have a text file like this
7,John,Kimberlake,john#mail.com,27,Bachelor
8,Tiny,Down,tiny#mail.com,34,Master
9,Jeniffer,Kime,Jen#mail.com,22,None
I have 1 textbox and 1 button.
The purpose is that you need to fill an id number to find the data about the person.
Dim Findstring = IO.File.ReadAllText("data.txt")
Dim Data As String = TextBox1.Text
Dim aryTextFile() As String
aryTextFile = Findstring.Split(",")
If aryTextFile.Contains(Data) Then
End If
I tried this and something like finding the index number in the array of the requested id but it didn't work.
Instead of ReadAllText use ReadLines and loop each line to get the data.
Walk through below code and comments.
There are much better ways to do this, But the below is very basic way of doing for easy understanding.
'READ EACH LINE OF THE TEXT FILE.
For Each item As String In IO.File.ReadLines("C:\\Desktop\\test.txt") 'ENSURE VALID PATH HERE
'THIS IS EACH LINE.
Dim Findstring = item
'ASSUME THIS IS TEXT ID FROM TEXT BOX.
Dim ID As String = "8"
'SPLIT THE LINE BASED ON ","
Dim aryTextLine() As String
aryTextLine = Findstring.Split(",")
'NOW YOU HAVE ARRAY TO READ EACH ITEM.
If aryTextLine(0) = ID Then
Dim name = aryTextLine(1)
End If
Next

Wildcard Search character in vb.net

I am wanting to make a wildcard search character (ex. Binary%) so when they click search it finds all the files with the word Binary in the filename and loads them into a list box. My current code is below.
Private Sub _test_TextChanged(sender As Object, e As TextChangedEventArgs) Handles _test.TextChanged
For x As Integer = 0 To _listbox.Items.Count - 1
If _listbox.Items(x).ToString = _test.Text$ Then
_listbox.SelectedIndex = x
Return
End If
Next
End Sub
Any help is welcome!
Thank you!
-Kyvex
What you are asking doesn't really match your code.
... "when they click search it finds all the files" ...
But you have a TextChanged event handler of a TextBox (not a Button)
..."with the word Binary in the filename and loads them into a list box"
But you select only the first item which matches the filter of items already in a ListBox
To get your code to do what it seems to want to do, simply use the Like operator and add the wildcard character (*) after the TextBox.Text
For x As Integer = 0 To _listBox.Items.Count - 1
If _listBox.Items(x).ToString Like _test.Text & "*" Then
_listBox.SelectedIndex = x
Return
End If
Next
Now you can select the first item in the listbox which matches the filter
If you have a multi-select ListBox, you can use this
If _test.Text = "" Then Exit Sub
_listBox.SelectionMode = SelectionMode.MultiSimple
For x As Integer = 0 To _listBox.Items.Count - 1
_listBox.SetSelected(x, _listBox.Items(x).ToString Like _test.Text & "*")
Next
(the filter logic is the same as the first example)
And you can make it case-insensitive
_listBox.SetSelected(x, _listBox.Items(x).ToString().ToUpper() Like _test.Text.ToUpper() & "*")

How can i check for a character after certain text within a listbox?

How can i check for a character after other text within a listbox?
e.g
Listbox contents:
Key1: V
Key2: F
Key3: S
Key4: H
How do I find what comes after Key1-4:?
Key1-4 will always be the same however what comes after that will be user defined.
I figured out how to save checkboxes as theres only 2 values to choose from, although user defined textboxes is what im struggling with. (I have searched for solutions but none seemed to work for me)
Usage:
Form1_Load
If ListBox1.Items.Contains("Key1: " & UsersKey) Then
TextBox1.Text = UsersKey
End If
Which textbox1.text would then contain V / whatever the user defined.
I did try something that kind of worked:
Form1_Load
Dim UsersKey as string = "V"
If ListBox1.Items.Contains("Key1: " & UsersKey) Then
TextBox1.Text = UsersKey
End If
but i'm not sure how to add additional letters / numbers to "V", then output that specific number/letter to the textbox. (I have special characters blocked)
Reasoning I need this is because I have created a custom save settings which saves on exit and loads with form1 as the built in save settings doesn't have much customization.
e.g Can't choose save path, when filename is changed a new user.config is generated along with old settings lost.
Look at regular expressions for this.
Using the keys from your sample:
Dim keys As String = "VFSH"
Dim exp As New RegEx("Key[1-4]: ([" & keys& "])")
For Each item As String in ListBox1.Items
Dim result = exp.Match(item)
If result.Success Then
TextBox1.Text = result.Groups(1).Value
End If
Next
It's not clear to me how your ListBoxes work. If you might find, for example, "Key 2:" inside ListBox1 that you need to ignore, you will want to change the [1-4] part of the expression to be more specific.
Additionally, if you're just trying to exclude unicode or punctuation, you could also go with ranges:
Dim keys As String = "A-Za-z0-9"
If you are supporting a broader set of characters, there are some you must be careful with: ], \, ^, and - can all have special meanings inside of a regular expression character class.
You have multiple keys, I assume you have multiple textboxes to display the results?
Then something like this would work. Loop thru the total number of keys, inside that you loop thru the alphabet. When you find a match, output to the correct textbox:
Dim UsersKey As String
For i As Integer = 1 To 4
For Each c In "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
UsersKey = c
If ListBox1.Items.Contains("Key" & i & ": " & UsersKey) Then
Select Case i
Case 1
TextBox1.Text = UsersKey
Case 2
TextBox2.Text = UsersKey
Case 3
TextBox3.Text = UsersKey
Case 4
TextBox4.Text = UsersKey
End Select
Exit For 'match found so exit inner loop
End If
Next
Next
Also, you say your settings are lost when the filename is changed. I assume when the version changes? The Settings has an upgrade method to read from a previous version. If you add an UpgradeSettings boolean option and set it to True and then do this at the start of your app, it will load the settings from a previous version:
If My.Settings.UpgradeSettings = True Then
My.Settings.Upgrade()
My.Settings.Reload()
My.Settings.UpgradeSettings = False
My.Settings.Save()
End If
Updated Answer:
Instead of using a listtbox, read the settings file line by line and output the results to the correct textbox based on the key...something like this:
Dim settingsFile As String = "C:\settings.txt"
If IO.File.Exists(settingsFile) Then
For Each line As String In IO.File.ReadLines(settingsFile)
Dim params() As String = Split(line, ":")
If params.Length = 2 Then
params(0) = params(0).Trim
params(1) = params(1).Trim
Select Case params(0)
Case "Key1"
Textbox1.Text = params(1)
Case "Key2"
Textbox2.Text = params(1)
End Select
End If
Next line
End If
You can associate text box with a key via its Name or Tag property. Lets say you use Name. In this case TextBox2 is associated with key2. TextBox[N] <-> Key[N]
Using this principle the code will look like this [considering that your list item is string]
Sub Test()
If ListBox1.SelectedIndex = -1 Then Return
Dim data[] As String = DirectCast(ListBox1.SelectedItem, string).Split(new char(){":"})
Dim key As String = data(0).Substring(3)
Dim val As String = data(1).Trim()
' you can use one of the known techniques to get control on which your texbox sits.
' I omit this step and assume "Surface1" being a control on which your text boxes sit
DirectCast(
(From ctrl In Surface1.Controls
Where ctrl.Name = "TextBox" & key
Select ctrl).First()), TextBox).Text = val
End Sub
As you can see, using principle I just explained, you have little parsing and what is important, there is no growing Select case if, lets say, you get 20 text boxes. You can add as many text boxes and as many corresponding list items as you wish, the code need not change.

Can dynamic variables be created in vb.net

I'm trying to dynamically create variables if possible.
I have a list box containing check boxes. The user selects items from it and the selections are then added to a database. They can select any number of items from the list (1-7). I have iterated through the selected items and added them to an arraylist.
I now want to store each selected item in a different variable to be added to the database. The idea is to use just one insert statement which is expecting 7 entries. If the user only selects say 2 items, these are inserted as well as 5 empty strings.
Dim part1 As String = ""
Dim part2 As String = ""
Dim part3 As String = ""
Dim part4 As String = ""
Dim part5 As String = ""
Dim part6 As String = ""
Dim part7 As String = ""
'add selected items to an array
For i = 0 To listPart.CheckedItems.Count - 1
selected.Add(listPart.CheckedItems(i).Text)
Next
I'd like to do something like:
For j = 0 To selected.Count
part+j = selected(j)
Next
This way I should have variables holding empty strings if the user does not select all 7 items
I don't think dynamic variables can be created, or need to be created.
Objects like ArrayList, Array, List, and Collection support adding items ( similar to how you are describing adding dynamic variables)
If you are looking to add a database entry for each item in the listview it's possible you could just loop through the listview
For item as ListViewItem in listPart.Items
'Save item.Text
Next item
you can do this with reflection, but it is not advisable. It is hard to debug, it is hard to implement, and it can lead to issues down the road with access restrictions.
http://msdn.microsoft.com/en-us/library/6z33zd7h.aspx
System.Reflection.FieldInfo FI = T.GetField("part" & j);
FI.SetValue({your class name here},selected(j));

Select Line from listbox and then convert to string

I want to search for a small bit of text in my listbox, if it's in the listbox I want to select it, and then covert it to a string.
How do I do this?
Because I cannot find the good command to select something on a specific line!
Thanks
To select a ListBox item, set the ListBox's SelectedIndex property. So, for example:
Dim stringToFind As String = "someString"
For i As Integer = 0 To Me.MyListBox.Items.Count - 1
Dim itemAsString As String = Me.MyListBox.Items(i).ToString()
If itemAsString.Contains(stringToFind) Then
Me.MyLabel.Text = itemAsString
Me.MyListBox.SelectedIndex = i
Exit For 'If you're using a MultiSelect ListBox, you can add to Me.MyListBox.SelectedIndices and remove this line.
End If
Next