vb.net write text to file ussing radio button - vb.net

Hi I would like to know how i can write to the following file C:test.txt a specific string of text like "LANDSCAPE" if a radiobutton is clicked?

If you simply want to create a file containing nothing but the text "LANDSCAPE", and you want to overwrite the file by that name if it already exists, then all you need to do is:
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked Then
File.WriteAllText("C:\test.txt", "LANDSCAPE")
End If
End Sub

Add the file writing code to checked change event of the radio button.
Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
'Write to file
End If
End Sub

Related

How to change buttons on a msgbox using a combo box in visual basic 2010 express?

I want to display a msgbox that contains information provided by a combo box. Specifically, if the combo box contains "Warning" I want the msgbox to display the warning icon.
Basically I need to know how to put the input from the combo box into the msgbox without having to make it have MsgBoxStyle.Critical or something like that.
What I thought would work:
Private Sub Button1_Click(ByVal sender As system.object, ByVal e As System.EventArgs) Handles Button1.Click
If ComboBox1.SelectedItem = "Warning" Then
ComboOutput = Msgboxstyle.critical
Hopefully my question is clear.
The following should work:
We load all the enum values on form load. Then on click we parse the name and display the message box.
Private Sub Form5_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ComboBox1.DataSource = [Enum].GetNames(GetType(MessageBoxIcon))
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim value = [Enum].Parse(GetType(MessageBoxIcon), CStr(ComboBox1.SelectedItem))
MessageBox.Show("Text", "Caption", MessageBoxButtons.OK, CType(value, MessageBoxIcon))
End Sub

Open another form when clicking an item in ListBox VB.net

I'm a network admin that have been task to do a paperless meeting system. So right now I'm looking at VB.net as my platform. My goal is populate a listbox with items inside a folder using their filenames, it's mostly PDF files. So i have achieve that goal and when I click the items in the listbox it the file in the folder opens. What I want is, if I click an item in the listbox, another form will show. This form has an embedded pdf reader and a textbox for comments. textbox will be save in a .txt file inside a another folder. here's my code
Public Class Form2
Dim MyFolderPath As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "C:\Users\ICTCAdmin\Desktop\Board Meeting\Academic")
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
For Each fullpath As String In IO.Directory.GetFiles(MyFolderPath)
ListBox1.Items.Add(IO.Path.GetFileName(fullpath))
Next
Catch ex As Exception
MsgBox(ErrorToString)
End Try
End Sub
Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Form1.Show()
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim fullpath As String = IO.Path.Combine(MyFolderPath, ListBox1.SelectedItem.ToString)
Process.Start(fullpath)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class
Thank you in advance.
In your second form create a Friend property like this at the top of the child form's code window
Public Class frmWhateverYourNewFormIsCalled
Friend Property filepath As String
In your calling form edit the listbox1_SelectedIndexChanged property to this
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim fullpath As String = IO.Path.Combine(MyFolderPath, ListBox1.SelectedItem.ToString)
dim childForm as new frmWhateverYourNextFormIsCalled
childForm.filepath = ""
childForm.Show()
End Sub
In your child form add in code to open the filePath in your embedded pdf reader. Probably in the Shown event handler. Not the Load event handler.
To view pdfs within your program this might help
Displaying a PDF in a control in Visual Basic 2010

creating then writing to a text file

I have button to create a text file and a textbox to write stuff to the text file when I press enter.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
path = "C:\Testing.txt"
File.Create(path)
End Sub
If e.KeyCode = Keys.Enter Then
System.IO.File.AppendAllText(path, TextBox1.Text & vbCrLf)
End If
The file is created properly but when I want to write to it using the code above, I get an error.
The process cannot access the file 'C:\Testing.txt' because it is being used by another process.
Change this line:
File.Create(path)
To this:
File.Create(path).Dispose()
However as stated before, you can cut all this out and simply use:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
path = "C:\Testing.txt"
If e.KeyCode = Keys.Enter Then
System.IO.File.AppendAllText(path, TextBox1.Text & vbCrLf)
End If
End Sub
As per MSDN: AppendAllText will "Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file."

Open a txt file when a button clicked in VB.NET

I have a log file in my project. This file is a text file (.txt). Is there a way to open this file when a button clicked without using the OpenFileDialog tool?
Note that I'm using VB.NET 2010.
Dim FILE_NAME As String = "C:\FileName.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Process.Start(FILE_NAME)
Else
MsgBox("File Does Not Exist")
End If
Here is a simple example:
Public Class OpenTextFile
Private Sub OpenTextFile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub OpenBUT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenBUT.Click
'OpenBUT_Click the text file
Process.Start("C:\File Name.txt")
End Sub
End Class

How can i search using a Text box and Listbox in vb.Net?

give me code, in which i can enter a word in a textbox and a listbox appear with a item that has same string in which i enter in a textbox.
Please Help me...
I found the following via Google, which sound like the type of things you want to do:
Autosearch ListBox in VB.NET
(WinForms)
Search Listboxes as You Type
(WinForms or is this VB6?)
Searching for items in a ListBox
(WPF)
Using # 1, here is some code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
List1.Items.Add("Computer")
List1.Items.Add("Screen")
List1.Items.Add("Modem")
List1.Items.Add("Printer")
List1.Items.Add("Scanner")
List1.Items.Add("Sound Blaster")
End Sub
Private Sub Text1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Text1.TextChanged
Dim i As Integer = List1.FindString(Text1.Text)
List1.SelectedIndex = i
If Text1.Text = "" Then
List1.SelectedIndex = -1
End If
End Sub
Think pseudocode, you can do this.
Grab the text from the textbox.
Set a pointer / counter to the listbox and loop through each item until the end of the list. If the textbox value has the same value as the listboxitem.text then you've found a match exit the for loop.
Add this code to texboxchange
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
ListBox1.SelectedIndex = ListBox1.FindString(TextBox1.Text.Trim)
End Sub