問題描述
我正在制作一個需要添加或刪除選項卡(選項卡控件)的應用程序.我已經很好地完成了標簽的添加和刪除,但是我有自定義按鈕而不是使用標簽.(此按鈕點擊后會跳轉到第一個標簽頁):
I am making an application that requires tabs (tab-control) to be added or removed. I have done the add and remove for tabs fine, but I have custom buttons instead of using the tabs. (This button will go the the first tab page when clicked):
//This will make it go to TAB 1
private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectedIndex = 0;
}
//This will change the MOUSEENTER to the correct 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 will change the MOUSELEAVE to the correct 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));
}
但是,我將如何創(chuàng)建它,所以當您單擊添加選項卡"按鈕時,它會創(chuàng)建一個新選項卡,但它也會創(chuàng)建一個帶有 tabControl1.SelectedIndex = 1;
的新按鈕,例如.
However, how will I create it so when you click the "Add Tab" button it creates a new tab but it also creates a new button with tabControl1.SelectedIndex = 1;
in it for example.
編輯
我這樣做是為了添加一個新選項卡(到 tabControl1):
I have done this to add a 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);
}
這會在現(xiàn)有標簽之上添加一個新標簽.但是我該怎么做才能使它還使用上面按鈕的屬性創(chuàng)建一個新按鈕,但它不是 tabControl1.SelectedIndex = 1;
它是 tabControl1.SelectedIndex =3;
每次添加新標簽時都會上升?- 謝謝
This adds a new tab on top of the existing ones fine. But I how do I do it so that it also creates a new button with the properties of the button above but makes it so instead of tabControl1.SelectedIndex = 1;
it does tabControl1.SelectedIndex = 3;
and goes up every time I add a new tab? - Thanks
推薦答案
經過幾次更新,這里是我的答案的新版本.它告訴你如何
After a few updates here is a new version of my answer. It shows you how to
- 添加一個
Button
,它會添加一個新的TabPages
并選擇它 - 使
Tab
控件在每個選項卡的右側繪制結束x"字符,就像 Firefox 或 Visual Studio 那樣.
- Add a
Button
which adds a newTabPages
and selects it - Make the
Tab
control draw closing 'x' characters to the right of each tab, like Firefox or Visual Studio do it.
使添加 Button
像你想要的那樣,也許是 Text="+" 和標簽的高度一樣的高度.它會自動定位在選項卡上.如果您的 Tab
被移動或調整大小,您可能需要重新定位它.
Make the add Button
like you want it, maybe the Text="+" and the height like the tabs' height. It is automatically positioned on the tab. You may need to reposition it, if your Tab
is moved or resized.
在 Form_Load
事件中,Tab
設置為 OwnerDrawFixed
并且每個頁面的文本都附加了幾個空格,以便為關閉騰出空間X.代碼創(chuàng)建一個類變量 closeX
來保存當前的 x 矩形并在 MouseClick
事件中對其進行測試.
In the Form_Load
event the Tab
is set to OwnerDrawFixed
and each page's text is append by a few blanks to make room for the closing x. The code creates a class variable closeX
to hold the current x-rectangle and tests it in the MouseClick
event.
確保連接 tabControl1_DrawItem
和 tabControl1_MouseClick
事件!
Make sure to wire up the tabControl1_DrawItem
and 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);
}
如果您想使用圖像來裝飾選項卡,請在表單中包含一個 ImageList,使用圖像加載它,如果關閉按鈕是第一個圖像,請像這樣更改代碼:
If you want to use an Image to adorn the tabs, include an ImageList with your form, load it with the Image(s) and if the close button is the 1st image, change the code like 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 );
}
我附上幾頁的 Tab 截圖
I append a screenshot of the Tab with a few pages
還有一個使用這個 關閉按鈕圖像:
And one using this close button image:
更新:請注意,如果您的 TabPage
包含 IDisposable
對象,則應確保在刪除頁面之前將它們全部釋放!請參閱此處 例如代碼!
Update: Note that if your TabPage
contains IDisposable
objects you should make sure they all get disposed before you remove the page! See here for example code!
這篇關于如何添加和刪除“自定義"C# 中的選項卡的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!