WPF: Submitting textbox content

This entry is part 19 of 54 in the series Learn WPF

When we design a web form, we would test out if we could collect the submitted information.

Say now we need to collect some information from the user using a textbox.  We can design a submit button and let the user click on the button to submit the information.

As you can see below, this can be easily done by using tb.Text where tb is the instance name of the TextBox.

WindowMain.xaml
<Window x:Class="TextBox1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox Name="tb" FontSize="16" Height="100">
            Type something here...
        </TextBox>
        <Button Click="OnClick" FontSize="24" HorizontalAlignment="Stretch" Margin="80,50,80,50">Submit</Button>
        <TextBlock Name="tbl" Text="Text Block" FontSize="24" Height="100"></TextBlock>
    </StackPanel>
</Window>

WindowMain.xaml
using System;
using System.Windows;

namespace TextBox1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OnClick(object sender, RoutedEventArgs e)
        {
            String someText = tb.Text;

            tbl.Text = someText;

        }
    }
}

You can type something in the textbox and after clicking on the submit button, you will see an output similar to below:

submittext

 

 

 

 

Series Navigation<< WPF: object sender
WPF: Radio button >>

Leave a comment

Leave a Reply