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

 

 

 

 

HTML Form: Form that is sent through Email

This entry is part 7 of 10 in the series HTML Form

Since we are not using CGI script to collect data from the form, we will create a form that we can send through email.

Clicking on the Send button in the form will launch the email program install in your PC.

<!DOCTYPE html>
<html>
<head lang='en'>     
    <meta charset='UTF-8'>
    <title>Codecrawl.com-Form</title>

</head>
<body>

<form method='post' action='mailto: johndoe@yahoo.com'>
    Name: <input type='text' name='name'>
    <br>
    Phone: <input type='text' name='phone'>
    <br>

    Comments:
    <br>
    <textarea name='comment' rows='3'></textarea>
    <br>
    <input type='submit' value='Send'>
    <input type='reset' value='Clear'>
</form>

</body>
</html>

HTML Form: Creating a Submit or Reset button

This entry is part 5 of 10 in the series HTML Form

For a form, we would need to include a Submit button on the form so that visitors can send the information to us.

The default button text is Submit, but we can use a value attribute to display different text on the button.

<!DOCTYPE html>
<html>
<head lang='en'>     
    <meta charset='UTF-8'>
    <title>Codecrawl.com-Form</title>

</head>
<body>
<br>
Name: <input type='text' name='name'>
<br>
Phone: <input type='text' name='phone'>
<br>

Comments:
<br>
<textarea name='comment' rows='3'></textarea>
<br>
<input type='submit' value='Send'>
<input type='reset' value='Clear'>

</body>
</html>