Nimit Joshi
Nimit Joshi

Reputation: 1046

Cannot access the TextBox in MainPage.xml.cs file

I am creating a Windows Store Blank application. Following is my code:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="100"></RowDefinition>
        <RowDefinition Height="100"></RowDefinition>
        <RowDefinition Height="700"></RowDefinition>
    </Grid.RowDefinitions>
    <TextBlock Text="Sample Search Application"  Margin="30" FontSize="36" />

    <StackPanel Grid.Row="1" Orientation="Horizontal">
        <TextBox x:Name="txtsearch" Width="200" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="30" TextChanged="txtsearch_TextChanged"></TextBox>
        <Button x:Name="BtnPrint" Content="GetValue" Click="BtnPrint_Click"></Button>
    </StackPanel>
    <StackPanel Grid.Row="2" Orientation="Horizontal">
        <TextBlock x:Name="Result" FontSize="36"></TextBlock>
    </StackPanel>
</Grid>

As you see that I have defined a textbox. But in the cs file I am unable to access this textblock. Following is my cs file snapshot:

enter image description here

Upvotes: 0

Views: 178

Answers (2)

Sajeetharan
Sajeetharan

Reputation: 222582

You need to define the TextBlock name,

<TextBlock Name="txtTest" Text="Flickr Search Application"  Margin="30" FontSize="36" />

EDIT:

Since you have TextBlock inside a parent control, you can use FindName method.

Upvotes: 2

Best_Where_Gives
Best_Where_Gives

Reputation: 481

Hit F5 and try again. Maybe it is not yet compiled.

Upvotes: 2

Related Questions