สอบถาม : การสร้างฟอร์มย่อย(mdi) บน tab control จะต้องทำยังไง
ว่าแต่วัตถุประสงค์ของท่าน คือต้องการสร้างเองโดยไม่พึ่ง Third party หรือใช้ Third party
หากต้องสร้างเองข้อนี้ผมไม่รู้ว่าสร้างยังไง แต่ที่แน่ ๆ ตัวอย่าง GUI Form ที่คุณแนบมา ใช้
Third party ร่วมอย่างแน่นอน
ลิ้งค์ Third party มีให้เลือกมากมายแล้วแต่คุณจะเลือกเจ้าไหน
https://www.infoq.com/research/dotnet-web-components
ตัวอย่างที่ผมใช้ของ Devexpress ก็มี Control xtraTabbedMdiManager ที่สามารถปรับฟอร์มให้อยู่ในรูปแบบนี้ได้ ตามรูป
ข้างล่างนี้เป็นโค้ดที่ผมเรียกใช้เปิดฟอร์ม
Class form
Code (VB.NET)
Public NotInheritable Class ClsForm
'เปิดฟอร์มที่ระบุ
Public Shared Sub Show_Form(Form_Name As Form)
If Check_Form_Open(Form_Name.Name.ToString()) = True Then ' หากมีฟอร์มที่เปิดอยู่แล้วให้ Active
Form_Name.MdiParent = FMain.ActiveForm
Form_Name.Activate()
Form_Name.WindowState = FormWindowState.Maximized
Form_Name.StartPosition = FormStartPosition.CenterParent
Else 'กรณีที่ยังไม่เปิดฟอร์มให้ Show
Form_Name.MdiParent = FMain.ActiveForm
Form_Name.Show()
End If
End Sub
'ตรวจสอบฟอร์มถูกเปิดใช้งานอยู่หรือไม่ True = พบฟอร์มที่เปิดอยู่ / False ไม่พบฟอร์มที่เปิดอยู่
Private Shared Function Check_Form_Open(Form_Name As String) As Boolean
Dim fc As FormCollection = Application.OpenForms
For Each frm As Form In fc
If frm.Name = Form_Name Then
Return True
Exit Function
End If
Next
Return False
End Function
End Class
โค้ดเรียกใช้ Show / Active form ดังนี้
Code (VB.NET)
'ฟอร์ม1
Private Sub stbtMenu1_Click(sender As Object, e As EventArgs) Handles stbtMenu1.Click
ClsForm.Show_Form(Form1)
End Sub
Date :
2018-08-08 12:53:50
By :
K
ตัวนี้ใช้ใน tabcontrol นะครับ
Code (C#)
public void AddNewForm(System.Windows.Forms.Form form,System.Windows.Forms.TabControl tab)
{
for (int i = 0; i < tab.TabCount;i++ )
{
if (tab.TabPages[i].Text == form.Text)
{
tab.TabIndex = i;
return;
}
}
System.Windows.Forms.TabPage tab = new System.Windows.Forms.TabPage();
tab.Controls.Add(tab);
form.TopLevel = false;
form.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right | System.Windows.Forms.AnchorStyles.Left);
form.Dock = System.Windows.Forms.DockStyle.Fill;
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form.Parent = tab;
tab.Text = form.Text;
try { form.Show(); }
catch { }
tab.SelectedTab = tab;
}
ถ้าใช้ทั้ง tab จะประมาณนี้ครับ
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TORServices.FormsTor
{
#region _SidTabControl
public class SideTabControl : System.Windows.Forms.TabControl
{
public void AddNewForm(System.Windows.Forms.Form form)
{
for (int i = 0; i < this.TabCount;i++ )
{
if (this.TabPages[i].Text == form.Text)
{
this.TabIndex = i;
return;
}
}
System.Windows.Forms.TabPage tab = new System.Windows.Forms.TabPage();
this.Controls.Add(tab);
form.TopLevel = false;
form.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right | System.Windows.Forms.AnchorStyles.Left);
form.Dock = System.Windows.Forms.DockStyle.Fill;
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form.Parent = tab;
tab.Text = form.Text;
try { form.Show(); }
catch { }
this.SelectedTab = tab;
}
public SideTabControl()
{
SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer, true);
TabStop = false;
DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
_closeButtonBrush = new System.Drawing.SolidBrush(_closeButtonColor);
ItemSize = new System.Drawing.Size(ItemSize.Width, 24);
Padding = new System.Drawing.Point(16, 0);
Dock = System.Windows.Forms.DockStyle.Top;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_stringFormat.Dispose();
_closeButtonBrush.Dispose();
}
base.Dispose(disposing);
}
public delegate void TabClosedDelegate(object sender, ClosedEventArgs e);
public delegate void TabClosingDelegate(object sender, ClosingEventArgs e);
public event TabClosedDelegate TabClosed;
public event TabClosingDelegate TabClosing;
private int _buttonWidth = 16;
[System.ComponentModel.DefaultValue(16), System.ComponentModel.Category("Action Buttons")]
public int ButtonWidth
{ get { return _buttonWidth; } set { _buttonWidth = value; }}
private int _crossOffset = 3;
[System.ComponentModel.DefaultValue(3), System.ComponentModel.Category("Action Buttons")]
public int CrossOffset
{get { return _crossOffset; } set { _crossOffset = value; }}
private readonly System.Drawing.StringFormat _stringFormat = new System.Drawing.StringFormat
{
Alignment = System.Drawing.StringAlignment.Near,
LineAlignment = System.Drawing.StringAlignment.Center
};
private System.Drawing.Color _closeButtonColor = System.Drawing.Color.Red;
private System.Drawing.Brush _closeButtonBrush;
[System.ComponentModel.Category("Action Buttons")]
public System.Drawing.Color CloseButtonColor
{
get { return _closeButtonColor; }
set
{
_closeButtonBrush.Dispose();
_closeButtonColor = value;
_closeButtonBrush = new System.Drawing.SolidBrush(_closeButtonColor);
Invalidate();
}
}
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
{
if (e.Bounds != System.Drawing.RectangleF.Empty)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
for (int nIndex = 0; nIndex < TabCount; nIndex++)
{
System.Drawing.Rectangle tabArea = GetTabRect(nIndex);
System.Drawing.Rectangle closeBtnRect = GetCloseBtnRect(tabArea);
if (nIndex != SelectedIndex)
{
e.Graphics.DrawRectangle(System.Drawing.Pens.DarkGray, closeBtnRect);
DrawCross(e, closeBtnRect, System.Drawing.Color.DarkGray);
}
else
{
//Drawing Close Button
e.Graphics.FillRectangle(_closeButtonBrush, closeBtnRect);
e.Graphics.DrawRectangle(System.Drawing.Pens.White, closeBtnRect);
DrawCross(e, closeBtnRect, System.Drawing.Color.White);
}
string str = TabPages[nIndex].Text;
e.Graphics.DrawString(str, Font, new System.Drawing.SolidBrush(TabPages[nIndex].ForeColor), tabArea, _stringFormat);
}
}
}
private void DrawCross(System.Windows.Forms.DrawItemEventArgs e, System.Drawing.Rectangle btnRect, System.Drawing.Color color)
{
using (System.Drawing.Pen pen = new System.Drawing.Pen(color, 2))
{
float x1 = btnRect.X + CrossOffset;
float x2 = btnRect.Right - CrossOffset;
float y1 = btnRect.Y + CrossOffset;
float y2 = btnRect.Bottom - CrossOffset;
e.Graphics.DrawLine(pen, x1, y1, x2, y2);
e.Graphics.DrawLine(pen, x1, y2, x2, y1);
}
}
private System.Drawing.Rectangle GetCloseBtnRect(System.Drawing.Rectangle tabRect)
{
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(tabRect.X + tabRect.Width - ButtonWidth - 4, (tabRect.Height - ButtonWidth) / 2, ButtonWidth, ButtonWidth);
return rect;
}
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if (!DesignMode)
{
System.Drawing.Rectangle rect = GetTabRect(SelectedIndex);
rect = GetCloseBtnRect(rect);
System.Drawing.Point pt = new System.Drawing.Point(e.X, e.Y);
if (rect.Contains(pt))
{
CloseTab(SelectedTab);
}
}
}
public void CloseTab(int tabindex) {CloseTab(TabPages[tabindex]); }
public void CloseTab(System.Windows.Forms.TabPage tp)
{
ClosingEventArgs args = new ClosingEventArgs(TabPages.IndexOf(tp));
OnTabClosing(args);
//Remove the tab and fir the event tot he client
if (!args.Cancel)
{
// close and remove the tab, dispose it too
TabPages.Remove(tp);
OnTabClosed(new ClosedEventArgs(tp));
tp.Dispose();
}
}
protected void OnTabClosed(ClosedEventArgs e)
{if (TabClosed != null) {TabClosed(this, e);}
}
protected void OnTabClosing(ClosingEventArgs e)
{ if (TabClosing != null) TabClosing(this, e);}
}
//Some support classes:
public class ClosingEventArgs
{
private readonly int _nTabIndex = -1;
public ClosingEventArgs(int nTabIndex)
{
_nTabIndex = nTabIndex;
Cancel = false;
}
public bool Cancel { get; set; }
/// <summary>
/// Get/Set the tab index value where the close button is clicked
/// </summary>
public int TabIndex
{
get
{
return _nTabIndex;
}
}
}
public class ClosedEventArgs : EventArgs
{
private readonly System.Windows.Forms.TabPage _tab;
public ClosedEventArgs(System.Windows.Forms.TabPage tab) { _tab = tab;}
/// <summary>
/// Get/Set the tab index value where the close button is clicked
/// </summary>
public System.Windows.Forms.TabPage Tab { get{return _tab; }}
}
#endregion
}
Date :
2018-08-08 15:22:56
By :
lamaka.tor
Load balance : Server 01