.insert is inserting 2 entries instead of one... please help.
-
tabControl1.TabPages.Insert(tabControl1.TabPages.Count, newtabpage); MessageBox.Show(Convert.ToString(tabControl1.TabPages.IndexOf(newtabpage)));
i use a breakpoint on the first line... and when i do, tabcontrol1.tabpages.count comes up as 5, therefor it should be inserting this new tabpage @ index 5... but the next line shows a messagebox, showing which index the tabpage i JUST added is at, and shows 0. the visual output of this, is that it creates 2 tabpages one at index 0, or at the front or left side, and one at the back or right. i don't understand how a single line of code could insert 2 tabpages... because of the breakpoint and messagebox i don;t think the rest of the function is relevant but i'll post it anyways, because i really just want some help on this.public void addatab(string title, string url, string tooltip) { TabPage newtabpage = blanktabpage; foreach (WebBrowser i in newtabpage.Controls) { i.Name = title; } newtabpage.Name = "tab" + title; newtabpage.Text = title; newtabpage.Tag = url; newtabpage.ToolTipText = tooltip; tabControl1.TabPages.Insert(tabControl1.TabPages.Count, newtabpage); MessageBox.Show(Convert.ToString(tabControl1.TabPages.IndexOf(newtabpage))); }
also i tried using .add first, and that produced the same thing... i used .insert so i could blatently point out the conundrum =P i've had the problem posted on microsofts site... msdn or something like thatm, but didnt get a response for a month... and i posted on my university forum, but the one person who kept helping me out, appears to be taking a break from the forums... so i've finally came here, because i know this place has some notbad tutorials and such, it's gotta have some smart people ;) also while im waiting i'll look more indepth of what exactly insert and add does for collections, to see if i can get an awnser there. FFXI ftw beatmania IIDX ftw Soccer ftw -
tabControl1.TabPages.Insert(tabControl1.TabPages.Count, newtabpage); MessageBox.Show(Convert.ToString(tabControl1.TabPages.IndexOf(newtabpage)));
i use a breakpoint on the first line... and when i do, tabcontrol1.tabpages.count comes up as 5, therefor it should be inserting this new tabpage @ index 5... but the next line shows a messagebox, showing which index the tabpage i JUST added is at, and shows 0. the visual output of this, is that it creates 2 tabpages one at index 0, or at the front or left side, and one at the back or right. i don't understand how a single line of code could insert 2 tabpages... because of the breakpoint and messagebox i don;t think the rest of the function is relevant but i'll post it anyways, because i really just want some help on this.public void addatab(string title, string url, string tooltip) { TabPage newtabpage = blanktabpage; foreach (WebBrowser i in newtabpage.Controls) { i.Name = title; } newtabpage.Name = "tab" + title; newtabpage.Text = title; newtabpage.Tag = url; newtabpage.ToolTipText = tooltip; tabControl1.TabPages.Insert(tabControl1.TabPages.Count, newtabpage); MessageBox.Show(Convert.ToString(tabControl1.TabPages.IndexOf(newtabpage))); }
also i tried using .add first, and that produced the same thing... i used .insert so i could blatently point out the conundrum =P i've had the problem posted on microsofts site... msdn or something like thatm, but didnt get a response for a month... and i posted on my university forum, but the one person who kept helping me out, appears to be taking a break from the forums... so i've finally came here, because i know this place has some notbad tutorials and such, it's gotta have some smart people ;) also while im waiting i'll look more indepth of what exactly insert and add does for collections, to see if i can get an awnser there. FFXI ftw beatmania IIDX ftw Soccer ftwWell the .Add method seems to work fine for me, although the .Insert doesn't but I'm sorry that I don't have more time to investigate at the moment, if anything crops up I'll let you know.
The Welsh will always support two teams: The Welsh, and anyone playing England :)
-
tabControl1.TabPages.Insert(tabControl1.TabPages.Count, newtabpage); MessageBox.Show(Convert.ToString(tabControl1.TabPages.IndexOf(newtabpage)));
i use a breakpoint on the first line... and when i do, tabcontrol1.tabpages.count comes up as 5, therefor it should be inserting this new tabpage @ index 5... but the next line shows a messagebox, showing which index the tabpage i JUST added is at, and shows 0. the visual output of this, is that it creates 2 tabpages one at index 0, or at the front or left side, and one at the back or right. i don't understand how a single line of code could insert 2 tabpages... because of the breakpoint and messagebox i don;t think the rest of the function is relevant but i'll post it anyways, because i really just want some help on this.public void addatab(string title, string url, string tooltip) { TabPage newtabpage = blanktabpage; foreach (WebBrowser i in newtabpage.Controls) { i.Name = title; } newtabpage.Name = "tab" + title; newtabpage.Text = title; newtabpage.Tag = url; newtabpage.ToolTipText = tooltip; tabControl1.TabPages.Insert(tabControl1.TabPages.Count, newtabpage); MessageBox.Show(Convert.ToString(tabControl1.TabPages.IndexOf(newtabpage))); }
also i tried using .add first, and that produced the same thing... i used .insert so i could blatently point out the conundrum =P i've had the problem posted on microsofts site... msdn or something like thatm, but didnt get a response for a month... and i posted on my university forum, but the one person who kept helping me out, appears to be taking a break from the forums... so i've finally came here, because i know this place has some notbad tutorials and such, it's gotta have some smart people ;) also while im waiting i'll look more indepth of what exactly insert and add does for collections, to see if i can get an awnser there. FFXI ftw beatmania IIDX ftw Soccer ftwOne thing I notice about your function is this line:
TabPage newtabpage = blanktabpage
Where is
blanktabpage
initialized and created? The question is important because I think your code is not actually creating a newTabPage
every time the function is called. The reason for this is that class types are reference types, which means that unless you actually have thenew
keyword in there, you are not creating another instance of the class in memory. I'm not completely sure I have this down properly (I may have been wrong in my explanation), but have a look at this article by Jon Skeet[^] on the distinction between reference types and value types. What this means (I think) for your code is thatnewtabpage
refers to an already existingTabPage
in memory. Changes that you are making with the variablenewtabpage
are affecting whatblanktabpage
points to. I'm not sure that this will necessarily produce the effect you are describing, but it is a problem. Try this instead: at the beginning of youraddatab
function, use thenew
keyword instead, like this:TabPage newtabpage = new TabPage()
Then set the properties of the tab page based on the properties of
blanktabpage
and see what happens when you callAdd
orInsert
. I'm fairly sure what I have said in this post is correct concerning reference types, but if I am wrong, I would welcome a correction from anyone who knows better. Sincerely, Alexander Wiseman -
One thing I notice about your function is this line:
TabPage newtabpage = blanktabpage
Where is
blanktabpage
initialized and created? The question is important because I think your code is not actually creating a newTabPage
every time the function is called. The reason for this is that class types are reference types, which means that unless you actually have thenew
keyword in there, you are not creating another instance of the class in memory. I'm not completely sure I have this down properly (I may have been wrong in my explanation), but have a look at this article by Jon Skeet[^] on the distinction between reference types and value types. What this means (I think) for your code is thatnewtabpage
refers to an already existingTabPage
in memory. Changes that you are making with the variablenewtabpage
are affecting whatblanktabpage
points to. I'm not sure that this will necessarily produce the effect you are describing, but it is a problem. Try this instead: at the beginning of youraddatab
function, use thenew
keyword instead, like this:TabPage newtabpage = new TabPage()
Then set the properties of the tab page based on the properties of
blanktabpage
and see what happens when you callAdd
orInsert
. I'm fairly sure what I have said in this post is correct concerning reference types, but if I am wrong, I would welcome a correction from anyone who knows better. Sincerely, Alexander Wisemanthanks for the idea, i tried changing it to new tabpage(); then making the tabpage equal to my blanktabpage, but that didn't solve it. i also read the article, and although it isn't 100% clear to me right now, because of how many times he says value to reference and reference to value =P, i still figure theirs no way my blanktabpage is changing value, because it is initialized in load_form. ... ... OK IGNORE EVERYTHING I JUST SAID! i just tried creating a fresh tabpage, and new webbrowser, and setup the webbrowser and added it to the tabpage, then setup the tabpage and added it to the tabcontrol and that worked! no multipuls of tabpages!... i still have no idea how 2 tabpages were added from a single .insert call, but whatever XD thanks alot for your help alexander :D w00t i can finally finish my project!!!
-
thanks for the idea, i tried changing it to new tabpage(); then making the tabpage equal to my blanktabpage, but that didn't solve it. i also read the article, and although it isn't 100% clear to me right now, because of how many times he says value to reference and reference to value =P, i still figure theirs no way my blanktabpage is changing value, because it is initialized in load_form. ... ... OK IGNORE EVERYTHING I JUST SAID! i just tried creating a fresh tabpage, and new webbrowser, and setup the webbrowser and added it to the tabpage, then setup the tabpage and added it to the tabcontrol and that worked! no multipuls of tabpages!... i still have no idea how 2 tabpages were added from a single .insert call, but whatever XD thanks alot for your help alexander :D w00t i can finally finish my project!!!
I'm glad you figured it out! You said in the first part of your response: "i tried changing it to new tabpage(); then making the tabpage equal to my blanktabpage" Setting the tabpage equal to
blanktabpage
would defeat the purpose, because you are changing what your variable refers to. The main point was to create a wholly new tab page and add that to the tab control. I see that is what you did, which is great. As far as two tab pages getting added, I have an idea about it. I did not want to mention it in my previous post because I wasn't sure if this was the issue. Here goes: I was thinking that perhaps you did not actually have two tab pages being added, but rather the first tab page in your control changing to look exactly the same as the other tab page, since you only really have one tab page in memory. Thus, when your function got called, the tab page in memory got changed to look exactly the same as the "new" tab page you were adding. You could verify this by checking how many tab pages total there are before and after the call to your function. If there are two more than before, then I am simply wrong. If there is only one more tab page total, then I think what I said is happening. If you get a chance, could you check it out, I would like to know. This is a really interesting issue! Sincerely, Alexander Wiseman -
I'm glad you figured it out! You said in the first part of your response: "i tried changing it to new tabpage(); then making the tabpage equal to my blanktabpage" Setting the tabpage equal to
blanktabpage
would defeat the purpose, because you are changing what your variable refers to. The main point was to create a wholly new tab page and add that to the tab control. I see that is what you did, which is great. As far as two tab pages getting added, I have an idea about it. I did not want to mention it in my previous post because I wasn't sure if this was the issue. Here goes: I was thinking that perhaps you did not actually have two tab pages being added, but rather the first tab page in your control changing to look exactly the same as the other tab page, since you only really have one tab page in memory. Thus, when your function got called, the tab page in memory got changed to look exactly the same as the "new" tab page you were adding. You could verify this by checking how many tab pages total there are before and after the call to your function. If there are two more than before, then I am simply wrong. If there is only one more tab page total, then I think what I said is happening. If you get a chance, could you check it out, I would like to know. This is a really interesting issue! Sincerely, Alexander Wisemanya i think that is what was happening too =P i just couldn't understand why if you made variable b equal to a, why changing b changed a... i could understand if b was a reference to a, that changing a would change b, but i would have never figured it to be the opposite x.x... in any case, i've found .equals() is the perfect solution... cause it allows me to copy the same specification as the tabpage, without changing the original ^^