Home > Back-end >  Textbox.text returning Null
Textbox.text returning Null

Time:12-15

I've currently got 2 textboxes, one inside a dockpanel, one outside of it. The textbox outside the dockpanel works, but the one inside doesn't.

if (int.TryParse(tbxNumber.Text, out int number)) { }
        else
        {
            MessageBox.Show("That text could not be converted into a number, try again.");
            tbxNumber.Text = String.Empty;
            return;
        }
if (int.TryParse(tbxHidden.Text, out int hideScore)) { }
        else
        {
            MessageBox.Show("That text could not be converted into a number, try again.");
            tbxNumber.Text = String.Empty;
            return;
        }

This one works:

<TextBox Name="tbxNumber" Text="1" Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="2" Margin="0,0,0,5"/>

This one doesn't:

<DockPanel Grid.Column="2" Grid.Row="6" Margin="0,0,0,5">
   <CheckBox Name="checkHidden" HorizontalAlignment="Left" Margin="5,0,0,0"/>
   <TextBox Name="tbxHidden" Text="1" Width="20" HorizontalAlignment="Right" Margin="0,0,5,0"/>
</DockPanel>

The textbox does however show up correctly in the window, however, is unable to return a value for TryParse()

CodePudding user response:

Add Focusable="true" for tbxHidden.

Can you provide more code? Code looks fine.

string Val = tbxHidden.Text;

if (int.TryParse(Val, out int OutVal))
{
    tbxNumber.Text = OutVal.ToString();
}
else
{
    tbxNumber.Text = "";
    //Show MsgBox
}
return;

CodePudding user response:

Both TextBoxes from your example work fine for me with int.TryParse(). So Textbox.text is not returning Null. Checked on .NET Framework 4.7.1

  • Related