试图添加一个加载按钮 [英] trying to add a load button
问题描述
我正在进行事件提醒,除了加载按钮之外,我都可以正常工作.保存按钮会将数据从DataGridView保存到文本文件,现在我需要做的是编写某种程序来读取该文件并知道 将数据放在何处.我非常困惑,很可能对整个过程都没有考虑.这是我的代码:
I am making an event reminder and I have everything working except for the load button. The save button will save the data from the DataGridView to a text file and now what I need to do is somehow write a program that will read through that file and know where to put the data. I am super confused and most likely overthinking this whole process. This is the code I have:
Public Class MainForm1
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If DataGridView1.Columns(e.ColumnIndex).Name = "Delete" AndAlso Me.DataGridView1.Rows(e.RowIndex).IsNewRow = False Then
Me.DataGridView1.EndEdit()
Me.DataGridView1.Rows.RemoveAt(e.RowIndex)
End If
If DataGridView1.Columns(e.ColumnIndex).Name = "Column4" AndAlso Me.DataGridView1.Rows(e.RowIndex).IsNewRow = False Then
Dim Update As UpdateWindow
Update = UpdateWindow
Update.Show()
End If
End Sub
Private Sub dltBtn_Click(sender As Object, e As EventArgs)
Dim dltBtn As dltWindow
dltBtn = dltWindow
dltBtn.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Button1 As addBtn
Button1 = addBtn
Button1.Show()
End Sub
Private Sub UptBtn_Click(sender As Object, e As EventArgs)
Dim UptBtn As UpdateWindow
UptBtn = UpdateWindow
UptBtn.Show()
End Sub
Dim thisFilename As String = Application.StartupPath & "\Event reminder.dat"
Private Sub saveBtn_Click(sender As Object, e As EventArgs) Handles saveBtn.Click
Me.Validate()
Me.SaveGridData(DataGridView1, thisFilename)
End Sub
Private Sub BtnLoad_Click(sender As Object, e As EventArgs) Handles BtnLoad.Click
Me.LoadGridData(DataGridView1, thisFilename)
End Sub
Private Sub SaveGridData(ByRef ThisGrid As DataGridView, ByVal thisFilename As String)
'ThisGrid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText
'ThisGrid.SelectAll()
'IO.File.WriteAllText(thisFilename, ThisGrid.GetClipboardContent().GetText.TrimEnd)
'ThisGrid.ClearSelection()
Dim FileToDelete As String
FileToDelete = "saved.txt"
If System.IO.File.Exists(FileToDelete) = True Then
System.IO.File.Delete(FileToDelete)
End If
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("saved.txt", True)
For Each row As DataGridViewRow In DataGridView1.Rows
If Not row.IsNewRow Then
file.WriteLine(row.Cells(0).Value.ToString & "~" & row.Cells(1).Value.ToString & "\n")
End If
Next
file.WriteLine("EOF")
file.Close()
End Sub
Private Sub LoadGridData(ByRef ThisGrid As DataGridView, ByVal Filename As String)
ThisGrid.Rows.Clear()
For Each THisLine In My.Computer.FileSystem.ReadAllText(Filename).Split(Environment.NewLine)
ThisGrid.Rows.Add(Split(THisLine, " "))
Next
End Sub
Private Sub MainForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
因此,我的想法是以某种方式在LoadGridData中编写一个程序,该程序可读取文本文件并知道〜"之前的所有内容.需要将其加载到单元格0中,〜"之后的所有内容都将被加载到单元格0中. nedds将被加载到单元格1中.如果我
可以得到一些帮助,我将不胜感激.
So what I am thinking is somehow write a program in the LoadGridData that reads through the text file and knows that everything before the "~" needs to be loaded into cell 0 and everything after the "~" nedds to be loaded in cell 1. If I
could get some help I would be much appreciative.
推荐答案
以下是一个按钮的单击操作,您可以保存并读取不同的事件.
The following is in one button click which were you would have the save and read in different events.
请注意,使用StringBuilder和File.WriteAllLines使事情比当前使用的方法更容易.为了回读数据,我使用了TextFieldParser来完成这种类型的工作.
Note the use of StringBuilder and File.WriteAllLines make things easier than the current method being used. For reading data back I use a TextFieldParser which was made for doing this type of work.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
-- save
Dim fileName As String =您的文件名"; Dim sb作为新的Text.StringBuilder 对于每行作为DataGridView1.Rows中的DataGridViewRow 如果不是row.IsNewRow然后 sb.AppendLine(
Dim fileName As String = "Your file name" Dim sb As New Text.StringBuilder For Each row As DataGridViewRow In DataGridView1.Rows If Not row.IsNewRow Then sb.AppendLine(
"{row.Cells(0).ToString}〜{row.Cells(1).ToString}") 万一 下一个 IO.File.WriteAllText(fileName,sb.ToString) - 读 使用MyReader作为新FileIO.TextFieldParser(fileName) MyReader.TextFieldType = FileIO.FieldType.Delimited MyReader.Delimiters = New String(){〜" 昏暗currentRow作为String() 虽然不是MyReader.EndOfData 尝试 currentRow = MyReader.ReadFields() DataGridView1.Rows.Add(New Object(){currentRow(0),currentRow(1)}) 以FileIO.MalformedLineException的形式捕获 -处理异常写入日志文件等 结束尝试 结束时间 最终使用 结束Sub
"{row.Cells(0).ToString}~{row.Cells(1).ToString}") End If Next IO.File.WriteAllText(fileName, sb.ToString) -- read Using MyReader As New FileIO.TextFieldParser(fileName) MyReader.TextFieldType = FileIO.FieldType.Delimited MyReader.Delimiters = New String() {"~"} Dim currentRow As String() While Not MyReader.EndOfData Try currentRow = MyReader.ReadFields() DataGridView1.Rows.Add(New Object() {currentRow(0), currentRow(1)}) Catch ex As FileIO.MalformedLineException -- handle the exception e.g. write to log file etc. End Try End While End Using End Sub
最后一行不需要EOF,我认为它有任何实际用途.最后,添加所有您认为适合读入值的断言或对话.
No need for EOF in the last line, I see it serving any real purpose. Lastly, add any assertions or conversations as you see fit for values read in.
这篇关于试图添加一个加载按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!