[英]vb.net Line numbering in multi line Rich Text Box if line starts with specific condition
Let me start by saying that I'm new to any language of coding besides G-code, and I've researched this until my fingers hurt.首先让我说我对 G 代码以外的任何编码语言都不熟悉,我已经研究了这个,直到我的手指受伤。 I've actually been working on this project for a little over a year now on my own, and this is the first hurdle I haven't been able to find my way around.
实际上,我自己已经在这个项目上工作了一年多,这是我无法找到的第一个障碍。
I'm creating an editor for cnc G-code, and i'm trying to add a Re-number function to it.我正在为 cnc G 代码创建一个编辑器,我正在尝试向其中添加一个重新编号功能。 I'm using a multi line richtextbox to display the the G-code to the user.
我正在使用多行富文本框向用户显示 G 代码。 I'm trying to edit each line of code that starts with the character "N", and if a line doesn't start with that character then it's left alone.
我正在尝试编辑以字符“N”开头的每一行代码,如果一行不以该字符开头,则将其保留。
I figured the best way to do this would be to loop thru the RTB and pass each line into an array.我认为最好的方法是通过 RTB 循环并将每一行传递到一个数组中。 Then I could use an If statement to see if a cell in the array started with the char "N" or in my case "blockLetter".
然后我可以使用 If 语句来查看数组中的单元格是否以字符“N”或在我的情况下以“blockLetter”开头。 Then use the replace function to correct the line Number.
然后使用替换功能更正行号。
This is what I have so far.这是我到目前为止。
Dim increment As Integer = txtLNIncrement.Text
Dim blockLetter As String = txtLNStartTxt.Text
Dim count As Integer = 0
Dim block As Integer = count + increment
For Each cell As String In frmNC.NcTextBox.Lines
If cell.StartsWith(blockLetter) Then
Dim newCell As String = cell.Replace(blockLetter, block)
block = block + increment
MessageBox.Show(newCell)
End If
Next
Example of G-code that needs to be renumbered: N50 M01 N60 T0101 (TOOL NAME) N70 M41 N80 G96 S350 N90 M03 N100 M08
需要重新编号的 G 代码示例:
N50 M01 N60 T0101 (TOOL NAME) N70 M41 N80 G96 S350 N90 M03 N100 M08
This is what I want: N10 M01 N20 T0101 (TOOL NAME) N30 M41 N40 G96 S350 N50 M03 N60 M08
这就是我想要的:
N10 M01 N20 T0101 (TOOL NAME) N30 M41 N40 G96 S350 N50 M03 N60 M08
This is what I'm getting when I run the code above: 1050 M01 2060 T0101 (TOOL NAME) 3070 M41 4080 G96 S350 5090 M03 60100 M08
这就是我在运行上面的代码时得到的结果:
1050 M01 2060 T0101 (TOOL NAME) 3070 M41 4080 G96 S350 5090 M03 60100 M08
I believe my issue is that the cell.replace is splitting each cell at the "N" character, and dropping it all together.我相信我的问题是 cell.replace 在“N”字符处拆分每个单元格,然后将它们放在一起。 Thus adding what I want to see in front of the existing numbers, minus the "N" character.
因此在现有数字前面添加我想看到的内容,减去“N”字符。 How can I overwrite the existing block number to the correct ascending block number, and retain the "N" character?
如何将现有块号覆盖为正确的升序块号,并保留“N”字符? Am I going about this in the correct way, or is there a better way?
我是以正确的方式解决这个问题的,还是有更好的方法? Any help is greatly appreciated.
任何帮助是极大的赞赏。
Try something like this out:尝试这样的事情:
Private increment As Integer = 10
Private blockLetter As String = "N"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim newLine As String
Dim values() As String
Dim lineNumber As Integer = 0
Dim lines As New List(Of String)(NcTextBox.Lines)
For i As Integer = 0 To lines.Count - 1
If lines(i).TrimStart().StartsWith(blockLetter) Then
values = lines(i).TrimStart(" " & blockLetter.ToCharArray).Split(" ")
lineNumber = lineNumber + increment
values(0) = lineNumber
newLine = blockLetter & String.Join(" ", values)
lines(i) = newLine
End If
Next
NcTextBox.Lines = lines.ToArray
End Sub
it's very simple:这很简单:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim blockLetter As String = "N"
Dim increment As Integer = 1
For i As Integer = 0 To RichTextBox1.Lines.Length - 1 Step 1
Dim fullLine As String = RichTextBox1.Lines(i)
If fullLine.StartsWith(blockLetter) Then
Dim numbering As String = fullLine.Remove(RichTextBox1.Lines(i).IndexOf(" "))
Dim block As Integer = numbering.Substring(1)
Dim newCell As String = blockLetter & block + increment
MessageBox.Show(newCell)
End If
Next
End Sub
Result:结果:
The Label1.Text will increase with button click.
Label1.Text 会随着按钮的点击而增加。
It's all about Substring() starting from index 1 after 'N', so you will get the number.这都是关于 Substring() 从 'N' 之后的索引 1 开始的,所以你会得到这个数字。
Good luck with your coding!祝你编码好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.