Windows Develop Bookmark and Share   
 index > Windows Forms General > Tab Control in c# using ado.net
 

Tab Control in c# using ado.net

I need to create a tabcontrol that has tabpages .i need to add and remove the tabpages this fuctionality

should be written with the help of two buttons add and remove.while openining these tab pages the

order which i follow should be same while removing these tabpages that is ineed to use the

swaping technique in the button remove.While searching the sites ive saw this logic from http://focuswindows.blogspot.com

/2008/05/how.html but i dont know whetether it is correct or not even though it is correct i am unable to apply it.any suggestions plz or any sample code u have plz send me.

thanks in advance


swpa  Tuesday, March 03, 2009 1:44 PM
Hi Swpa,

Thank you for your reply! I understand what you really need now.

As ABitSmart has suggested, you'd betteruse List<T> instead of Stack to manage the order that the tabpages are clicked, because using List<T> you can rearrange the order when the user navigates through the tabpages. In this case, you needn't use Stack in your application any more.

I suggest that you handle the Selected event of the TabControl and in the event handler rearrange the order. The following is a sample.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.tabControl1.Selected += new TabControlEventHandler(tabControl1_Selected);
}

List<TabPage> _cache = new List<TabPage>();

void tabControl1_Selected(object sender, TabControlEventArgs e)
{
if (e.TabPage != null)
{
if (_cache.Contains(e.TabPage))
{
_cache.Remove(e.TabPage);
}
_cache.Add(e.TabPage);
}
}

private void button1_Click(object sender, EventArgs e)
{
TabPage page = new TabPage(this.textBox1.Text);
this.tabControl1.TabPages.Add(page);
}

private void button2_Click(object sender, EventArgs e)
{
if (_cache.Count > 0)
{
// note that we need tounsubscribethe Selected eventbeforeremoving the tabpagefrom
// the TabControl,becauseremovingthe tabpagefrom the TabControl will cause the Selected
// eventof the TabControl to occurwhichin turnwouldrearrange the orderand thisisn't
// what we expect
this.tabControl1.Selected -=new TabControlEventHandler(tabControl1_Selected);
this.tabControl1.TabPages.Remove(_cache[_cache.Count -1]);
// subscribe the Selectedevent again
this.tabControl1.Selected += new TabControlEventHandler(tabControl1_Selected);
_cache.RemoveAt(_cache.Count -1);
}
}
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu

  • Marked As Answer byswpa Friday, March 06, 2009 6:14 AM
  •  
Linda Liu  Friday, March 06, 2009 4:02 AM

Hi Swpa,

You can use a variable to maintain the current number of the tabpage. The following is a sample:

int pageNumber = 1;
private void button1_Click(object sender, EventArgs e)
{
// TabPage page = new TabPage(this.textBox1.Text);
TabPage page = new TabPage("TabPage" + pageNumber.ToString());
this.tabControl1.TabPages.Add(page);
pageNumber++;
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu

  • Marked As Answer byswpa Tuesday, March 10, 2009 4:58 AM
  •  
Linda Liu  Monday, March 09, 2009 3:30 AM
I suppose you are using the code shared by LindaLiu. I am editing the remove button handler,

private void button2_Click(object sender, EventArgs e)
{
if (_cache.Count > 0)
{
// note that we need tounsubscribethe Selected eventbeforeremoving the tabpagefrom
// the TabControl,becauseremovingthe tabpagefrom the TabControl will cause the Selected
// eventof the TabControl to occurwhichin turnwouldrearrange the orderand thisisn't
// what we expect
this.tabControl1.Selected -=new TabControlEventHandler(tabControl1_Selected);
this.tabControl1.TabPages.Remove(_cache[_cache.Count -1]);
// subscribe the Selectedevent again
this.tabControl1.Selected += new TabControlEventHandler(tabControl1_Selected);
_cache.RemoveAt(_cache.Count -1);
}

if (_cache.Count > 0)
{
this.tabControl1.SelectedTab =_cache[_cache.Count - 1];
}

}


I have added some code to make the last Tab of the list as the selected tab. This should work.
  • Marked As Answer byswpa Tuesday, March 10, 2009 6:35 AM
  •  
ABitSmart  Tuesday, March 10, 2009 6:12 AM
The link which you provided shows how to add and remove of a tabpage. Since you need a pattern for adding and removing, you can always add to the end and remove from the end i.e
- when the ADD button is hit you add a new tab page to the end of the existing ones
- when the REMOVE button is hit you remove the tab which is at the end


ABitSmart  Tuesday, March 03, 2009 4:00 PM
Thanks AbitSmart for ur response but my requirement is like this

But i need to follow a pattern that when i start opening the tabpages supose tr are five tab pages and i open 1,3,5

tabpages in order i need to preserve this order becuase while removing ineed to remove when i click remove the same order

should be folowed while removing that is the order while opening the tabpages should be preserved so that while

removing ineed to remove the recent tabpage opened.So how do i preserve the oreder can u please give me any

sugestions or sample code.And i shouldnt use any backend (data base) support to preserve the order.My platform c# using ado.net.

Thanks in advance
swpa  Wednesday, March 04, 2009 5:30 AM
I guess there isn't anything inherently supported for this. You will need to maintian a cache of the tab browsing and manipulate it while the user interacts. Something like a stack. When the user clicks on a tab it goes to the top of the stack and when deleting taking the element at the top of the stack is selected.
One thing to keep in mind is, if you follow this approach, delete all entries for the particular page from the stack when you delete it from the top of the stack.

Just an idea from top of my mind. Hope it helps.
  • Proposed As Answer byABitSmart Wednesday, March 04, 2009 5:41 AM
  •  
ABitSmart  Wednesday, March 04, 2009 5:41 AM
Thanks for ur response Abit Smart i guess i have to follow the last in first out technique so can u plz help me out to with the

coding for given 'n' tabpages in C# using ado.net i am finding it dificult.
swpa  Wednesday, March 04, 2009 6:11 AM
This might help http://msdn.microsoft.com/en-us/library/system.collections.stack.aspx
  • Proposed As Answer byABitSmart Wednesday, March 04, 2009 6:25 AM
  •  
ABitSmart  Wednesday, March 04, 2009 6:25 AM
Thanks for ur response Abit Smart but unable to do it can u send me any other search site or a sample code.http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=65. ive used also this lifo sample in c# to help with my

requirement but unable to do it.Can u plz change the code of this sample according to my requirement becoz i am fining a

problem in implementing in it.while adding these these tabpages i should automaticaly get tabpage6,tabpage7

that is i need to use a for loop and count to increment how do i do that.

thanks in advance
swpa  Wednesday, March 04, 2009 1:31 PM

Hi Swpa,

Based on my understanding, you have a TabControl on the form and would like to add/remove tabpages to and from this TabControl when clicking Buttons on the form. What you want is to remove the tabpages in the order in which they are added. For example, tabpage1 is added to the TabControl and the tabpage2 is added and then tabpage3 is added. When clicking the "Remove" button, tabpage1 is removed. Click the "Remove" button again, and the tabpage2 is removed. If I'm off base, please feel free to let me know.

The feature described above is FIFO(first-in, first-out) rather than LIFO(last-in, first-out). For FIFO, you can use System.Collections.Queue class to manage the objects. The following is a sample. It assumes that you add a TabControl, a TextBox and two Buttons on the form. When running, type title in the TextBox and click the "add" button and a TabPage with the specified title will be added to the TabControl. Click the "remove" button and the TabPage which is added earliest will be removed from the TabControl.

public partial class Form1 : Form
{

public Form1()

{

InitializeComponent();

}

Queue queue = new Queue();

private void button1_Click(object sender, EventArgs e)

{

TabPage page = new TabPage(this.textBox1.Text);

this.tabControl1.TabPages.Add(page);

queue.Enqueue(page);

}

private void button2_Click(object sender, EventArgs e)

{

if (queue.Count > 0)

{

this.tabControl1.TabPages.Remove(queue.Peek() as TabPage);

queue.Dequeue();

}

}

}

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu

Linda Liu  Thursday, March 05, 2009 9:25 AM
Thanks for ur response Linda Liu

but my requirement is i have two buttons add a nd remove add buton where i add 'n '(any no of

tabpages )where i need to use count , this functionality written in add button. Now when i open(not add )the tabpages ineed to preserve

the order using the stack.that is when suppose i have five tab pages open in the order tabpage1,tabpage3,tabpage5 order preserved.

while removing the tab pages ineed to remove the in the reverse order Tabpage5,tabpage3,tabpage1using lifo(last in

firstout) technique that is the latest page opened should be removed .

so how do i do this i am finding it aproblem implemting plz send me a sample.

thanks in advance




yes
swpa  Thursday, March 05, 2009 9:52 AM

Hi Swpa,

The modified sample:

Stack stack = new Stack();

private void button1_Click(object sender, EventArgs e)

{

TabPage page = new TabPage(this.textBox1.Text);

this.tabControl1.TabPages.Add(page);

stack.Push(page);

}

private void button2_Click(object sender, EventArgs e)

{

if (stack.Count > 0)

{

this.tabControl1.TabPages.Remove(stack.Peek() as TabPage);

stack.Pop();

}

}

If you have any question, please feel free to let me know.

Sincerely,
Linda Liu

Linda Liu  Thursday, March 05, 2009 10:43 AM
thanks for ur response Linda Liu but according to the code uve send me while adding the tabpages in the order tabpage3,tabpage4,tabpage5


i remove in the order 5,4,3 that is in the order i add.



But my functionality should be in such way that does not deal with adding it should deal with while when i open that is while clicking

tabpages suppose i have 5 tabpages after adding them.now when i open that is click in the order

tabpage3,tabpage5,tabpage4while remove it tabpage4,tabpage5,tabpage3 that is the order of clicking should be

preserved so that i can remove those tabpages in the reverse order(lifo).I shouldnt

deal with it in the order of adding .It should deal with clicking the tabpages that is opening (clicking) order should be

preserved. i hope u understood my requirement if u have any doughts let me know.

thanks in advance u ar really helping me alot .




yes
swpa  Thursday, March 05, 2009 11:32 AM
Try this,
List<TabPage>_cache=newList<TabPage>();
//calledwheneveratabisselected.addsthecurrentselecteditemaslastitem
privatevoidUpdateCache(TabPagecurrent)
{
if(_cache.Contains(current))
{
_cache.Remove(current);//ifitemisalreadypresent,removeitandadditaslast
}
_cache.Add(current);
}
//alwaysremovethelastitem
privatevoidbuttonRemove_Click(objectsender,EventArgse)
{
if(0<_cache.Count)
{
_cache.RemoveAt(_cache.Count-1);
}
}

The only thing you need to do is call the UpdateCache method on the tab change and pass it the newly selected tabpage.

Hope this helps/
  • Proposed As Answer byABitSmart Thursday, March 05, 2009 11:38 AM
  •  
ABitSmart  Thursday, March 05, 2009 11:38 AM
thanks for ur response Linda Liu this is wat ive done but it isnt working


wat changs i have to do can u pls help me out

public partial class Form1 : Form
{


private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabControl tabControl1;
Stack stack = new Stack();
List<TabPage> _cache = new List<TabPage>();
public Form1()
{
InitializeComponent();
}

private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();

this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.tabControl1.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);

this.tabControl1.Location = new System.Drawing.Point(16, 16);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(1120, 672);
this.tabControl1.TabIndex = 0;
//
// tabPage1
//
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Size = new System.Drawing.Size(1112, 646);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";



this.button1.Location = new System.Drawing.Point(194, 722);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(395, 722);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(360, 14);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1156, 774);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "Form1";
this.tabControl1.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();

UpdateCache();
}

private void button1_Click(object sender, EventArgs e)
{
TabPage page = new TabPage(this.textBox1.Text);

this.tabControl1.TabPages.Add(page);

stack.Push(page);
UpdateCache();
}

private void UpdateCache()
{
TabPage page = new TabPage();
if (_cache.Contains(page))
{
_cache.Remove(page); //if item is already present, remove it and add it as last
}

_cache.Add(page);

}

private void button2_Click(object sender, EventArgs e)
{
//if (stack.Count > 0)
//{

// this.tabControl1.TabPages.Remove(stack.Peek() as TabPage);

// stack.Pop();

//}

if (0 < _cache.Count)
{
_cache.RemoveAt(_cache.Count - 1);
}


}




}
}
thanks in advance.

yes
swpa  Thursday, March 05, 2009 11:56 AM
Hi Swpa,

Thank you for your reply! I understand what you really need now.

As ABitSmart has suggested, you'd betteruse List<T> instead of Stack to manage the order that the tabpages are clicked, because using List<T> you can rearrange the order when the user navigates through the tabpages. In this case, you needn't use Stack in your application any more.

I suggest that you handle the Selected event of the TabControl and in the event handler rearrange the order. The following is a sample.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.tabControl1.Selected += new TabControlEventHandler(tabControl1_Selected);
}

List<TabPage> _cache = new List<TabPage>();

void tabControl1_Selected(object sender, TabControlEventArgs e)
{
if (e.TabPage != null)
{
if (_cache.Contains(e.TabPage))
{
_cache.Remove(e.TabPage);
}
_cache.Add(e.TabPage);
}
}

private void button1_Click(object sender, EventArgs e)
{
TabPage page = new TabPage(this.textBox1.Text);
this.tabControl1.TabPages.Add(page);
}

private void button2_Click(object sender, EventArgs e)
{
if (_cache.Count > 0)
{
// note that we need tounsubscribethe Selected eventbeforeremoving the tabpagefrom
// the TabControl,becauseremovingthe tabpagefrom the TabControl will cause the Selected
// eventof the TabControl to occurwhichin turnwouldrearrange the orderand thisisn't
// what we expect
this.tabControl1.Selected -=new TabControlEventHandler(tabControl1_Selected);
this.tabControl1.TabPages.Remove(_cache[_cache.Count -1]);
// subscribe the Selectedevent again
this.tabControl1.Selected += new TabControlEventHandler(tabControl1_Selected);
_cache.RemoveAt(_cache.Count -1);
}
}
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu

  • Marked As Answer byswpa Friday, March 06, 2009 6:14 AM
  •  
Linda Liu  Friday, March 06, 2009 4:02 AM
Tanks u very much LindaLiu it is excelently working.Thanks a lot i woudnt be able to do it without u.

But i have a dought while adding the tabpages i am giving them name to the textbox instead of that suppose as a default

i have one" tabpage1" i just increment it so that while adding the tabpages using the add button i get tabpage2 ,tabpage3,tabpage4,tabpage5 automatocally while adding instead of giving the name in the textbox.

i thing ineed to use a loop but i am failing in that can u plz help me out.


Thanks for being patient with me

yes
swpa  Friday, March 06, 2009 6:14 AM

Hi Swpa,

You can use a variable to maintain the current number of the tabpage. The following is a sample:

int pageNumber = 1;
private void button1_Click(object sender, EventArgs e)
{
// TabPage page = new TabPage(this.textBox1.Text);
TabPage page = new TabPage("TabPage" + pageNumber.ToString());
this.tabControl1.TabPages.Add(page);
pageNumber++;
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu

  • Marked As Answer byswpa Tuesday, March 10, 2009 4:58 AM
  •  
Linda Liu  Monday, March 09, 2009 3:30 AM
Thanks lindaliu for ur help its working but one last problem with this requirement that is as u know the my program works in such a way i have tabpages 1,2,3,4,5

they get clicked in the order 2,4,5 while removing they get removed in the order 5,4,2 now my problem is ineed to set the

focus that is while remove i click on the remove button i first remove page 5, then automaticaly the focus to 4 then

removed and to 2 then removed ineed to set the focus automaticaly on the next page that is going to get removed so that the

user can understand which page is removed next. can u plz send me any sample acording my program.

Ive end up in loops the whole weekend but couldnt find any solution can u plz help me out.



Thanks in advance.

yes
swpa  Tuesday, March 10, 2009 4:58 AM
After you remove, you can set the focus on the last tabpage in the List using the "Focus" method. You will have to put a check to see if there are any elements remaining in the list before you set focus.

ABitSmart  Tuesday, March 10, 2009 5:09 AM
Thanks for ur response AbitSmart but ive used the focus method already but still unable to implement it .

Can u please send me any sample acording to my program given above.


Thanks in advance.

yes
swpa  Tuesday, March 10, 2009 5:48 AM
I suppose you are using the code shared by LindaLiu. I am editing the remove button handler,

private void button2_Click(object sender, EventArgs e)
{
if (_cache.Count > 0)
{
// note that we need tounsubscribethe Selected eventbeforeremoving the tabpagefrom
// the TabControl,becauseremovingthe tabpagefrom the TabControl will cause the Selected
// eventof the TabControl to occurwhichin turnwouldrearrange the orderand thisisn't
// what we expect
this.tabControl1.Selected -=new TabControlEventHandler(tabControl1_Selected);
this.tabControl1.TabPages.Remove(_cache[_cache.Count -1]);
// subscribe the Selectedevent again
this.tabControl1.Selected += new TabControlEventHandler(tabControl1_Selected);
_cache.RemoveAt(_cache.Count -1);
}

if (_cache.Count > 0)
{
this.tabControl1.SelectedTab =_cache[_cache.Count - 1];
}

}


I have added some code to make the last Tab of the list as the selected tab. This should work.
  • Marked As Answer byswpa Tuesday, March 10, 2009 6:35 AM
  •  
ABitSmart  Tuesday, March 10, 2009 6:12 AM
thanks for AbitSmart its excelently working


that little change made a lot of difference.

Thanks to u once again and to all the forrum members for helping me out.

yes
swpa  Tuesday, March 10, 2009 6:36 AM

You can use google to search for other answers

Custom Search

More Threads

• Authentication Sample in Windows App.
• Need Title of Creator Window
• Visual Web Express edition 2005 Advanced SQL Generation Options Are Not Accessable
• Restricting colors for a custom control?
• Duplicating MS Access Continuous Subform Functionality in .Net
• Widbey's Stable! Very important...
• System.Windows.Forms.SendKeys.Send and SHIFT
• How can I open a form, and not able to exit from it until I press a button ? C#
• Detecting a window that has had FlashWindow called on it.
• Problem in Crystal Reports V9.0