Can't turn combobox value into a string - Visual Basic - vb.net

I am trying to use a combobox as an input for resolving an IP address, but I have an issue. When a user selected their desired IP from the list, and click 'test', it doesn't seem to recognise the input of the combobox. If I replace the combobox with a text box, it works fine - for some reason, it won't accept the combobox input, even thought the result of both that and the textbox are exactly the same, they are both simple strings.
Dim ip As String = combobox1.Text
_sock = New TcpClient(ip, port)
If I change 'combobox1.Text' to textbox1.Text, and then enter the IP manually into textbox1, it doesn't work! I also tried 'combobox1.Text.ToString', and that didn't work. This is extremely frustrating, so a quick answer would be appreciated, thanks.

'Returns the Text that has been selected:
Dim ip As String = combobox1.SelectedItem
'Returns the Index of the selected Item (ascending from 0):
Dim ip As String = combobox1.SelectedIndex

Ok, I figured it out - the values in the data source for the combobox were taken from a text file, where each part of the string was taken from a different line. Therefore, instead of being 192.186.1.75 (for example), it was infact VbCrLf + 192.186.1.75, which is what caused the error. I guess my next question is, does anyone know how to stop that?
Edit - Nevermind, it's fixed now!
Thank you all for your help :)

Related

Extracting Substrings from textboxes in VB

I am working on a VB program for school. I am having some trouble extracting a substring from a string and I would really appreciate some help.
The form has different text boxes and one of them is where you type in a person's full name into one text box. The listbox on the form, when hitting the compute button, is supposed to display only the person's last name.
I am not sure how I am supposed to extract just the last name of the string of whatever name is typed into the text box.
All I got so far is:
Dim name As String
name = txtName.Text
(txtName is the name of the text box)
Okay, so I added:
lstOut.Items.Add(name.Substring(6))
That to my code. The name I typed in for an example when I ran the program was Helen Woods. 6 is in the substring because that is where the space starts and when I clicked compute, it listed only the last name, just like I wanted. But, this only works if the first name is five letters long. I need a way to make the program automatically find the space in between the two names.
EDIT:
When I add:
lstOut.Items.Add(name.IndexOf(""))
The listbox gives me a 0 whenever I type in a name and hit the compute button.
try this:
Private Sub GetLastName()
dim lsName as new List(Of String)
dim name as string
for each name in txtName.text.split(" ")
lsName.Add(name)
next
lstOut.items.Add(lsName.item(lsName.count-1))
end sub
You can call this procedure at your button event.

Multiple Lines Webrequest From RichTextBox

Ok so i'm trying to make multiple webrequests, but in a different way.
i'm trying to let my users add the websites on each line of a richtextbox and when they click a button it will run this:
Dim request As WebRequest = WebRequest.Create("Website")
But not for the hole richtextbox, but for every line, so say the richtextbox has all these lines:
google.com
facebook.com
youtube.com
and once they click the button, those sites will be put into the space where it says Website.
If anyone can help me with this, thank you!
Also sorry if this is messy, I don't think there's any other way to present this information!
This could get you started. Textboxes have a Lines property that contains a collection of strings from each line of the textbox (See this MSDN page for reference)
Dim arrUrls() As String = RichTextBox1.Lines
For i As Integer = 0 To UBound(arrUrls)
If Not arrUrls(i).Length=0 Then Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(arrUrls(i))
Next

Updating the text displaid in MACROBUTTON in MS Word

I am using macrobutton in VBA to have a field whose value is calculated by accessing some other system, and at the same time, I want to be able to double-click on that field to specify some settings used to retrieve data from that other system.
The whole macro stuff works fine but I can not figure out how to change the label on the macrobutton.
Typically the macrobutton looks like this { MACROBUTTON macro_name label {some_arg}{some_arg2} }
I tried accessing selection.fields(1).code.text and even doing regexp to replace 'label' by something else but that just does not work, i.e. either I lose the args or I screw up the label.
Any advice for this issue, or perhaps a suggestion of some other type of field I could use to achieve this? I wouldn't mind using DOCVARIABLE but these can not respond to clicks and carry arguments?
You should be able to do something like this:
Sub Testit1()
Dim strText As String
Dim strLabel As String
Dim strNewLabel As String
strLabel = "Chew"
strNewLabel = "Devour"
' Replace the field code values in the first field to change the label
strText = ActiveDocument.Fields(1).Code.Text
ActiveDocument.Fields(1).Code.Text = Replace(strText, strLabel, strNewLabel)
End Sub
This will basically do a search and replace inside the field code for the macrobutton field you want to change. ActiveDocument.Fields(1).Code.Text is the part I think you are looking for.

Searching a bound Combo Box for User Input

Alright Gents,
Im trying to search a vb.net combo box for an item. The combo box is already bound to dataset. The display member is set to display just a single column of the selected record. I had it set initially so the objects displayed in the combo were a customized class. In this class I specified all the properties I wanted to keep track of and that seemed to work well. However now that I am using the combo box in its bound state it is much more difficult to manipulate the data.
Mission:
To have the user type a number, if the number is contained in the ComboBox, the combo box should then move to that record so that all the other items bound to that control will update as well.
Research:
I have looked into the System.Windows.Forms.BingingManagerBase class and that seems to have the information I need. I just can't figure out the bridge between that and what I'm trying to do. I want to throw something together so I tried to simply do a SQL search against the dataset for the text in the combobox. Unfortunately that requires late binding and my targeted version of the .Net compact framework does not support that.
Here is an example of the late binding I was attempting. (Im working with VB.net 2005, Compact Framework 3.5 I believe:
For i as integer = 0 to combobox.items.count - 1
dim Dsr as Dataset.Row
dim dv as dataview
Dsr = DirectCase(Dv.row, Dataset.Row)
If Dsr(i).DesiredColumn = DesiredRow.Desiredcolumn then
'Do such and such code
End If
Next
I want to be able to search the dataset for a specific record matching a query. After I find that row matching the query I want to be able to move my combo box to the row found in the SQL query. The main problem seems to be that the Combo works in Datarowviews and my datasets are mostly cast to rows pertaining to the DS.
Anyone have some insight on this it would be much appreciated.
Thanks again!
If you know the item that should be set as selected in the combobox you can just set the ComboBox.SelectedItem Property
If you actually do really need to loop through all the items bound to the combobox then once you reach the correct one you can set the ComboBox.SelectedIndex Property.
For i As Integer = 0 To ComboBox.Items.Count - 1
Dim drv As System.Data.DataRowView = Nothing
Dim desiredColumn As String = String.Empty
drv = DirectCast(ComboBox.Items.Item(i), DataRowView)
desiredColumn = Convert.ToString(drv("Tag"))
Debug.WriteLine(desiredColumn)
Next
This seems to find the column value for every record in the combo box allowing me to find the correct index of the text I am searching for. Like I said though, if I could find a way to search through the list of items in the combobox without having to address each one, I would be grateful.

DataGridView and Combobox Column?

when DataGridView has a combobox column, how can I get the text it displays as oppose to the value it represents? When I do DGV.Item("cbo",i).Value I get the value but it won't take DGV.Item("cbo",i).Text. I trying Ctype(DGV.Item("cbo",i),ComboBox).Text and this does not work either.
Try
DGV.item("cbo",i).DisplayMember
Umm are you talking about Win Forms?
If so, Value is the property you want, and is what is to be displayed on the screen and held behind the scenes.
If you want something different not shown to the user, I've often used the property Tag for that.
I found this, and the answers didn't work for me. In case someone else finds this, here is what I did.
dgv.rows(i).Cells(cboname.index).EditedFormattedValue
Hope if someone finds this through Google it will help them.
Dim dgvcmbcell As DataGridViewComboBoxCell = DgvItemsUnits.Item("UNIT_SER", 0)
Dim SelectedText As String = dgvcmbcell.EditedFormattedValue.ToString()