简体   繁体   English

从xaml中的文本框中获取价值

[英]Getting value from textbox in xaml

private void search(object sender, RoutedEventArgs e)
{
    if (number.Text < 455)
    {
        string site;
        site = number.Text;
        var rs = Application.GetResourceStream(new Uri("def/f" + site + ".html", UriKind.Relative));
        StreamReader sr = new StreamReader(rs.Stream);
        browser.NavigateToString(sr.ReadToEnd());
    }
    else
    {
        MessageBox.Show("Enter Value between 1 to 454");
    }
} 

I gave InputScope="Number" in TextBox. 我在TextBox中给了InputScope =“Number”。 So the user cant type words. 所以用户不能输入单词。 gave I have created the button with name, so if button pressed search function will be called. 我已经创建了带有名称的按钮,因此如果按下按钮,则会调用搜索功能。 I need to validate that the user entered text in between 1 to 454. If it is, it should go on. 我需要验证用户是否在1到454之间输入了文本。如果是,则应该继续。 Other wise i should alert them like i do above. 其他明智的我应该像我上面那样提醒他们。 But it shows error like, 但它显示错误,如,

Error 1 Operator '<' cannot be applied to operands of type 'string' and 'int'

You are comparing Text which is string with integer which is 455 . 您正在比较Text是字符串与整数,即455 Before comparison convert the text to integer and do this. 在比较之前将文本转换为整数并执行此操作。

if (Convert.ToInt32(number.Text) < 455)

Here is your solution 这是你的解决方案

int num = 0;

if (int.TryParse(number.Text, out num) && num > 0 && num < 455)
{
    string site;
    site = number.Text;
    var rs = Application.GetResourceStream(new Uri("def/f" + site + ".html", UriKind.Relative));
    StreamReader sr = new StreamReader(rs.Stream);
    browser.NavigateToString(sr.ReadToEnd());
}
else
{
    MessageBox.Show("Enter Value between 1 to 454");
}

Well, the message should tell you what is wrong. 好吧,这条消息应该告诉你什么是错的。 Text is a string and you are using it as an int . Text是一个string ,您将其用作int

private void search(object sender, RoutedEventArgs e)
{
    int n;
    if(int.TryParse(number.Text, out n))
    {
       if (n < 455)
       {
           var rs = Application.GetResourceStream(new Uri("def/f" + number.Text + ".html", UriKind.Relative));
           StreamReader sr = new StreamReader(rs.Stream);
           browser.NavigateToString(sr.ReadToEnd());
       }
       else
       {
           MessageBox.Show("Enter Value between 1 to 454");
       }
    }
} 

You can't guarantee that the user will type a valid number, so this is one way to check: 您无法保证用户将键入有效的号码,因此这是一种检查方法:

int parsed;

if (int.TryParse(number.Text, out parsed) && parsed < 455)
{
    // Logic to execute when valid
}
else
{
    MessageBox.Show("Enter Value between 1 to 454");
} 

You should use either Convert.ToInt32() or int.TryParse() to convert your string to an integer in order to do the comparison. 您应该使用Convert.ToInt32()int.TryParse()将字符串转换为整数以进行比较。 You can't directly compare strings and integers the way you're doing it. 您无法直接比较字符串和整数的方式。

Common C# question 常见的C#问题

int iNumber = -1;
if(int.TryParse(textBoxNumber.Text, out iNumber))
{
 // iNumber now have value you can check
 // ...
}
else { } // failed to parse string

It should be something like this probably: we should check (num > 0 && num < 455). 应该是这样的:我们应该检查(num> 0 && num <455)。 We should check -ve values as well. 我们也应该检查-ve值。

int num = -1

if(int.TryParse(number.Text, out num))
    {
       if (num > 0 && num < 455)
       {
         //Do here 
       }
       else
       {
           MessageBox.Show("Enter Value between 1 to 454");
       }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM