简体   繁体   中英

Windows 7 got error. Net framework application 4.0 (written with WinXP SP3)

What am I doing wrong? I have application, which run normally, but when I press button in Win 7 (search in Google) that assigns

hl.NavigateUri = new Uri("http://www.google.com.ua/#hl=ru&source=hp&biw=1280&bih=933&q=");

it Crashes. What am I doing wrong? On Windows XP SP3 machine it normally works..

private void button1_Click(object sender, RoutedEventArgs e)
{
    try
    {
        bool google = true;

        if (((Button)sender).Content.ToString() == "Google")
            google = true;
        else
            google = false;
        Run run1 = new Run(textBox1.Text);
        Hyperlink hl = new Hyperlink(run1);
        TextBlock tb = new TextBlock();
        //Label label1 = new Label();
        //label1.Content = textBox1.Text;
        //label1.Tag = ;
        if (google)
            hl.NavigateUri = new Uri("http://www.google.com.ua/#hl=ru&source=hp&biw=1280&bih=933&q=");
        else
            hl.NavigateUri = new Uri("http://yandex.ua/yandsearch?text=");
        hl.Tag = i;
        hl.Click += new RoutedEventHandler(hl_Click);
        tb.Inlines.Add(hl);
        tb.Inlines.Add(" ");
        wrapPanel1.Children.Add(tb);
        if (google)
        {
            col1_links.Add("http://www.google.com.ua/#hl=ru&source=hp&biw=1280&bih=933&q=" + HttpUtility.UrlEncode(textBox1.Text));
        }
        else
        {
            col1_links.Add("http://yandex.ua/yandsearch?text=" + HttpUtility.UrlEncode(textBox1.Text));
        }
        col1_texts.Add(textBox1.Text);
        webBrowser1.Navigate(col1_links[i].ToString());
        i++;
        SaveData();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

I got the following error:

在此处输入图片说明

I can rewrite your code in a few lines:

private void button1_Click(object sender, RoutedEventArgs e)
{
    try
    {
        var google = ((Button)sender).Content.ToString() == "Google";
        var run1 = new Run(textBox1.Text);
        var hl = new Hyperlink(run1)
        {
            NavigateUrl = google ? "http://www.google.com.ua/#hl=ru&source=hp&biw=1280&bih=933&q=" :
                                   "http://yandex.ua/yandsearch?text=";
            Tag = i;
        };
        var tb = new TextBlock();
        hl.Click += new RoutedEventHandler(hl_Click);
        tb.Inlines.Add(hl);
        tb.Inlines.Add(" ");
        wrapPanel1.Children.Add(tb);
        col1_links.Add(hl.NavigateUrl + HttpUtility.UrlEncode(textBox1.Text));
        col1_texts.Add(textBox1.Text);
        webBrowser1.Navigate(col1_links[i].ToString());
        i++;
        SaveData();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Try to be more professional ;) . First of all, NavigateUrl is "string" (not Uri), try to use this (not NavigateUri). Second thing is there is no "i" variable, where is it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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