RaceLt
RaceLt

Reputation: 65

Custom usercontrol textbox.text

I am trying to make a new UserControl (TextBox which selects all text on focus) but i can't update .text from code.

What i'm trying to achieve is:

kolicinaTbox.text = "test"

My custom control is:

<UserControl x:Class="SelectAllTbox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Demo"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="0"  VerticalAlignment="Top" Width="120"/>

</Grid>

Public Class SelectAllTbox
Private Sub textBox_GotFocus(sender As Object, e As RoutedEventArgs) Handles textBox.GotFocus
    textBox.SelectAll()

End Sub
End Class    

And this is the control in my code:

<demo:SelectAllTbox x:Name="kolicinaTbox" HorizontalAlignment="Left" Margin="82,152,0,0" VerticalAlignment="Top" Width="63"/>

Upvotes: 0

Views: 70

Answers (1)

mm8
mm8

Reputation: 169150

Add a property to your UserControl (the SelectAllTbox class) that gets/sets the Text property of the TextBox:

Public Property Text() As String
    Get
        Return textBox.Text
    End Get
    Set(ByVal value As String)
        textBox.Text = value
    End Set
End Property

Then you can set the Text property of the TextBox using this property:

kolicinaTbox.Text = "test"

Upvotes: 1

Related Questions