locked
get text from a TextBox

    Question

  • Hello,

    I'm trying to develop a Windows 8 app, and in the process used blend to add some Textboxes to the UI in XAML. However, I want to eventually get the text the user has imputed into the boxes, and use it in C#. Is there a way to easily do this? I've been looking for online, and it seems sort of like the opposite of data binding, and I haven't had a lot of luck looking around for an answer to this question.

    Thanks for the Help!

    Thursday, August 14, 2014 9:46 PM

Answers

  • Hi DrakeTX,

    Well, first step you need assign a x:Name for a TextBox control:

     <TextBox x:Name="txt" HorizontalAlignment="Left" Height="194" Margin="337,210,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="350"/>
    

    second step, get the Text from cs:

            public MainPage()
            {
                this.InitializeComponent();
                string text = txt.Text;
            }
    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Magnus (MM8)MVP Friday, August 15, 2014 3:36 PM
    • Marked as answer by DrakeTX Friday, August 15, 2014 6:26 PM
    Friday, August 15, 2014 8:53 AM
    Moderator

All replies

  • Hi DrakeTX,

    Well, first step you need assign a x:Name for a TextBox control:

     <TextBox x:Name="txt" HorizontalAlignment="Left" Height="194" Margin="337,210,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="350"/>
    

    second step, get the Text from cs:

            public MainPage()
            {
                this.InitializeComponent();
                string text = txt.Text;
            }
    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Magnus (MM8)MVP Friday, August 15, 2014 3:36 PM
    • Marked as answer by DrakeTX Friday, August 15, 2014 6:26 PM
    Friday, August 15, 2014 8:53 AM
    Moderator
  • Thanks for the help James!

    This makes sense, but I'm having a bit of difficulty implementing this as when I do, I get a The Name does not exist in the current context error.  Right now, the Textboxes were generated by blend in a DataTemplatein App.xaml

    <DataTemplate x:Name="PostAnEventBlocks" x:Key="DataTemplate1">
          	<Grid>
    	<TextBlock HorizontalAlignment="Left" Width = "200" Margin="14,10,0,487" FontSize="25">Event Name</TextBlock>
          	<TextBox Name="EventNameTextBox" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Width="186" Margin="14,71,0,0"/>
    			
          	</Grid>
          </DataTemplate>

    where the DataTemplate is called in MainPage.xaml

    <HubSection x:Name="PostAnEventSection" Header="Post An Event" DataContext="{Binding MainViewModel.PostAnEventModel}" ContentTemplate="{StaticResource DataTemplate1}" IsHeaderInteractive="{Binding HasMoreItems}" Style="{StaticResource AppHubSectionStyle}" Width="642" 
                 />

    Is there a arrange this so that the TextBoxes can be more easily edited? I seriously doubt that the DataTemplate is in the right place, or the right format for what I really need.

    Thanks for all of the help!

    -Drake

    Friday, August 15, 2014 6:53 PM