I have the following code
Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
textbox = New TextBox
textbox.Size = New Size(50, 50)
If Controls.Count > 0 Then
TextBox.Top = Controls(Controls.Count - 1).Bottom
End If
Me.SplitContainer2.Panel1.Controls.Add(textbox)
End Sub
What that does is add instances of textbox one after the other.
The next one writes a text file to my system.
Private Sub Button6_Click_1(sender As System.Object, e As System.EventArgs) Handles Button6.Click
Dim FILE_NAME As String = "C:\Users\Pantheo\test2.txt"
If File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.Write(textbox.Text)
objWriter.Close()
MsgBox("Text written to file")
Else
MsgBox("File Does Not Exist")
End If
End Sub
However it only writes the value of the last control.
For example if i have 2 textboxes created, by clicking 2 times the Button5, the test2.txt contains the .Text value from the second textbox only and not the first
Is there a way to write the .Text value from every textbox, to the same text file?
what you're doing if i see this right is every time you click button5 making a new instance on your textbox ( new TextBox) that for sure will only hold the last instance which was generated with it. Try adding you're instances to a collection and then iterate through them
Related
I have a text file(its hosts file)
I can add lines(domains) to the file through a text box.
When i click the 'Save' button, inputted value(domain) saves to the text(hosts) file with a 127.0.0.1 prefix. At the same time it shows that in a list box too(while showing it in list box, it won't show the prefix(127.0.0.1). It'll only show the value got from the text field)
What i need now is that,
When i click and select an item from the list box and press 'Remove' button, it needs to be removed from both the list box and the text(hosts) file.
Also, while removing the selected item from the list box, the prefix(127.0.0.1) also should be removed from the file without leaving an empty line.
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles BtnCustomDomainremove.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
Dim delLine As Integer = 1
Dim lines As List(Of String) = System.IO.File.ReadAllLines("C:\Windows\System32\drivers\etc\hosts").ToList
lines.RemoveAt(delLine - 1)
System.IO.File.WriteAllLines("C:\Windows\System32\drivers\etc\hosts", lines)
MessageBox.Show("URL removed from Block List !", "Custom Block List")
End Sub
Above code it removes the top value from file and selected one from list box.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lines As List(Of String) = System.IO.File.ReadAllLines("C:\Windows\System32\drivers\etc\hosts").ToList
Dim Findstring = IO.File.ReadAllText("C:\Windows\System32\drivers\etc\hosts")
Dim Lookfor As String = CStr("127.0.0.1" + ListBox1.SelectedItem)
If lines.Contains(Lookfor) Then
For i = 0 To Findstring.Length - 1
lines.Remove(Lookfor)
Next
System.IO.File.WriteAllLines("C:\Windows\System32\drivers\etc\hosts", lines)
ListBox1.Items.Remove(ListBox1.SelectedItem)
MessageBox.Show("URL removed from Block List !", "Custom Block List")
Else
MsgBox("string not found")
End If
End Sub
This code works for me, you might have to tweak it slightly to match your prefereneces however, it seems to find each occurunce (if there's more than one) of the selected item in listbox1 (if there's only one occurance you can remove the for loop.
Instead of Reading and Writing the file on every change to the list, save the list when the form closes. Read the file when the form opens and populate the list. While the form is open just add and remove list items as you normally would.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim path = "C:\Windows\System32\drivers\etc\hosts.txt"
Dim lines() As String = File.ReadAllLines(path)
'Substring(9) the characters in "127.0.0.1" are index 0-8 so we begin with index 9
'and return the rest of the string.
Dim TrimmedList = From line In lines
Select line.Substring(9)
For Each item In TrimmedList
ListBox2.Items.Add(item)
Next
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Dim sb As New StringBuilder()
For Each item In ListBox2.Items
sb.AppendLine("127.0.0.1" & item.ToString)
Next
Dim path = "C:\Windows\System32\drivers\etc\hosts.txt"
File.WriteAllText(path, sb.ToString)
End Sub
Requires
Imports System.IO
Imports System.Text
try this code
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles BtnCustomDomainremove.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
Dim details As String = ""
For Each o As Object In ListBox1.Items
details += o.ToString + vbNewLine
Next
My.Computer.FileSystem.WriteAllText("C:\Windows\System32\drivers\etc\hosts", details, False)
MessageBox.Show("URL removed from Block List !", "Custom Block List")
End Sub
I have start this application every things its work fine but i have a small bug but i can not find the solution to solve the error.
i have debug it and the error its because the file not exist
is there any way to me to populate my datagridview with all *.gif images From a directory and the check if its null or some thing like that.
What i mean is is there any way to my to populate all gif images found on the chose Directory?
in fact i have all ready try like this but i get one error "Provided column already belongs to the DataGridView control.
Well finaly i have found a solution to load all images from a directory to a datagridview programmatic
Here Is The Working Code
Public Class Form5
Private Sub addBtn_Click(sender As Object, e As EventArgs) Handles addBtn.Click
'Populate()
ShowImages()
End Sub
'CLEAR DATAGRIDVIEW
Private Sub clearBtn_Click(sender As Object, e As EventArgs) Handles clearBtn.Click
DataGridView1.Rows.Clear()
End Sub
'WHEN AN IMAGE IS CLICKED
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
MessageBox.Show("You Clicked Image At Col: " + e.ColumnIndex.ToString() + " Row: " + e.RowIndex.ToString())
End Sub
Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Public Sub ShowImages()
Dim directory As New System.IO.DirectoryInfo("C:\avitogifconverter\")
If directory.Exists Then
Dim pngFiles() As System.IO.FileInfo = directory.GetFiles("*.gif")
For Each pngFile As System.IO.FileInfo In pngFiles
If pngFile.Exists Then
Dim image = System.Drawing.Image.FromFile(pngFile.FullName)
Using image
Dim count = 1
' do something with the image like show in picture box
'CONSTRUCT IMG COLUMN
Dim imgCol As DataGridViewImageColumn = New DataGridViewImageColumn()
imgCol.HeaderText = "Photo"
imgCol.Name = "Col 1"
DataGridView1.Columns.Add(imgCol)
'CONSTRUCT ROWS
'FIRST ROW
Dim img As Image = System.Drawing.Image.FromFile(pngFile.FullName)
Dim row As Object() = New Object() {img, img, img}
DataGridView1.Rows.Add(row)
End Using
End If
Next
End If
End Sub
End Class
Using Directory.EnumerateFiles, you could do something like this:
Dim row = New List(Of Image)(3)
For Each filename In Directory.EnumerateFiles("C:\avitogifconverter", "*.gif")
row.Add(Image.FromFile(filename))
If row.Count = 3 Then
DataGridView1.Rows.Add(row.ToArray())
row.Clear()
End If
Next
If row.Count > 0 Then
DataGridView1.Rows.Add(row.ToArray())
row.Clear()
End If
I've been playing around learning the ropes of VB.NET. I have started making my own small generator which just generates txt on the left hand side listbox based on whatever the user chooses in the combo box as you can see in the picture. I would like to configure the already Save Results to open a file dialog box and allow the user to say what is already in the listbox in a txt file in any directory he or she choose. Please look at my VB Program picture below to get a better understanding
My VB program
Imports System.IO
Public Class Form1
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ProgressBar1.Value = ProgressBar1.Value + 60 'change this to incrase green bar'
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' declare the required variables
Dim i As Integer = 0
Dim item As String
Dim fileReader As StreamReader
' set the value min and max values of progress bar
' clear the contents of the ListBox1
ListBox1.Items.Clear()
' check whether an item is selected in the combox1
If ComboBox1.SelectedIndex <> -1 Then
' is selected get the item
item = ComboBox1.SelectedItem.ToString()
Else
' otherwise set item to empty
item = ""
End If
' depending on the item initialize the fileReader with
' with respective file name
If item.Equals("Random") Then
fileReader = New StreamReader("world mix.txt")
ElseIf item.Equals("China") Then
fileReader = New StreamReader("china.txt")
ElseIf item.Equals("Korea") Then
fileReader = New StreamReader("korea.txt")
ElseIf item.Equals("United Arab Emirates") Then
fileReader = New StreamReader("ae.txt")
ElseIf item.Equals("Russia") Then
fileReader = New StreamReader("ru.txt")
ElseIf item.Equals("USA") Then
fileReader = New StreamReader("usa lead.txt")
ElseIf item.Equals("New Zeland") Then
fileReader = New StreamReader("nz.txt")
Else
fileReader = New StreamReader("names.txt")
End If
' loop till end of the file
Do While fileReader.Peek() <> -1
' check whether the progress is less than 200
' if so just increment the progress bar value
If ProgressBar1.Value <= 100 Then
' set the line that is read into the listBox
ListBox1.Items.Add(fileReader.ReadLine().ToString())
' increment the iterator
End If
Loop
' close the file
fileReader.Close()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' when stop button is clicked, just display a pop up message
MessageBox.Show("The progress bar has stopped!")
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
''This is the save results button button''
End Sub
End Class
I'm creating a basic To-do program. I want to save the list of tasks the user creates. Now I've tried with saving them as text files but I don't want it that way. I want it so that I can save the tasks the user creates within the program rather than external text files and then retrieve those saved files and display them in a text file.
Essentially I need a way to save data without needing to rely on databases.
A good example is GeeTeeDee. It seems to be saving its files and data etc. in the program within rather than external text file.(I'm assuming this because I can't seem find them. I could be wrong)
Update
I was doing a bit of searching can came across this: Click here!!!
But the problem is that I'm confused as to how this works. Is someone able to clear things for me? It would be GREATLY appreciated as it's exactly what I'm looking for.
The "code project" example saves the data at an external file with extension [*.brd] .
You can Use XmlSerializer to save and load your data from external xml file with extension xml,brd or anything else.
Try the code below, add into a form1 three buttons (Button1,Button2,Button3) and a DataGridView1, paste the code and Run.
press button "add data dynamically" or/and add,edit,delete row directlly from DataGridView1.
press Save data.
close and run programm
press Load data.
Imports System.Xml.Serialization
Imports System.IO
Class Form1
Dim ds As DataSet
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Button1.Text = "Load Data"
Button2.Text = "add data dynamically"
Button3.Text = "Save Data"
'Create Dataset
ds = CreateDataset()
'Set DataGridView1
DataGridView1.DataSource = ds.Tables("Person")
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
LoadFromXMLfile("c:\temp\persons.xml")
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
AddDataToDataSetDynamically(ds)
End Sub
Private Sub Button3_Click_1(sender As System.Object, e As System.EventArgs) Handles Button3.Click
SaveToXMLFile("c:\temp\persons.xml", ds)
End Sub
Private Function CreateDataset() As DataSet
Dim dataset1 As New DataSet("Persons")
Dim table1 As New DataTable("Person")
table1.Columns.Add("Id")
table1.Columns.Add("FName")
table1.Columns.Add("Age")
'...
dataset1.Tables.Add(table1)
Return dataset1
End Function
Private Sub AddDataToDataSetDynamically(d As DataSet)
d.Tables("Person").Rows.Add(1, "Andrew", "46")
d.Tables("Person").Rows.Add(2, "Nicky", "43")
d.Tables("Person").Rows.Add(3, "Helen", "15")
End Sub
Private Sub SaveToXMLFile(filename As String, d As DataSet)
Dim ser As XmlSerializer = New XmlSerializer(GetType(DataSet))
Dim writer As TextWriter = New StreamWriter(filename)
ser.Serialize(writer, d)
writer.Close()
End Sub
Private Sub LoadFromXMLfile(filename As String)
If System.IO.File.Exists(filename) Then
Dim xmlSerializer As XmlSerializer = New XmlSerializer(ds.GetType)
Dim readStream As FileStream = New FileStream(filename, FileMode.Open)
ds = CType(xmlSerializer.Deserialize(readStream), DataSet)
readStream.Close()
DataGridView1.DataSource = ds.Tables("Person")
Else
MsgBox("file not found! add data and press save button first.", MsgBoxStyle.Exclamation, "")
End If
End Sub
End Class
add that code to form1 and get the data to a textbox (add button4,textbox1)
Private Function PrintRows(dataSet As DataSet) As String
Dim s As String = ""
Dim thisTable As DataTable
For Each thisTable In dataSet.Tables
Dim row As DataRow
For Each row In thisTable.Rows
Dim column As DataColumn
For Each column In thisTable.Columns
s &= row(column) & " "
Next column
s &= vbCrLf
Next row
Next thisTable
Return s
End Function
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
TextBox1.Text = PrintRows(ds)
End Sub
I’m making a form with a checkbox where if checked a target file is appended with text-A and if Unchecked the file is appended with text-B
How can I do this?
what i have sofar:
Private Sub CheckBox1_Checked(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.Checked
Dim FILE_NAME As String = "C:\ANSWERSLIST.txt"
'Adding items for AutoCAD 2006...
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
objWriter.WriteLine("YES")
objWriter.Close()
End If
End Sub
You could use the AppendAllText method:
If MyCheckBox.IsChecked Then
File.AppendAllText("foo.txt", "text-A")
Else
File.AppendAllText("foo.txt", "text-B")
End If