PDA

View Full Version : ایجاد دکمه Close روی tabpage



ehsankhan
پنج شنبه 15 بهمن 1394, 12:29 عصر
با سلام

من کد زیر رو با راهنمایی یکی از دوستان نوشتم و کاری که این کد می کنه این هست با کلیک بر روی یک دکمه صفحه جدید به صورت تب پیج باز میشه . سوال من اینجاست که چه طوری می تونم یک باتن کلوز روی تب پیج بوجود آمده ایجاد کنم تا با کلیلک بر روی آن تب پیج بسته بشه . ممنون از راهنمایی دوستان

private void groupPanel1_Click(object sender, EventArgs e)
{


}
TabPage ttnew;
private void buttonItem2_Click(object sender, EventArgs e)
{
if (ttnew == null)
{


ttnew = new TabPage();


ttnew.Text = "احسان" + (tabControl1.Controls.Count + 1).ToString();


Form3 frm=new Form3();


frm.TopLevel = false;


frm.Visible = true;


frm.FormBorderStyle = FormBorderStyle.None;


frm.Dock = DockStyle.Fill;


frm.Size = ttnew.Size;

ttnew.Controls.Add(frm);


tabControl1.Controls.Add(ttnew);


tabControl1.SelectedTab = ttnew;


}






else
{


tabControl1.SelectedTab = ttnew;


}
}

erfan_urchin
پنج شنبه 15 بهمن 1394, 19:50 عصر
سلام
اول از همه برید توی پراپرتی TabControl و مقدار پراپرتی DrowMode رو برابر OwnerDrawFixed قرار بدین
بعد کدهای زیر رو بنویسید

int LEADING_SPACE = 12;
int CLOSE_SPACE = 15;
int CLOSE_AREA = 15;
private void Form1_Load(object sender, EventArgs e)
{


int tabLength = tabControl1.ItemSize.Width;


for (int i = 0; i < this.tabControl1.TabPages.Count; i++)
{
TabPage currentPage = tabControl1.TabPages[i];


int currentTabLength = TextRenderer.MeasureText(currentPage.Text, tabControl1.Font).Width;


currentTabLength += LEADING_SPACE + CLOSE_SPACE + CLOSE_AREA;


if (currentTabLength > tabLength)
{
tabLength = currentTabLength;
}
}


Size newTabSize = new Size(tabLength, tabControl1.ItemSize.Height);
tabControl1.ItemSize = newTabSize;
}

private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
e.Graphics.DrawString("x", e.Font, Brushes.Black, e.Bounds.Right - CLOSE_AREA, e.Bounds.Top + 4);
e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black, e.Bounds.Left + LEADING_SPACE, e.Bounds.Top + 4);
e.DrawFocusRectangle();
}


private void tabControl1_MouseDown(object sender, MouseEventArgs e)
{
for (int i = 0; i < this.tabControl1.TabPages.Count; i++)
{
Rectangle r = tabControl1.GetTabRect(i);
Rectangle closeButton = new Rectangle(r.Right - 15, r.Top + 4, 9, 7);
if (closeButton.Contains(e.Location))
{
if (MessageBox.Show("Would you like to Close this Tab?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.tabControl1.TabPages.RemoveAt(i);
break;
}
}
}
}

ehsankhan
پنج شنبه 15 بهمن 1394, 21:31 عصر
سلام
اول از همه برید توی پراپرتی TabControl و مقدار پراپرتی DrowMode رو برابر OwnerDrawFixed قرار بدین
بعد کدهای زیر رو بنویسید

int LEADING_SPACE = 12;
int CLOSE_SPACE = 15;
int CLOSE_AREA = 15;
private void Form1_Load(object sender, EventArgs e)
{


int tabLength = tabControl1.ItemSize.Width;


for (int i = 0; i < this.tabControl1.TabPages.Count; i++)
{
TabPage currentPage = tabControl1.TabPages[i];


int currentTabLength = TextRenderer.MeasureText(currentPage.Text, tabControl1.Font).Width;


currentTabLength += LEADING_SPACE + CLOSE_SPACE + CLOSE_AREA;


if (currentTabLength > tabLength)
{
tabLength = currentTabLength;
}
}


Size newTabSize = new Size(tabLength, tabControl1.ItemSize.Height);
tabControl1.ItemSize = newTabSize;
}

private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
e.Graphics.DrawString("x", e.Font, Brushes.Black, e.Bounds.Right - CLOSE_AREA, e.Bounds.Top + 4);
e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black, e.Bounds.Left + LEADING_SPACE, e.Bounds.Top + 4);
e.DrawFocusRectangle();
}


private void tabControl1_MouseDown(object sender, MouseEventArgs e)
{
for (int i = 0; i < this.tabControl1.TabPages.Count; i++)
{
Rectangle r = tabControl1.GetTabRect(i);
Rectangle closeButton = new Rectangle(r.Right - 15, r.Top + 4, 9, 7);
if (closeButton.Contains(e.Location))
{
if (MessageBox.Show("Would you like to Close this Tab?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.tabControl1.TabPages.RemoveAt(i);
break;
}
}
}
}



کاری رو که گفتید انجام دادم و کد رو هم نوشتم کار خاصی انجام نمیده