How to add and remove "custom" tabs in C# -
i making application requires tabs (tab-control) added or removed. have done add together , remove tabs fine, have custom buttons instead of using tabs. (this button go the first tab page when clicked):
//this create go tab 1 private void button1_click(object sender, eventargs e) { tabcontrol1.selectedindex = 0; } //this alter mouseenter right image private void button1_mouseenter(object sender, eventargs e) { button1.mouseenter += new eventhandler(button1_mouseenter); this.button1.backgroundimage = ((system.drawing.image)(properties.resources.tab_down)); } //this alter mouseleave right image private void button1_mouseleave(object sender, eventargs e) { button1.mouseleave += new eventhandler(button1_mouseleave); this.button1.backgroundimage = ((system.drawing.image)(properties.resources.tab_norm)); }
however, how create when click "add tab" button creates new tab creates new button tabcontrol1.selectedindex = 1;
in example.
edit
i have done add together new tab (to tabcontrol1):
private void button2_click(object sender, eventargs e) { string title = "tabpage " + (tabcontrol1.tabcount + 1).tostring(); tabpage mytabpage = new tabpage(title); tabcontrol1.tabpages.add(mytabpage); }
this adds new tab on top of existing ones fine. how do creates new button properties of button above makes instead of tabcontrol1.selectedindex = 1;
tabcontrol1.selectedindex = 3;
, goes every time add together new tab? - thanks
after few updates here new version of answer. shows how
addbutton
adds new tabpages
, selects it make tab
command draw closing 'x' characters right of each tab, firefox or visual studio it. make add together button
want it, maybe text="+" , height tabs' height. automatically positioned on tab. may need reposition it, if tab
moved or resized.
in form_load
event tab
set ownerdrawfixed
, each page's text append few blanks create room closing x. code creates class variable closex
hold current x-rectangle , tests in mouseclick
event.
make sure wire tabcontrol1_drawitem
, tabcontrol1_mouseclick
events!
private void cb_addpage_click(object sender, eventargs e) { string title = "tabpage " + (tabcontrol1.tabcount + 1).tostring() + " "; tabpage mytabpage = new tabpage(title); tabcontrol1.tabpages.add(mytabpage); tabcontrol1.selectedtab = mytabpage; } private void form1_load(object sender, eventargs e) { tabcontrol1.drawmode = tabdrawmode.ownerdrawfixed; cb_addpage.top = tabcontrol1.top; cb_addpage.left = tabcontrol1.right - cb_addpage.width; foreach (tabpage tp in tabcontrol1.tabpages) tp.text += " "; } rectangle closex = rectangle.empty; private void tabcontrol1_drawitem(object sender, drawitemeventargs e) { size xsize = new size(13,13); tabpage tp = tabcontrol3.tabpages[e.index]; e.drawbackground(); using (solidbrush brush = new solidbrush(e.forecolor) ) e.graphics.drawstring(tp.text+ " ", e.font, brush, e.bounds.x + 3, e.bounds.y + 4 ); if (e.state == drawitemstate.selected) { closex = new rectangle( e.bounds.right - xsize.width, e.bounds.top, xsize.width, xsize.height); using (solidbrush brush = new solidbrush(color.lightgray)) e.graphics.drawstring("x", e.font, brush, e.bounds.right - xsize.width, e.bounds.y + 4); } } private void tabcontrol1_mouseclick(object sender, mouseeventargs e) { if (closex.contains(e.location)) tabcontrol1.tabpages.remove(tabcontrol1.selectedtab); }
if want utilize image adorn tabs, include imagelist form, load image(s) , if close button 1st image, alter code this:
if (e.state == drawitemstate.selected) { closex = new rectangle(e.bounds.right - xsize.width - 3, e.bounds.top + 5, xsize.width, xsize.height); e.graphics.drawimage(imagelist1.images[0], closex, new rectangle(0,0,16,16), graphicsunit.pixel ); }
i append screenshot of tab few pages
and 1 using close button image:
update: note if tabpage
contains idisposable
objects should create sure disposed before remove page! see here illustration code!
c# tabs tabcontrol
No comments:
Post a Comment