Click here to Skip to main content
15,888,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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());
            
      }


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 ;)

EDIT: 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();
Posted
Updated 22-Feb-11 4:29am
v4
Comments
Sandeep Mewara 22-Feb-11 9:18am    
If you use VS Debugger, it would just take a minute to find where exactly that error happens.

You must be using some property of an object that is null.
Hej då 22-Feb-11 9:29am    
Do you have any idea of an object that can hold this strings for the buttons that this code create? u can see that i bolded where the error occurs :)
Henry Minute 22-Feb-11 10:18am    
Unfortunately bolding doesn't show up very well in code snippets.

Better to add a comment at the end of the problem statement, like:
browser.Navigate(b.Tag.ToString()); // <++++++++++++++++++++ error here +++++++++++++++
or similar. :)

1 solution

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's Tag property which is null. (a new button doesn't have anything assigned to it's tag).
C#
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
C#
Button b = sender as System.Windows.Controls.Button;
if (b != null)
{
    // rest of your code here
}
 
Share this answer
 
Comments
Hej då 22-Feb-11 10:35am    
i highly appreciate your answer :)

Fixed it :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900