How to determine if a value of a key exist in my settings vb.net - vb.net

i have start to develop a new application to generate md5 hashes from strings and the it save automatic to my.settings.md5_hashes
and now i need to check if a certain value all ready exist in my settings md5_hashes. i have find some examples but it give me all ways a error on it
the error
Error 1 Value of type 'System.Collections.Specialized.StringCollection' cannot be converted to 'String'.
how can i check if a value its all ready exist on my settings?
This is my code
Public Class Form3
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If My.Settings.md5_hashes = (TextBox1.Text) Then
End If
If Not My.Settings.md5_hashes = (TextBox1.Text) Then
Me.Show()
End If
End Sub
End Class
This is the code to generate the hashes and save it to my.settings.md5_hashes
Imports System.Text
Imports System.Security.Cryptography
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
My.Settings.md5_hashes.Clear()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim md5 As MD5 = System.Security.Cryptography.MD5.Create()
Dim inputBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
Dim hash As Byte() = md5.ComputeHash(inputBytes)
Dim sb As New StringBuilder()
For i As Integer = 0 To hash.Length - 1
sb.Append(hash(i).ToString("x2"))
Next
TextBox2.Text = sb.ToString
ListBox1.Items.Add(TextBox1.Text + "<--->" + TextBox2.Text)
My.Settings.md5_hashes.Add(TextBox1.Text + "<--->" + TextBox2.Text)
My.Settings.Save()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each item In My.Settings.md5_hashes
ListBox1.Items.Add(item)
Next
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox1.Text = ""
TextBox2.Text = ""
ListBox1.Items.Clear()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Form2.Show()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim W As IO.StreamWriter
Dim i As Integer
W = New IO.StreamWriter("C:\MD5.txt", True)
For i = 0 To ListBox1.Items.Count - 1
W.WriteLine(ListBox1.Items.Item(i))
Next
W.Close()
MsgBox("You File Is Save You Can Locate It At C:\MD5.txt")
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Form3.Show()
End Sub
End Class
well i have just change some part of the code
this is my new code
Imports System.Text
Imports System.Security.Cryptography
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'My.Settings.md5_hashes.Clear()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim md5 As MD5 = System.Security.Cryptography.MD5.Create()
Dim inputBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
Dim hash As Byte() = md5.ComputeHash(inputBytes)
Dim sb As New StringBuilder()
For i As Integer = 0 To hash.Length - 1
sb.Append(hash(i).ToString("x2"))
Next
TextBox2.Text = sb.ToString
ListBox1.Items.Add(TextBox1.Text)
ListBox1.Items.Add(TextBox2.Text)
My.Settings.md5_hashes.Add(TextBox1.Text)
My.Settings.md5_hashes.Add(TextBox2.Text)
My.Settings.md5_hashes.Add("<--->")
My.Settings.Save()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each item In My.Settings.md5_hashes
ListBox1.Items.Add(item)
Next
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox1.Text = ""
TextBox2.Text = ""
ListBox1.Items.Clear()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Form2.Show()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim W As IO.StreamWriter
Dim i As Integer
W = New IO.StreamWriter("C:\MD5.txt", True)
For i = 0 To ListBox1.Items.Count - 1
W.WriteLine(ListBox1.Items.Item(i))
Next
W.Close()
MsgBox("You File Is Save You Can Locate It At C:\MD5.txt")
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Form3.Show()
End Sub
End Class
well now its return true but how can i get the value to show on a msgbox the results?
well i solved my self the way to get results of the name for example if i search with the name to retrieved the hash , but how to do if i search for a hash and get the name how to take the name i stored initial there ?

Use the Contains method:
If My.Settings.md5_hashes.Contains(TextBox1.Text) = True Then
'Hash exists, do something.
Else
'Hash does not exist, do something else.
End If
Tip: Check what members IntelliSense shows you when you type My.Settings.md5_hashes. (notice the dot on the end). By doing so you would most likely have found the answer to this yourself.
EDIT:
In your specific case it gets a little more complicated because you must separate the input and the hash before you can check that the hash exists.
Try this (this is for the first code you posted in the question):
Private Function HashExists(ByVal Hash As String) As Boolean
For Each Entry As String In My.Settings.md5_hashes 'Iterate through every hash entry.
Dim Parts As String() = Entry.Split(New String() {"<--->"}, 2, StringSplitOptions.RemoveEmptyEntries) 'Split the entry on "<--->".
If Parts.Length = 1 AndAlso Parts(0) = Hash Then
Return True 'No "<--->" was present, but the entire entry matches 'Hash'.
ElseIf Parts.Length >= 2 AndAlso Parts(1) = Hash
Return True 'The entry was split successfully and the second part and 'Hash' are equal.
End If
Next
Return False 'No valid hash found.
End Function
Then use this like:
If HashExists(TextBox1.Text) = True Then
'Hash exists, do something.
Else
'Hash does not exist, do something else.
End If
EDIT 2:
To get the name from a hash or a hash from a name the above code just need to be modified slightly to return any of the two parts.
Private Function GetEntry(ByVal Input As String, ByVal NameFromHash As Boolean) As String
For Each Entry As String In My.Settings.md5_hashes 'Iterate through every hash entry.
'Parts(0) is the name.
'Parts(1) is the hash.
Dim Parts As String() = Entry.Split(New String() {"<--->"}, 2, StringSplitOptions.RemoveEmptyEntries) 'Split the entry on "<--->".
If Parts.Length >= 2 Then
If NameFromHash = True AndAlso Parts(1) = Input Then
Return Parts(0) 'Input was a valid hash, return name.
ElseIf NameFromHash = False AndAlso Parts(0) = Input Then
Return Parts(1) 'Input was a valid name, return hash.
End If
End If
Next
Return Nothing 'No valid entry found.
End Function
Usage:
'Get the name from a hash.
Dim Name As String = GetEntry(TextBox1.Text, True)
'Get the hash from a name.
Dim Hash As String = GetEntry(TextBox1.Text, False)
If the function returns Nothing that means it couldn't find the name/hash you specified. To avoid getting a NullReferenceException you can check so in an If-statement:
'Name from hash.
If Name IsNot Nothing Then
'The specified hash was found, do something with the name.
Else
'Hash not found.
MessageBox.Show("The specified hash was not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
'Hash from name.
If Hash IsNot Nothing Then
'The specified name was found, do something with the hash.
Else
'Name not found.
MessageBox.Show("The specified name was not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If

Related

Saving and reading files on Visual basic

Hi I'm creating a "Toilet paper tracker" on visual basic and I'm struggling with saving and reading files, I know I am missing stuff. The user should be able to login and input a threshold and when reached a warning will pop up saying "buy more toilet paper" (i haven't coded this yet) and the user can add to create a total and subtract from it too. The user should also be able to save the total to a file and I want the program to be able to read the file and change the total if the user wants to add or subtract again. It would be greatly appreciated if you pointed me in the right direction, I'm only young so it's relatively simple. Here is my program :)
Imports System.IO
Public Class frmTPT
Private Sub TPT_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call loadchangetotal()
End Sub
Sub loadchangetotal()
cboChange.Items.Add("Add to Total")
cboChange.Items.Add("Subtract from Total")
End Sub
Private Sub cboVenue_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboChange.SelectedIndexChanged
If cboChange.Text = "Add to Total" Then
Dim frmChangeACopy As New frmChangeA
frmChangeACopy.Show()
Me.Hide()
ElseIf cboChange.Text = "Subtract from Total" Then
Dim frmChangeSCopy As New frmChangeS
frmChangeSCopy.Show()
Me.Hide()
End If
End Sub
Private Sub btnReturn_Click(sender As Object, e As EventArgs)
Dim frmLoginCopy As New frmLogin
frmLoginCopy.Show()
Me.Hide()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtThreshold.Text = ""
cboChange.Text = ""
txtTotal.Text = ""
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
End
End Sub
Private Sub LogoutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LogoutToolStripMenuItem.Click
Dim frmLoginCopy As New frmLogin
frmLoginCopy.Show()
Me.Hide()
End Sub
Private Sub btnReadTotal_Click(sender As Object, e As EventArgs) Handles btnReadTotal.Click
Dim FileReader As StreamReader
Dim result As DialogResult
result = OpenFileDialog1.ShowDialog
If result = DialogResult.OK Then
FileReader = New StreamReader(OpenFileDialog1.Filename)
txtFileContent.Text = FileReader.ReadToEnd() 'i want to be able to read a
'previously saved total so that
FileReader.Close() 'it can be used to find the new total
'after it has been added to
End If 'or subtratced
End Sub
Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
Call SaveFile()
End Sub
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim A, S, NewTotal As Integer
A = Val(frmChangeA.txtAdd.Text)
S = Val(frmChangeS.txtSubtract.Text)
NewTotal = A - S 'I want to be able to load a previously saved total if one exists and add and
'subtract from it
End Sub
End Class
Sub SaveFile()
Dim FileWriter As StreamWriter
Dim results As DialogResult
results = SaveFileDialog1.ShowDialog
If results = DialogResult.OK Then
FileWriter = New StreamWriter(SaveFileDialog1.FileName, False)
FileWriter.Write(txtFileContent.Text) ' is txtFileContent supposed to be
' the name of my textbox?
FileWriter.Close()
End If
End Sub
Design
You didn't mention if you were using .Net Core or 4.x. If the later, you can sometimes use the Insert Snippet functionality to learn how to do common tasks. For example in this case you could right click in the code editor and select Insert Snippet then Fundamentals then File System and finally Write text to a file. This will result in the following VB code:
My.Computer.FileSystem.WriteAllText("C:\Test.txt", "Text", True)
Unfortunately, this option doesn't work with .Net core since the My namespace wasn't ported to core.
The key point of this problem lies in reading and writing data from text. It is a clear idea to write two methods to achieve read and write.
You can refer to the following code. The two methods in the following example are WriteTotal(), ReadTotal().
Design:
Public Class Form1
Dim Oldtotal As Integer
Dim Newtotal As Integer
Private Sub btnLoadTotal_Click(sender As Object, e As EventArgs) Handles btnLoadTotal.Click
WriteTotal()
ReadTotal()
End Sub
Private Sub btnUpdateTotal_Click(sender As Object, e As EventArgs) Handles btnUpdateTotal.Click
cboChange.Text = Nothing
WriteTotal()
ReadTotal()
MsgBox("Inventory updated")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cboChange.SelectedIndex = 0
ReadTotal()
End Sub
Sub WriteTotal()
Using swriter As IO.StreamWriter = New IO.StreamWriter("D:/paperstore.txt", False, System.Text.Encoding.UTF8)
If cboChange.Text = "Add to Total" Then
Newtotal = Oldtotal + CType(txtThreshold.Text, Integer)
swriter.WriteLine(Newtotal)
ElseIf cboChange.Text = "Subtract from Total" Then
If CType(txtThreshold.Text, Integer) > Oldtotal Then
swriter.WriteLine(Oldtotal)
MsgBox("buy more toilet paper")
Else
Newtotal = Oldtotal - CType(txtThreshold.Text, Integer)
swriter.WriteLine(Newtotal)
End If
Else
swriter.WriteLine(txtTotal.Text)
End If
End Using
End Sub
Sub ReadTotal()
Using sreader As IO.StreamReader = New IO.StreamReader("D:/paperstore.txt", System.Text.Encoding.UTF8)
Try
Oldtotal = sreader.ReadLine()
txtTotal.Text = Oldtotal
Catch ex As Exception
MsgBox(ex.Message)
End
End Try
End Using
End Sub
End Class

Save text on my.settings saved multiple times vb.net

i have start a new application to encrypt strings and save them on my settings the code its work fine but wen it save it save multiple times the same.
How can i do for save all the content from a listbox to my settings with out to repeat.
This is my code
Imports System.Text
Imports System.Security.Cryptography
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'My.Settings.md5_hashes.Clear()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim md5 As MD5 = System.Security.Cryptography.MD5.Create()
Dim inputBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
Dim hash As Byte() = md5.ComputeHash(inputBytes)
Dim sb As New StringBuilder()
For i As Integer = 0 To hash.Length - 1
sb.Append(hash(i).ToString("x2"))
Next
TextBox2.Text = sb.ToString
ListBox1.Items.Add(TextBox1.Text + "<--->" + TextBox2.Text)
My.Settings.md5_hashes.Add(TextBox1.Text + "<--->" + TextBox2.Text)
My.Settings.Save()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each item In My.Settings.md5_hashes
ListBox1.Items.Add(item)
Next
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
For Each item In My.Settings.md5_hashes
ListBox1.Items.Add(item)
Next
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
End Class
Well i just solved my self the problem its because wen i start the timer its is there the code to retrieved all the content from my settings to the listbox.
I just removed and solved.

System.I.O the process cannot access the file because it is being used by another process

I'm designing a program that calculates the average of scores. the program works great except when I try to add a name and a number from textbox into the textfile it returns an error. I believe that I have closed the file every time it has been used. my code is:
Public Class Form4
Public Function GetNumberOfLines(ByVal file_path As String) As Integer
Using sr As New StreamReader("C:\file.txt")
Dim NumberOfLines As Integer
Do While sr.Peek >= 0 'a pre test loop to read lines
sr.ReadLine()
NumberOfLines += 1
Loop
Return NumberOfLines
sr.Close() 'closes file after it is read
sr.Dispose()
End Using
End Function
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Hide()
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
Dim FILE_NAME As String = "C:\file.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.Close()
objWriter.Write(TextBox5.Text)
MsgBox("Text written to file")
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Using sr As New System.IO.StreamReader("C:\file.txt")
Dim LofInt As New List(Of Integer)
While sr.Peek <> -1
Dim line As String = sr.ReadLine
Dim intValue As String = String.Empty
For Each c As Char In line
If IsNumeric(c) Then
intValue += c
End If
Next
LofInt.Add(intValue)
End While
sr.Close()
sr.Dispose()
LofInt.Sort()
For Each i In LofInt
Scores.Items.Add(i)
Next
Dim sum As Decimal
For Each decAdded As Decimal In Me.Scores.Items
sum += Double.Parse(decAdded)
Next
TextBox4.Text = sum
sr.Close()
sr.Dispose()
End Using
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox1.Text = My.Computer.FileSystem.ReadAllText("C:\file.txt")
TextBox2.Text = GetNumberOfLines("C:\file.txt")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sum As Decimal
Dim avg As Decimal
For Each decAdded As Decimal In Me.Scores.Items
sum += Double.Parse(decAdded)
Next
avg = sum / (GetNumberOfLines("C:\file.txt"))
TextBox3.Text = avg
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Close()
End Sub
Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs)
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
TextBox1.Clear()
TextBox2.Clear()
Scores.Items.Clear()
TextBox4.Clear()
TextBox1.Text = My.Computer.FileSystem.ReadAllText("C:\file.txt")
TextBox2.Text = GetNumberOfLines("C:\file.txt")
Using sr As New System.IO.StreamReader("C:\file.txt")
Dim LofInt As New List(Of Integer)
While sr.Peek <> -1
Dim line As String = sr.ReadLine
Dim intValue As String = String.Empty
For Each c As Char In line
If IsNumeric(c) Then
intValue += c
End If
Next
LofInt.Add(intValue)
End While
LofInt.Sort()
For Each i In LofInt
Scores.Items.Add(i)
Next
Dim sum As Decimal
For Each decAdded As Decimal In Me.Scores.Items
sum += Double.Parse(decAdded)
Next
TextBox4.Text = sum
sr.Close()
sr.Dispose()
End Using
End Sub
Dim objWriter As New System.IO.StreamWriter(FILE_NAME) is where the error occurs

How do I store a text file inside a VB.net project?

I have built a basic VB app in the latest visual studio community. It's a simple app to load a text file into a list box then allow the list to be filtered down and finally the selected value to be copied.
It all works fine but I am curious, if I want to distribute this to other users I need to send them the text file (and the location is currently hard coded).
There has to be a better way to do this, do I need to import the text file as some sort of object in my project so it is then part of the project as opposed to a text file on its own?
Here is my code:
Public Class Form1
Dim MyArray() As String = My.Computer.FileSystem.ReadAllText("C:\Temp\Products.txt").Split(Environment.NewLine)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListBox1.DataSource = MyArray
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
TextBox2.Text = ListBox1.SelectedValue
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Clipboard.Clear()
Clipboard.SetText(TextBox2.Text)
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim FilteredArray(0) As String
Dim ProdName As String
Dim X As Long = 0
ListBox1.DataSource = MyArray
For Each ProdName In ListBox1.Items
If InStr(UCase(ProdName), UCase(TextBox1.Text)) > 0 Then
ReDim Preserve FilteredArray(X)
FilteredArray(X) = ProdName
X = X + 1
End If
Next
ListBox1.DataSource = FilteredArray
End Sub
End Class
Any help is appreciated.
For completeness, here is my final solution:
Imports System.IO
Imports System.Reflection
Public Class Form1
Dim MyArray() As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using sr As StreamReader = New StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsApplication2.Products.txt"))
MyArray = Split(sr.ReadToEnd(), vbLf)
End Using
ListBox1.DataSource = MyArray
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
TextBox2.Text = ListBox1.SelectedValue
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Clipboard.Clear()
Clipboard.SetText(TextBox2.Text)
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim FilteredArray(0) As String
Dim ProdName As String
Dim X As Long = 0
ListBox1.DataSource = MyArray
For Each ProdName In ListBox1.Items
If InStr(UCase(ProdName), UCase(TextBox1.Text)) > 0 Then
ReDim Preserve FilteredArray(X)
FilteredArray(X) = ProdName
X = X + 1
End If
Next
ListBox1.DataSource = FilteredArray
End Sub
End Class
I think is a good way to store such file is resources of assembly.
Include your file into project. Set Build Action to Embedded Resource and then this file will be store inside the assembly.
How to read resource file from assembly you can find here: How to read embedded resource text file

clear combobox text entered in text box portion

I created a simple program that reads and writes to an output file in the bin folder, it works almost perfect. btnRemove deletes the selected item in cboFriends(which is good). However, I also need btnRemove to delete text entered in the text box portion. How do i do this? I apologize in advance for the basicness of the question.
Public Class frmMain
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub frmMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim outFile As IO.StreamWriter
outFile = IO.File.CreateText("MyFriends.txt")
For intIndex As Integer = 0 To cboFriends.Items.Count - 1
outFile.WriteLine(cboFriends.Items(intIndex))
Next intIndex
outFile.Close()
End Sub
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim inFile As IO.StreamReader
Dim strInfo As String
If IO.File.Exists("MyFriends.txt") Then
inFile = IO.File.OpenText("MyFriends.txt")
Do Until inFile.Peek = -1
strInfo = inFile.ReadLine
cboFriends.Items.Add(strInfo)
Loop
inFile.Close()
End If
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
If cboFriends.Items.Contains(cboFriends.Text) Then
Else
cboFriends.Items.Add(cboFriends.Text())
End If
End Sub
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
cboFriends.Items.Remove(cboFriends.Text)
End Sub
End Class
It appears you are looking for the SelectedText property
To set it to a blank string, do the following
cboFriends.SelectedText = ""
cboFriends.SelectedText would work if the text was selected, but if i type "asdfjkl;" and then press [Remove] it does nothing.
Upon further digging cboFriends.Text = "" does the trick!