user2622989
user2622989

Reputation:

XAML, C# Input Validation Error

I created the textbox with InputScope="Number" Name="site". Whenever the user enters the number in between 1 to 454. It will show the entered number's html file.

For example if user enters 3, it will open def/f3.html. Here' is my problem if user enters 003, it will search for def/f003.html Since I dont have that file, it breaks.

help me to avoid 0's before number

My C# Code:

    private void search(object sender, RoutedEventArgs e)
    {
        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");
        }
    } 

Upvotes: 0

Views: 201

Answers (2)

Naren
Naren

Reputation: 2311

I think this works fine ..

string site;
site = num.ToString();

Upvotes: 0

McGarnagle
McGarnagle

Reputation: 102763

Just use num instead of site:

var rs = Application.GetResourceStream(new Uri("def/f" + num + ".html", UriKind.Relative));

Upvotes: 1

Related Questions