我正在嘗試制作一個程式,該程式利用一個文本框,其中列出了可以單擊的文本選項。
作為文本框示例:
[選擇:<1><2><3>]
因此用戶可以作為示例單擊(在文本上)<2> 選擇第二個選項或 <3> 選擇第三個選項。這個想法來自使用模擬系統的 AutoCAD 命令提示符。
我將如何在 vb.net 代碼中實作這樣的事情(如果可能的話)?
uj5u.com熱心網友回復:
這是一個快速示例,展示了如何使用
uj5u.com熱心網友回復:
試試這個:
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
Dim SplitText As String() = TextBox1.Text.Split(CChar("<"), ">")
Dim SelectedText As String = GetSelectedText()
Dim Options As New List(Of String)
If Not SelectedText = "" Then
For i = 0 To SplitText.Length - 1
If IsNumeric(SplitText(i)) Then
Options.Add("<" & SplitText(i) & ">")
End If
Next
For i = 0 To Options.Count - 1
If SelectedText = Options(i) Then
'Put your code here if it is the current option in the loop equals the selected option.
'I added a messagebox just so you can see the current option.
MessageBox.Show("You selected option: " & Options(i))
End If
Next
End If
End Sub
Private Function GetSelectedText()
Dim CursorPosition As Integer = TextBox1.SelectionStart
Dim SelectedNumber As String = ""
Dim NumberLength As Integer = 0
If CursorPosition = 0 Or CursorPosition = TextBox1.Text.Length Then
Return ""
End If
Do Until Mid(TextBox1.Text, CursorPosition - NumberLength, 1) = "<"
NumberLength = 1
Loop
SelectedNumber = Mid(TextBox1.Text, CursorPosition - NumberLength, NumberLength 1)
NumberLength = 0
CursorPosition = 1
Do Until Mid(TextBox1.Text, CursorPosition NumberLength, 1) = ">"
NumberLength = 1
Loop
SelectedNumber &= Mid(TextBox1.Text, CursorPosition, NumberLength 1)
If IsNumeric(SelectedNumber.Remove(0, 1).Remove(SelectedNumber.Length - 2, 1)) Then
Return SelectedNumber
Else
Return ""
End If
End Function
我把它放在文本框點擊事件中,它可以作業。我沒有嘗試將代碼放在任何其他事件中。我假設文本框被命名為:TextBox1。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/415374.html
標籤:
