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!
Related
The input box is "not declared".
Not sure exactly why this happening and I can't figure it out.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Do
f = InputBox("Inputs", "Enter The name Of the File.")
If f = Nothing Or f = "" Then
MessageBox.Show("Ooops!! No file name entered.")
Else
Exit Do
End If
Loop
End Sub
It appears that you didn't initialize the f variable:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Do
Dim f As String = InputBox("Inputs", "Enter The name Of the File.")
...
Loop
End Sub
Have you initialized the form using InitializeComponent() in the constructor?
Other than that, this (more concise) code works as expected for me:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Do
If Not String.IsNullOrEmpty(InputBox("Inputs", "Enter the name of the file.")) Then Exit Do
MessageBox.Show("Oops!! No file name entered.")
Loop
End Sub
I'd check your references. And turn Option Strict On in any case to highlight any type/declaration issues.
I have orders in text files in the debug folder and when i type the name of the order in a text box it displays the order is a list box and there is a combo box underneath where i can change the status of the meal preparation (Being prepared, ready to deliver etc,). If i go back to that form and type in the same order name into the textbox i need to previous prepartion status to be already in the textbox. Thanks for any help!
Public Class frmOrderStatus
Private Sub btnStatus_Click(sender As Object, e As EventArgs) Handles btnStatus.Click
Dim sr As IO.StreamReader = IO.File.OpenText(strTxtOrderNum & ".txt")
Do Until sr.EndOfStream
lstOrder.Items.Add(sr.ReadLine)
Loop
End Sub
Private Sub OrderStatus_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstOrder.Items.Clear()
btnStatus.Enabled = False
ChangeStatus.Enabled = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnValidate.Click
strTxtOrderNum = txtOrderNum2.Text
btnStatus.Enabled = True
ChangeStatus.Enabled = True
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
strSaveStatus = ChangeStatus.SelectedIndex
End Sub
Private Sub ChangeStatus_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ChangeStatus.SelectedIndexChanged
End Sub
End Class
It recognizes the file; it just tells you it is in use. A Stream must be closed and disposed. I don't see the StreamReader even being closed let alone disposed. A `Using...End Using block will close and dispose of objects even if there is an error.
I just used a text file I happened to have to test.
Private strTxtOrderNum As String = "host"
Private Sub ReadFile()
Using sr As IO.StreamReader = IO.File.OpenText(strTxtOrderNum & ".txt")
Do Until sr.EndOfStream
ListBox1.Items.Add(sr.ReadLine)
Loop
End Using
End Sub
Private Sub WriteFile()
Dim strSelectedItem = ComboBox1.Text
Using swVar As IO.StreamWriter = IO.File.AppendText(strTxtOrderNum & ".txt")
swVar.WriteLine(strSelectedItem)
End Using
End Sub
Dim dir = "..//Football/"
Private Sub FTablebutton_Click(sender As Object, e As EventArgs) Handles FTablebutton.Click
For Each file As String In System.IO.Directory.GetFiles(dir)
FfilesComboBox.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file))
Next
End Sub
Private Sub FfilesComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles FfilesComboBox.SelectedIndexChanged
Dim openfile As String = System.IO.Path.Combine(dir, FfilesComboBox.SelectedItem.ToString)
'start the process using the openfile string
Process.Start(openfile)
End Sub
I am able to add all the files to combobox but the problem is i cannot open the file when selected from the combobox
Try this
Private Sub FTablebutton_Click(sender As Object, e As EventArgs) Handles FTablebutton.Click
For Each file As String In System.IO.Directory.GetFiles(dir)
FfilesComboBox.DisplayMember = "key"
FfilesComboBox.ValueMember = "value"
FfilesComboBox.Items.Add(New DictionaryEntry(System.IO.Path.GetFileNameWithoutExtension(file), System.IO.Path.GetFileName(file)))
Next
End Sub
Private Sub FfilesComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles FfilesComboBox.SelectedIndexChanged
Dim openfile As String = System.IO.Path.Combine(dir, FfilesComboBox.SelectedItem.Value.ToString)
'start the process using the openfile string
Process.Start(openfile)
End Sub
If you Then use Visual studio 2008 or newer. you can use Anonymous class to store the full file path with the FileNameWithoutExtension.
Dim dir = "..//Football/"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FfilesComboBox.DisplayMember = "Text"
End Sub
Private Sub FTablebutton_Click(sender As Object, e As EventArgs) Handles FTablebutton.Click
For Each file As String In System.IO.Directory.GetFiles(Dir)
FfilesComboBox.Items.Add(New With {.Text = System.IO.Path.GetFileNameWithoutExtension(file), .Value = file})
Next
End Sub
you can use ComboBox1.SelectedItem.Value to get the value (that is the file full path)
Private Sub FfilesComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles FfilesComboBox.SelectedIndexChanged
'start the process using the openfile string
Process.Start(FfilesComboBox.SelectedItem.Value)
End Sub
This will work even you choose to loop over sub directories.
This code is tested and worked fine
I have a TextBox called TextBox1, which is filled by a Button that gets a file path using OpenFileDialog. I want a button (Button3) to start several processes one after another with an interval of 2 hours then close it and open the next one.
In total I have 4 different TextBoxes (TextBox1, TextBox2, TextBox3 and TextBox4) and 4 different file paths that I want to open with the same button with the interval I mentioned before.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
OpenFileDialog1.Title = "Please Select a File"
OpenFileDialog1.InitialDirectory = "C:temp"
OpenFileDialog1.ShowDialog()
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog1.OpenFile()
TextBox1.Text = OpenFileDialog1.FileName.ToString()
If Not (strm Is Nothing) Then
'insert code to read the file data
strm.Close()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
OpenFileDialog2.Title = "Please Select a File"
OpenFileDialog2.InitialDirectory = "C:temp"
OpenFileDialog2.ShowDialog()
End Sub
Private Sub OpenFileDialog2_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog2.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog2.OpenFile()
TextBox3.Text = OpenFileDialog2.FileName.ToString()
If Not (strm Is Nothing) Then
'insert code to read the file data
strm.Close()
End If
End Sub
Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
End Sub
End Class
To launch a new process use:
dim myProcess = Process.start(filename)
You dont' need the code that does the following:
Dim strm As System.IO.Stream
strm = OpenFileDialog2.OpenFile()
If Not (strm Is Nothing) Then
strm.Close()
End If
This is opening the exe file as if it were trying to read the data from it.
Instead just use
dim process = Process.Start(OpenFileDialogX.Filename)
Note: your initial directory seems to be c:temp not c:\temp as it probably should be
I'm very frustrated trying to get my code to work.
I'm trying to have a selected item in a listbox removed also in the textbox.
Getting ready to remove text;
Removed the text;
But it's still in the textbox.
Here is my code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox2.Text += TextBox1.Text & vbNewLine
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
'
'//HOW TO REMOVE THE SELECTED TEXT IN THE LISTBOX ALSO REMOVED IN THE TEXTBOX2??
'
'
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim filenames As String = "C:\log\log.txt"
My.Computer.FileSystem.WriteAllText(filenames, TextBox2.Text, False)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filenames As String = "C:\log\log.txt"
If My.Computer.FileSystem.FileExists(filenames) Then
TextBox2.Text = My.Computer.FileSystem.ReadAllText(filenames)
Dim items()
items = TextBox2.Lines()
For Each item In items
ListBox1.Items.Add(item)
Next
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Clipboard.SetText(ListBox1.SelectedItem)
End Sub
End Class
The worst part is that every time I try to look it up online, there are no errors until I click the button that says 'Value Cannot Be Null'
It happened every single time.
Please, before you mash the -1 button, at least tell me why. I'm new to this.
This should work for you
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.Replace(ListBox1.Items(ListBox1.SelectedIndex), Nothing)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
End Class