Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
private TabItem AddTabItem()
       {
           TabItem tab = new TabItem();



               int count = _tabItems.Count;
               for (int i = 0; i < count; i++)
               {

                   // create new tab item


                   tab.Header = string.Format("Tab {0}", count);
                   tab.Name = string.Format("tab{0}", count);
                   tab.HeaderTemplate = tabDynamic.FindResource("TabHeader") as DataTemplate;

                   WebBrowser wb = new WebBrowser();
                   tab.Content = wb;
                   wb.Source = new Uri("http://www.google.com");


                   // insert tab item right before the last (+) tab item
                   _tabItems.Insert(count - 1, tab);
               }


           return tab;


       }


So basically i just need a way on how to get the new instantiated "wb" variable to be used outside this method. this line of code is for my internet browser program that supports multiple tabs. the good news is that it works the bad new is that each tab on my browser contains the same link, and what i mean by that is every tab is identical like if i log in to my Facebook on one tab it will be the same for the other tab, i just need a way to turn each tab to an individual tab. I have tried making the WebBroswer instance variable wb as a global variable but hats what causes the problem, plus i cant assign my Refresh,Forward and Backward buttons to this variable because its inside this method. So i thought that its either i find a way to make the wb variable to become global (if possible) so that i could assign my buttons like refresh etc,. with it being on its own individual tab. This is the last step i need to finish my work :( please Help ASAP i only got a fwe hours left
Posted
Comments
[no name] 15-Jun-14 11:29am    
Each tab's web browser control has the same link because you are setting them all to have the same link. It's unclear from your posting why you think that they are all linked together. You *could* have an array or List of web browser controls if that would make you feel better about it. But I am not sure that you have an actual problem as yet.

Hello friend,
If you want to access wb object from the method where AddTabItem() is called, then I would suggest to declare the wb object from calling method and pass the object as parameter to AddTabItem() function.

In this way, you'll be able to access wb from outside of this method.
- DD
 
Share this answer
 
Comments
Member 10885326 15-Jun-14 12:08pm    
thank you for replying, i apologize i don't quite get what you mean. my apologies sir I'm still kind of new to C#
C# doesn't have Global variables (though the static keyword and the Singleton pattern do give you ways to emulate them) - generally, you don't need them.

Instead, just move the declaration:
C#
private WebBrowser wb = new WebBrowser();
private TabItem AddTabItem()
          {
                TabItem tab = new TabItem();
                int count = _tabItems.Count;
                for (int i = 0; i < count; i++)
                {
                     // create new tab item
              tab.Header = string.Format("Tab {0}", count);
                    tab.Name = string.Format("tab{0}", count);
                    tab.HeaderTemplate = tabDynamic.FindResource("TabHeader") as DataTemplate;
 
                    tab.Content = wb;
                    wb.Source = new Uri("http://www.google.com");
                    // insert tab item right before the last (+) tab item
                    _tabItems.Insert(count - 1, tab);
                }
            return tab;
        }
And wb will be available throughout the class (but not outside it, which is a good thing, honest!)
 
Share this answer
 
Comments
Member 10885326 15-Jun-14 12:06pm    
so i tried your revised code and thank you so much for replying, but its still showing the same website on tab1, tab2 and so on so forth. its still not functioning like it would on any other web browser with multiple tab functions :(
You get the same webbrowser on all your tabs because this is exactly what your code is written to do.
You iterate from zero to your tabs count, and at each step insert in last position a new Tab with the same webbrowser than on every other tab.

You should really put a breakpoint at the beginning of your AddTabItem() method and run it in debug mode; you would instantly understand what is going on.

Finally, if you need to keep several webbrowsers, one for each tab, you need to declare a collection of them; you could use an Array, a List, a Dictionary, whatever, but you need a collection.
Examples:
C#
private List<WebBrowser> _webBrowsers = new List<WebBrowser>();
// OR
private Dictionary<int, WebBrowser> _webBrowsers = new Dictionary<int, WebBrowser>();


But anyway the priority here is to correct your AddTabItem() method. Debug it, execute it line by line, and check for the values of your variables at each step. The solution will come easily after that, and you will have learned an important part of a developer's life : debugging.
 
Share this answer
 
v2

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