|
|
|
[C#] อยากได้ คำสั่งให้ Form ลูฏไป เปิด form ลูก อีกตัวหนึ่งครับ (MDI) |
|
|
|
|
|
|
|
ผมจำคำสั่งไม่ค่อยจะได้ แต่หลักการก็คือกลับไปเรียกที่ Mediator เป็นหลักการ design pattern อย่างหนึ่ง
ให้ไว้เผื่อศึกษาครับ
http://en.wikipedia.org/wiki/Mediator_pattern
ประมาณว่า Form ลูก ก็จับเอา Parent ออกมา นั่นคือ Form ที่เป็น MDI นั่นเอง
Code (C#)
// ที่ Form Login เรียกกลับขึ้นไปที่ MdiForm
// ทำการ casting กรณีมี property หรือ method ที่แตกต่างออกไป ควรจะเป็นชื่อ Form คุณ
MainForm f = (MainForm)this.MdiForm;
f.OpenAnotherForm();
|
|
|
|
|
Date :
2014-10-13 21:18:47 |
By :
gunnermontana |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอนำเสนอครับผมใช้อยู่เห็นว่า OK ดี แล้วก็ง่ายด้วยครับ
Code (C#)
namespace TORServices
{
#region _SidTabControl
public class cntlSidTabControl : System.Windows.Forms.TabControl
{
public void AddNewForm(Form form)
{
for (int i = 0; i < this.TabCount;i++ )
{
if (this.TabPages[i].Text == form.Text)
{
this.TabIndex = i;
return;
}
}
TabPage tab = new TabPage();
this.Controls.Add(tab);
form.TopLevel = false;
form.Anchor = (AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left);
form.Dock = DockStyle.Fill;
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form.Parent = tab;
tab.Text = form.Text;
try { form.Show(); }
catch { }
this.SelectedTab = tab;
}
public cntlSidTabControl()
{
SetStyle(ControlStyles.DoubleBuffer, true);
TabStop = false;
DrawMode = TabDrawMode.OwnerDrawFixed;
_closeButtonBrush = new SolidBrush(_closeButtonColor);
ItemSize = new Size(ItemSize.Width, 24);
// used to expand the tab header, find a better way
Padding = new Point(16, 0);
}
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;
[DefaultValue(16), Category("Action Buttons")]
public int ButtonWidth
{
get { return _buttonWidth; }
set { _buttonWidth = value; }
}
private int _crossOffset = 3;
[DefaultValue(3), Category("Action Buttons")]
public int CrossOffset
{
get { return _crossOffset; }
set { _crossOffset = value; }
}
private readonly StringFormat _stringFormat = new StringFormat
{
Alignment = StringAlignment.Near,
LineAlignment = StringAlignment.Center
};
private Color _closeButtonColor = Color.Red;
private Brush _closeButtonBrush;
[Category("Action Buttons")]
public Color CloseButtonColor
{
get { return _closeButtonColor; }
set
{
_closeButtonBrush.Dispose();
_closeButtonColor = value;
_closeButtonBrush = new SolidBrush(_closeButtonColor);
Invalidate();
}
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
if (e.Bounds != RectangleF.Empty)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
for (int nIndex = 0; nIndex < TabCount; nIndex++)
{
Rectangle tabArea = GetTabRect(nIndex);
Rectangle closeBtnRect = GetCloseBtnRect(tabArea);
if (nIndex != SelectedIndex)
{
e.Graphics.DrawRectangle(Pens.DarkGray, closeBtnRect);
DrawCross(e, closeBtnRect, Color.DarkGray);
}
else
{
//Drawing Close Button
e.Graphics.FillRectangle(_closeButtonBrush, closeBtnRect);
e.Graphics.DrawRectangle(Pens.White, closeBtnRect);
DrawCross(e, closeBtnRect, Color.White);
}
string str = TabPages[nIndex].Text;
e.Graphics.DrawString(str, Font, new SolidBrush(TabPages[nIndex].ForeColor), tabArea, _stringFormat);
}
}
}
private void DrawCross(DrawItemEventArgs e, Rectangle btnRect, Color color)
{
using (Pen pen = new 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 Rectangle GetCloseBtnRect(Rectangle tabRect)
{
Rectangle rect = new Rectangle(tabRect.X + tabRect.Width - ButtonWidth - 4, (tabRect.Height - ButtonWidth) / 2, ButtonWidth, ButtonWidth);
return rect;
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (!DesignMode)
{
Rectangle rect = GetTabRect(SelectedIndex);
rect = GetCloseBtnRect(rect);
Point pt = new Point(e.X, e.Y);
if (rect.Contains(pt))
{
CloseTab(SelectedTab);
}
}
}
public void CloseTab(int tabindex)
{
CloseTab(TabPages[tabindex]);
}
public void CloseTab(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 TabPage _tab;
public ClosedEventArgs(TabPage tab)
{
_tab = tab;
}
/// <summary>
/// Get/Set the tab index value where the close button is clicked
/// </summary>
public TabPage Tab
{
get
{
return _tab;
}
}
}
#endregion
}
|
|
|
|
|
Date :
2014-10-14 11:24:37 |
By :
lamaka.tor |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|