如何从第一行及其所有列开始动态编辑表,按下按钮的第二行 [英] How to edit table dynamically starting with first row and its all column and the second row on pressing button
问题描述
Private Sub sendbutton_Click(sender As Object, e As EventArgs) Handles sendbutton.Click
TabPage1.Hide()
TabPage3.Show()
Dim FileNum As Integer = FreeFile()
Dim tempL As String
Dim receipt As String
Dim check As String = " "
Dim test As String = "Processing..."
Dim see As String = "Successful"
TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.AutoSize))
Try
FileOpen(FileNum, TextBox1.Text, OpenMode.Input)
Do Until EOF(FileNum)
Dim ctrl As New TextBox
Dim sign As New Label
Dim txt As New TextBox
TableLayoutPanel2.RowCount += 1
TableLayoutPanel2.Controls.Add(ctrl, 0, TableLayoutPanel2.RowCount - 1)
TableLayoutPanel2.Controls.Add(txt, 1, TableLayoutPanel2.RowCount - 1)
TableLayoutPanel2.Controls.Add(sign, 2, TableLayoutPanel2.RowCount - 1)
tempL = LineInput(FileNum)
SerialPort1.WriteLine(tempL)
Do
If (check.Equals(" ")) Then
Dim i As Integer = 0
ctrl.BorderStyle = BorderStyle.None
ctrl.ReadOnly = True
ctrl.Size = New System.Drawing.Size(140, 20)
ctrl.BackColor = SystemColors.Window
ctrl.Visible = True
While (i < tempL.Length)
Timer1.Start()
ctrl.Text += tempL.Chars(i)
i = i + 1
End While
check = "1"
Exit Do
End If
Loop
Do
If (check.Equals("1")) Then
Dim i As Integer = 0
txt.ForeColor = Color.Red
txt.BorderStyle = BorderStyle.None
txt.ReadOnly = True
txt.BackColor = SystemColors.Window
txt.Visible = True
While (i < test.Length)
Timer1.Start()
txt.Text += test.Chars(i)
i = i + 1
End While
check = "2"
Exit Do
End If
Loop
Do
System.Threading.Thread.Sleep(2000)
receipt = SerialPort1.ReadExisting()
RichTextBox2.Text = receipt
If (receipt.Equals("OK")) Then
Dim i As Integer = 0
RichTextBox2.Text = receipt
txt.Text = ""
txt.ForeColor = Color.Green
txt.BorderStyle = BorderStyle.None
txt.BackColor = SystemColors.Window
While (i < see.Length)
Timer1.Start()
txt.Text += see.Chars(i)
i = i + 1
End While
If check.Equals("2") Then
sign.ForeColor = Color.Green
sign.Text = ChrW(&H2713)
check = " "
End If
Exit Do
End If
Loop
System.Threading.Thread.Sleep(2000)
Loop
FileClose(FileNum)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
我尝试了什么:
i尝试在控件中编辑文本时放置计时器以显示打字模式下的文字,但似乎也不起作用,我也把检查字符串只是为了检查它是否执行了以前的文本...所以通过col编辑得到col表格中的一行,然后是其他行编辑
What I have tried:
i tried to put timer while editing text in controls so to display the text in typewriting mode but that also doesn't seem to work and i also put the check string just to check whether it executed its previous text or not... so to get col by col editing of a row and then other row editing in table
推荐答案
假设您要逐行读取文本文件...
我建议使用 File.ReadAllLines( )方法 [ ^ ]从文本文件中获取所有行。
如何使用它?
1.在Form1模块中声明2个变量(请参阅有关Scope的MSDN文档在VB):
- 首先保持当前行号:
- 第二个保存文件名:
Assuming that you want to read text file line by line...
I'd suggest to use File.ReadAllLines() method[^] to get all lines from text file.
How to use it?
1. Declare 2 variables in Form1 module (see MSDN documentation about Scope in VB):
- first to hold current line number:
- second to hold file name:
Dim currLine As Integer = 0
Dim sFileName As String = String.Empty
2.添加按钮(将其命名为: BtnReadFile
)并在里面点击下面的
事件代码:
2. Add button (name it: BtnReadFile
) and inside Click
event put below code:
sFileName = Me.TextBox1.Text
3.添加从文本文件读取单行的功能
3. Add function to read single line from text file
Public Function ReadLine(sPath As String, lineNo As Integer) AS String
Return File.ReadAllLines(sPath).Skip(lineNo-1).Take(1).SingleOrDefault()
End Function
4.添加按钮(将其命名为: BtnNextLine
)并在内单击
事件放在代码下面:
4. Add button (name it: BtnNextLine
) and inside Click
event put below code:
'increase line no
currLine += 1
'read single line
Dim singleLine = ReadLine(sFileName, currLine)
'
'code to proccess data here
'
重要提示:
这个例子不是pefect,但演示了如何逐行读取文本文件。
待办事项:
- 添加错误处理
- 改进代码
+获取文件中的行数
+从文本文件中读取前一行
  ; +阻止用户进入小于1或高于lines.Count的行。
详情请见:
Visual Basic中的范围| Microsoft Docs [ ^ ]
如何:控制变量的范围(Visual Basic)| Microsoft Docs [ ^ ]
Microsoft .NET中的变量和方法范围 [ ^ ]
Important note:
This example is not pefect, but demonstrates how to read text file line by line.
TO DO:
- add error handling
- improve code to
+ get the count of lines in a file
+ read previous line from text file
+ prevent user to go into a line less than 1 or upper than lines.Count.
For further details, please see:
Scope in Visual Basic | Microsoft Docs[^]
How to: Control the Scope of a Variable (Visual Basic) | Microsoft Docs[^]
Variable and Method Scope in Microsoft .NET[^]
这篇关于如何从第一行及其所有列开始动态编辑表,按下按钮的第二行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!