工具栏收藏夹,导航和存储问题 [英] Favorites to ToolBar, Navigation and Store Problem
问题描述
我正在尝试制作一个包含收藏夹按钮的工具栏.这些按钮保存浏览器.和browser.DocumentTitle.ToString();
我有以下代码:
I''m trying to make a toolbar that holds favorite buttons. The buttons hold the browser.Url.ToString(); and the browser.DocumentTitle.ToString();
I have this code:
private void button1_Click_1(object sender, RoutedEventArgs e)
{
System.Windows.Forms.WebBrowser browser = GetCurrentWebBrowser();
//ButtonBar.Items.Add(newItem: browser.DocumentTitle.ToString());
groupBox1.Visibility = Visibility.Hidden;
var tb = new System.Windows.Controls.ToolBar();
tb = ButtonBar;
var b = new System.Windows.Controls.Button();
var l = new System.Windows.Forms.Label();
l.Text = browser.Url.ToString();
b.CommandParameter = browser.Url.ToString();
b.Content = textBox2.Text;
tb.Items.Add(b);
b.Click += new RoutedEventHandler(b_Click);
b.Tag = browser.Url.ToString();
}
void b_Click(object sender, RoutedEventArgs e)
{
var l = new System.Windows.Forms.Label();
var b = new System.Windows.Controls.Button();
ButtonBar.Items.ToString();
System.Windows.Forms.WebBrowser browser = GetCurrentWebBrowser();
ERROR HERE >>>>>>>>>>> browser.Navigate(b.Tag.ToString());
}
您可以看到我有一些尝试使按钮无法容纳浏览器的网址..
任何建议如何使我创建的按钮都同时包含browser.Url和TextBox作为内容?
如果我不太清楚,请告诉我;)
当我运行此代码时,我得到对象引用未设置为对象的实例".问题出在b.Tag.ToString();
You can see that i have some failed attempts to make the Buttons to hold the browser''s Url..
Any suggestion how to make the buttons i create to hold both the browser.Url from the textBox and the browser.DocumentText as Content?
If im not clear enough just tell me ;)
When i run this code i get "Object reference not set to an instance of an object." the problem is at the Navigation point with the b.Tag.ToString();
推荐答案
的导航点上,您的代码中有几个问题,但您报告的错误是由创建新按钮然后尝试使用它的Tag
属性为空的事实. (新按钮未为其标签分配任何内容).
There are several problems in your code but the error you are reporting is caused by the fact that you create a new button and then try to use it''sTag
property which is null. (a new button doesn''t have anything assigned to it''s tag).
void b_Click(object sender, RoutedEventArgs e)
{
var l = new System.Windows.Forms.Label();
var b = new System.Windows.Controls.Button(); // <====== b = a new button here (empty Tag)
ButtonBar.Items.ToString();
System.Windows.Forms.WebBrowser browser = GetCurrentWebBrowser();
browser.Navigate(b.Tag.ToString()); // <======= Try to use Tag here and it's null
}
您可能想要类似
的东西
you probably want something like
Button b = sender as System.Windows.Controls.Button;
if (b != null)
{
// rest of your code here
}
这篇关于工具栏收藏夹,导航和存储问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!