我正在使用Visual Studio 2010中的Web浏览器,但我无法将选项卡的名称更新为网站名称。例如,当你访问像CNN.Com这样的网站时,我希望标签也说“cnn.com”。顺便说一下,该项目没有使用默认的WebBrowser表单。请以最简单的方式解释它,因为我是C#的新手(刚从C ++和Java转移),所以我不熟悉使用Windows窗体。谢谢。任何帮助表示赞赏。
以下是问题的图片:http://postimage.org/image/5ym4yx0pt/
...
public Form1()
{
InitializeComponent();
}
int i = 1;
private void Form1_Load(object sender, EventArgs e)
{
WebBrowser Browse = new WebBrowser();
//Load a tab when loading form
tabControl1.TabPages.Add("Tab");//problem
tabControl1.SelectTab(i - 1);
Browse.Name = "Lithium Browser";
Browse.Dock = DockStyle.Fill;
tabControl1.SelectedTab.Controls.Add(Browse);
i++;
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("www.google.com");
}
private void button1_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(textBox1.Text);
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
WebBrowser Browse = new WebBrowser();
tabControl1.TabPages.Add("Tab"); //problem
tabControl1.SelectTab(i - 1);
Browse.Name = "Lithium Browser";
Browse.Dock = DockStyle.Fill;
tabControl1.SelectedTab.Controls.Add(Browse);
i++;
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
i = i- 1;
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoBack();
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoForward();
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoHome();
}
private void toolStripButton6_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Refresh();
}
private void toolStripButton7_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Stop();
}
private void yahooSearchToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripDropDownButton1.Text = yahooSearchToolStripMenuItem.Text;
}
private void youtubeSearchToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripDropDownButton1.Text = youtubeSearchToolStripMenuItem.Text;
}
private void googleSearchToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripDropDownButton1.Text = googleSearchToolStripMenuItem.Text;
}
private void toolStripButton8_Click(object sender, EventArgs e)
{
if (toolStripDropDownButton1.Text == googleSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.google.com/search?q=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == yahooSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://search.yahoo.com/search?p=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.youtube.com/results?search_query=" + toolStripTextBox1.Text);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
//add KeyUp event for detecting 'Enter' key
//navigate to specified URL withoud pressing the 'Go' button
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(textBox1.Text);
}
}
private void toolStripTextBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (toolStripDropDownButton1.Text == googleSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.google.com/search?q=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == yahooSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://search.yahoo.com/search?p=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://www.youtube.com/results?search_query=" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://en.wikipedia.org/wiki/" + toolStripTextBox1.Text);
}
if (toolStripDropDownButton1.Text == youtubeSearchToolStripMenuItem.Text)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate("http://en.wikipedia.org/wiki/" + toolStripTextBox1.Text);
}
}
}
private void newTabToolStripMenuItem_Click(object sender, EventArgs e)
{
WebBrowser Browse = new WebBrowser();
tabControl1.TabPages.Add("Tab");
tabControl1.SelectTab(i - 1);
Browse.Name = "Lithium Browser";
Browse.Dock = DockStyle.Fill;
tabControl1.SelectedTab.Controls.Add(Browse);
i++;
}
private void closeTabToolStripMenuItem_Click(object sender, EventArgs e)
{
tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
i = i - 1;
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
}
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.ShowDialog();
}
private void printPreviewDialog1_Load(object sender, EventArgs e)
{
}
private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
{
//Associate PrintPreviewDialog with PrintDocument.
printPreviewDialog1.Document = printDocument1;
// Show PrintPreview Dialog
printPreviewDialog1.ShowDialog();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Exit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Application.Exit();
}
}
// Bring up 'Print Dialog'
private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
{
PageSetupDialog pageSetup = new PageSetupDialog();
pageSetup.PrinterSettings = new System.Drawing.Printing.PrinterSettings();
pageSetup.PageSettings = new System.Drawing.Printing.PageSettings();
pageSetup.EnableMetric = false;
pageSetup.ShowDialog();
}
private void stopToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Stop();
}
private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Refresh();
}
private void homeToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoHome();
}
private void previousPageToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoBack();
}
private void nextPageToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoForward();
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 about = new Form2();
about.Show();
}
private void calenderToolStripMenuItem_Click(object sender, EventArgs e)
{
calenForm cal = new calenForm();
cal.Show();
}
}
}
...........
答案 0 :(得分:2)
假设webbrowser是内置的webbrowser,您可以在每次更改webbrowser文档标题时触发OnDocumentTitleChanged事件以更改选项卡文本。
要执行此操作,在表单加载事件中,在声明浏览后,开始键入browse.DocumentTitleChanged + =并且工具提示应该出现“tab”以插入此代码或其他内容。只需选项卡两次,visual studio将为您插入一个新方法,其中包含“抛出新的未示例异常”;线。删除该行并将其替换为将标签文本更改为浏览器文档标题的代码。
如果您需要更多信息,我建议您在这里:http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx
虽然我确信使用事件是最好的解决方案。事件被设计为在某些重要的节目“事件”发生时执行,并且改变网页就是其中之一,事件似乎是合适的解决方案。 (事件本质上是C#相当于C ++的函数指针,如果有帮助的话)。
答案 1 :(得分:1)
设置两个标签中包含的页面文本的HTML标题标签将显示在显示网页的选项卡中。
有关在后面的代码中设置标题的更多信息,请参阅以下内容 How to use Eval in codebehind to set Page.Title
此链接也是
http://www.asprobot.com/ASP.NET/ASPNET-Title-Tag-and-Meta-Tags