locked
Set value and get value... RRS feed

  • Question

  • I can set a Grid.Row value to a textbox(txtQuestion) by:
    txtQuestion.SetValue(Grid.RowProperty, 1);

    but how can i get the row value of txtQuestion into another textbox(txtRowvalue)?
    i tryed this on a button click, but wont work:
    txtRowvalue.Text = txtQuestion.GetValue(Grid.RowProperty);

    Help anyone ?
    Thursday, December 17, 2009 3:01 AM

Answers

  • Hi Combo, Grid.RowProperty is of type int, you can't cast it to a string. Instead call the ToString-Method on it:

     

          txtRowvalue.Text = txtQuestion.GetValue(Grid.RowProperty).ToString();
     
    Thursday, December 17, 2009 5:58 AM
  • My bad, so much for theoretical answers.

    .ToString() is your friend in this case.

    Thursday, December 17, 2009 6:19 AM

All replies

  • I can set a Grid.Row value to a textbox(txtQuestion) by:
    txtQuestion.SetValue(Grid.RowProperty, 1);

    but how can i get the row value of txtQuestion into another textbox(txtRowvalue)?
    i tryed this on a button click, but wont work:
    txtRowvalue.Text = txtQuestion.GetValue(Grid.RowProperty);

    Help anyone ?

    Your code is correct except you aren't casting the result to string:
    txtRowvalue.Text = (string)txtQuestion.GetValue(Grid.RowProperty);

    Thursday, December 17, 2009 4:49 AM
  • Hallo brother,

     I tryed as you said, still nothing appears in txtRowvalue...?

    Thursday, December 17, 2009 5:12 AM
  • Where in your code are you trying to return the grid.row property? Is it in the initialise event?

    What happens if you try to get the value in your immediate window while debugging?

    Thursday, December 17, 2009 5:34 AM
  • the XAML:

    <Grid x:Name="grid1">
    <Grid.RowDefinitions>
    <RowDefinition Height="50"/>
    <RowDefinition Height="50"/>
    <RowDefinition Height="50"/>
    <
    /Grid.RowDefinitions>

    <
    Grid.ColumnDefinitions>
    <ColumnDefinition/>
    <ColumnDefinition Width="100"/>
    </Grid.ColumnDefinitions>

    <TextBox x:Name="txtQuestion" Text="ToolpacK" FontSize="24" Background="Red" Foreground="White" Grid.Row="0" Grid.Column="0"/>
    <Button x:Name="Button" Click="Button_Click" Content="Switch grid" Grid.Row="2" Grid.Column="0"/>
    <TextBox x:Name="txtRowvalue" Background="LightGray" Grid.Row="0" Grid.Column="2"></TextBox>

     

    </Grid>

     the C#:

    public partial class MainPage : UserControl
    {

    public MainPage()
    {
    InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
    txtQuestion.SetValue(
    Grid.RowProperty, 1);
    txtRowvalue.Text = (
    string)txtQuestion.GetValue(Grid.RowProperty);
    }

    }

    ?

    Thursday, December 17, 2009 5:49 AM
  • Hi Combo, Grid.RowProperty is of type int, you can't cast it to a string. Instead call the ToString-Method on it:

     

          txtRowvalue.Text = txtQuestion.GetValue(Grid.RowProperty).ToString();
     
    Thursday, December 17, 2009 5:58 AM
  • My bad, so much for theoretical answers.

    .ToString() is your friend in this case.

    Thursday, December 17, 2009 6:19 AM
  • Thanks alot my friends! now it works, im a new-beginner to c#.

    Why do i have to make " .tostring(); " isnt the grid row property a number value? and why would a textbox have problems to pick up numbervalues ?

    Thursday, December 17, 2009 6:34 AM
  • Hi tjcombo,

    yes, the Grid.Row-Property is a number value. A "int" to be concrete. But the Text-Property of the TextBox is a "string". So you need to get a string from the int, and you do this by simply calling the ToString()-Method that is defined in the object-class. Alright?

    Thursday, December 17, 2009 2:59 PM