我想知道是否有办法只允许文本框的某些部分在冻结的句子中进行编辑。 e.g:
He share of ________ to worse. Weddings and any opinions suitable smallest nay.
My he houses or months settle remove ladies appear. Engrossed suffering
supposing he recommend do eagerness.
可以编辑下划线,但句子根本无法更改。
由于
答案 0 :(得分:0)
将三个TextBox彼此相邻放置,只允许编辑中间的一个。这是一些XAML:
<StackPanel
Orientation="Horizontal">
<TextBox
Text="He share of"
IsReadOnly=True />
<TextBox
Text="<Enter your phrase here>" />
<TextBox
Text="to worse. ......."
IsReadOnly=True />
</StackPanel>
最终用户将整条线视为一个单元。
答案 1 :(得分:0)
Public Class Form1
Private SelectionStart As Integer = 10
Private SelectionEnd As Integer = 20
Private SelectionCurrent As Integer = 10
Private Sub CursorControl(Optional ByVal e As Object = Nothing)
If SelectionCurrent > SelectionEnd Then
SelectionCurrent = SelectionEnd
ElseIf SelectionCurrent < SelectionStart Then
SelectionCurrent = SelectionStart
End If
If TextBox1.SelectionStart >= SelectionEnd Then
If Not e Is Nothing Then
e.Handled = True
e = Nothing
End If
TextBox1.SelectionStart = SelectionEnd
TextBox1.SelectionLength = 0
Me.SelectionCurrent = TextBox1.SelectionStart
ElseIf TextBox1.SelectionStart <= SelectionStart Then
If Not e Is Nothing Then
e.Handled = True
e = Nothing
End If
TextBox1.SelectionStart = SelectionStart
TextBox1.SelectionLength = 0
Me.SelectionCurrent = TextBox1.SelectionStart
Else
Me.SelectionCurrent = TextBox1.SelectionStart
End If
End Sub
Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click
CursorControl()
End Sub
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
CursorControl()
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
TextBox1.SelectionStart = Me.SelectionCurrent
TextBox1.SelectionLength = 0
If e.KeyCode = Keys.Right Or e.KeyCode = Keys.Left Or e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then
e.Handled = True
e = Nothing
End If
CursorControl(e)
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
TextBox1.SelectionStart = Me.SelectionCurrent
TextBox1.SelectionLength = 0
If e.KeyChar = "_" Then
e.Handled = True
End If
If TextBox1.SelectionStart >= SelectionEnd Then
e.Handled = True
e = Nothing
ElseIf TextBox1.SelectionStart <= SelectionStart Then
e.Handled = True
e = Nothing
Else
TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.SelectionStart) & e.KeyChar & TextBox1.Text.Substring(TextBox1.SelectionStart + 1, TextBox1.Text.Length - TextBox1.SelectionStart - 1)
TextBox1.SelectionStart = Me.SelectionCurrent
e.Handled = True
e = Nothing
End If
CursorControl()
End Sub
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
TextBox1.SelectionStart = Me.SelectionCurrent
TextBox1.SelectionLength = 0
If e.KeyCode = 189 Then '_
e.Handled = True
End If
Me.SelectionCurrent = Me.SelectionCurrent + 1
Me.TextBox1.SelectionStart = Me.SelectionCurrent
CursorControl(e)
End Sub
结束班
答案 2 :(得分:0)
通过RichTextBox的Winform控件SelectionProtected Property允许您将文本范围标记为只读(受保护)。
将整个文本标记为受保护,然后有选择地取消保护所需的可编辑范围将产生所需的效果。
Public Class Form1
Private underlinedFont As Font
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RichTextBox1.Text = "This is protected. This is unprotected."
' Protect all text
RichTextBox1.SelectAll()
RichTextBox1.SelectionProtected = True
' Unprotect the word "unprotected"
Dim startUnProtected As Int32 = RichTextBox1.Find("unprotected", RichTextBoxFinds.WholeWord)
If startUnProtected > -1 Then
RichTextBox1.SelectionProtected = False
If underlinedFont Is Nothing Then underlinedFont = New Font(RichTextBox1.Font.FontFamily, RichTextBox1.Font.Size, FontStyle.Underline, RichTextBox1.Font.Unit)
RichTextBox1.SelectionFont = underlinedFont
End If
End Sub
End Class