-2

I want to a WPF project in c# and I want in my MainWindow interface to add a textbox. I use toolbox to add textBox field and with view code I found the following function:

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{

}

How can I change the default value of the textBox? The only thing the is generated when I add the Textbox was this function. Which variable is responsible for the text? EDIT: I am trying to modify a WPF application which is provided with the kinect SDK. It seems that is not that straight-forward. When I tried to right click on the textBox I cant see any properties option. Inside MainWIndow.xaml.cs there is a line with the property of the textbox:

<TextBox HorizontalAlignment="Left" Height="23" Margin="540,3,0,0" TextWrapping="Wrap" Text="0" VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged" Grid.ColumnSpan="3"/>

However I cant find the name of the textbox anywhere.

1
  • 4
    To get or change the text you should use the Text property Commented Jul 14, 2015 at 13:18

2 Answers 2

1

If you select the textbox and press F4 to access the selected item's properties (normally displayed in the bottom right of the screen), you can set the Text property in the property grid. The value you enter will be used when the form is first created. This is equivalent to setting the Text XAML attribute on the TextBox element.

In code, you can also do this in your constructor using textbox1.Text = "hello"; for instance.

Note, to access the TextBox in code you will need to assign it a name (also via the properties grid).


<TextBox x:Name="textbox1" Text="Default Value Goes Here" HorizontalAlignment="Left" Height="23" Margin="540,3,0,0" TextWrapping="Wrap" Text="0" VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged" Grid.ColumnSpan="3"/>
Sign up to request clarification or add additional context in comments.

11 Comments

My problem is that where should I look for the generated textbox object.
If you go to the form designer, you should have the XAML code below inside which you will find a line like <TextBox ... />
Yes I have already posted this line in the post.
Ok. I see that! You need to add an x:Name attribute ... i'll update my answer.
I've also added the default value bit for you!
|
0

The Text property is what holds the value of the textbox.

Textbox1.Text = " ";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.