我浏览器中的主要C#错误

use*_*065 0 c# visual-studio-2010 winforms

以下是我在调试时遇到的错误:

错误1字符文字中的字符太多错误2'System.Windows.Forms.WebBrowser.Navigate(string)'的最佳重载方法匹配有一些无效的参数错误3参数1:无法从'char'转换为'string'

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BroZer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Reload_Click(object sender, EventArgs e)
        {
            webBrowser1.Refresh();
        }

        private void Go_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(textBox1.Text);
        }

        private void Back_Click(object sender, EventArgs e)
        {
            webBrowser1.GoBack();
        }

        private void Forward_Click(object sender, EventArgs e)
        {
            webBrowser1.GoForward();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            webBrowser1.Navigate('https://www.google.com/search?&ie=UTF-8&q= + (textBox1.Text)');
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所有错误都在第41行.

Ed *_* S. 7

'https://www.google.com/search?&ie=UTF-8&q= +(textBox1.Text)'

我知道你认为应该怎么做,但这是错误的.

'x'表示一个字符文字(即,一个实例char,不是string.在这种情况下,是字符x),但你像字符串一样使用它,然后想插入textbox1.Text它,但C#根本不支持这种类型的直接插值.你想写:

// concatenate a string literal and a string variable
"https://www.google.com/search?&ie=UTF-8&q=" + textBox1.Text;
Run Code Online (Sandbox Code Playgroud)

接下来的两个错误消息是第一个错误消息的直接结果.这里的错误信息非常清楚,你可以很好地搜索它们的含义并尝试推断出问题的根本原因.


hea*_*150 5

改变线

webBrowser1.Navigate('https://www.google.com/search?&ie=UTF-8&q= + (textBox1.Text)');
Run Code Online (Sandbox Code Playgroud)

webBrowser1.Navigate(string.Format("https://www.google.com/search?&ie=UTF-8&q={0}", textBox1.Text);
Run Code Online (Sandbox Code Playgroud)

这是因为Navigate方法需要String或Uri作为您通过char发送的参数(WebBrowser.Navigate Method @ MSDN).