Silverlight 文本框覆盖

发布于 2024-07-22 06:21:18 字数 899 浏览 5 评论 0原文

我正在尝试获取文本框按键事件来捕获退格键按下事件。 我通过添加一个覆盖文本框的类来实现这一点。 我不知道该怎么做是与用户控件中文本框所在的类进行通信。

当用户在文本框中键入...例如 abcd 或退格键时,我需要更新用户控件上的某些内容。 假设我想要显示文本框中有多少个字符的东西。 有人可以帮我吗? 这是我到目前为止所拥有的,

Option Strict On
Imports System.Text.RegularExpressions

Partial Public Class Page
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
        Dim textbox As New MyTextBox() With {.Width = 300, .Height = 100}
        LayoutRoot.Children.Add(textbox)
    End Sub
End Class

Public Class MyTextBox
    Inherits TextBox
    Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
        MyBase.OnKeyDown(e)
        If e.Key = Key.Back Then
            e.Handled = True
            MyBase.OnKeyDown(e)

        ElseIf e.Key = Key.Delete Then
            e.Handled = True
            MyBase.OnKeyDown(e)
        End If
    End Sub
End Class

谢谢 香农

I'm trying to get a textbox keydown event to trap the backspace key down event. I have that working by adding a Class that overrides the textbox. What i don't know how to do is have that communicate with the class where the textbox is in the user control.

When a user types in the text box... say abcd or backspace, i need to update something on the usercontrol. let's just say i want to have something that displays how many characters are in the textbox. can someone help me with that. Here is what i have so far

Option Strict On
Imports System.Text.RegularExpressions

Partial Public Class Page
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
        Dim textbox As New MyTextBox() With {.Width = 300, .Height = 100}
        LayoutRoot.Children.Add(textbox)
    End Sub
End Class

Public Class MyTextBox
    Inherits TextBox
    Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
        MyBase.OnKeyDown(e)
        If e.Key = Key.Back Then
            e.Handled = True
            MyBase.OnKeyDown(e)

        ElseIf e.Key = Key.Delete Then
            e.Handled = True
            MyBase.OnKeyDown(e)
        End If
    End Sub
End Class

thanks
shannon

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗷兮 2024-07-29 06:21:18

您不需要子类化 TextBox 来执行此操作。 相反,为 TextBox.TextChanged 事件就在您的 UserControl 类中。 调用此函数时,事件的发送者应该是您的 TextBox。 然后您可以从中获取文本并执行您需要执行的操作。

更新:根据评论,以下内容应该有效:(

Partial Public Class Page
    Inherits UserControl

    Private TextBox1 as TextBox

    Public Sub New()
        InitializeComponent()
        TextBox1 = New TextBox() With {.Width = 300, .Height = 100}
        LayoutRoot.Children.Add(textbox)
    End Sub

    Private Sub OnTextChanged(sender as Object, e as TextChangedEventArgs) Handles TextBox1.TextChanged
        If e.Key = Key.Back Then
            e.Handled = True
        ElseIf e.Key = Key.Delete Then
            e.Handled = True
        End If
    End Sub
End Class

我的 VB 有点生疏,因此事件处理程序语法可能不完全正确。)

基本思想是在文本更改时收到通知UserControl 中的 TextBox。 这样您就可以根据需要修改 UserControl 的其他部分。

You shouldn't need to subclass TextBox to do this. Instead, add handler for the TextBox.TextChanged event right in your UserControl class. When this is called, the sender of the event should be your TextBox. You can then get the text from it and do what you need to do.

Update: Based on the comment, the following should work:

Partial Public Class Page
    Inherits UserControl

    Private TextBox1 as TextBox

    Public Sub New()
        InitializeComponent()
        TextBox1 = New TextBox() With {.Width = 300, .Height = 100}
        LayoutRoot.Children.Add(textbox)
    End Sub

    Private Sub OnTextChanged(sender as Object, e as TextChangedEventArgs) Handles TextBox1.TextChanged
        If e.Key = Key.Back Then
            e.Handled = True
        ElseIf e.Key = Key.Delete Then
            e.Handled = True
        End If
    End Sub
End Class

(My VB is a bit rusty, so the event handler syntax might not be completely correct.)

The basic idea is to get notified when text changes in the TextBox within your UserControl. This way you can modify the other parts of the UserControl as necessary.

隔岸观火 2024-07-29 06:21:18

你可能想错了。 由于您使用的是 Silverlight,因此您可以通过绑定表达式获取此信息。 举个简单的例子,您说您想知道文本框中有多少个字符。 您可以使用以下 xaml 来实现此目的:

    <TextBox x:Name="txtInput" />
    <TextBlock Text="{Binding ElementName=txtInput, Path=Text.Length}" />

You may be thinking the wrong way. Since you're in Silverlight, you can get this information with binding expressions. Just as a simple example, you said you want to know how many characters are in a text box. You can achieve this with the following xaml:

    <TextBox x:Name="txtInput" />
    <TextBlock Text="{Binding ElementName=txtInput, Path=Text.Length}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文