สอบถามครับ การเรียกฟอร์มลูก ที่เราเปิดไว้แล้วให้แสดงออกมา
Code (VB.NET)
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
Dim CurrentForm1 As Form
For Each CurrentForm1 In Me.MdiChildren
If TypeOf CurrentForm1 Is SaleForm Then
CurrentForm1.MdiParent = Me
CurrentForm1.Show()
MessageBox.Show("ฟอร์มนี้เปิดใช้งานอยู่แล้ว !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Question)
Exit Sub
End If
Next
Dim SaleForm1 As New SaleForm
SaleForm.MdiParent = Me
SaleForm.Show()
End Sub
Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
Dim CurrentForm2 As Form
For Each CurrentForm2 In Me.MdiChildren
If TypeOf CurrentForm2 Is WithdrawForm Then
CurrentForm2.MdiParent = Me
CurrentForm2.Show()
MessageBox.Show("ฟอร์มนี้เปิดใช้งานอยู่แล้ว !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Question)
Exit Sub
End If
Next
Dim WithdrawForm1 As New WithdrawForm
WithdrawForm.MdiParent = Me
WithdrawForm.Show()
End Sub
ถ้าเรียกฟอร์มลูกมาหลายๆๆฟอร์มแล้ว ถ้าอยากเรียกฟอร์มที่เราหาไม่เจอขึ้นมาโดยการกดไปที่เมนูเดิมอีกครั้ง
จะมีmsgboxแสดงออกมาตามภาพและเมื่อกดOKไปฟอร์มนั้นจะโชว์ขึ้นมาเลย เพราะบางทีเปิดฟอร์มเยอะไปทำให้หาไม่เจอTag : .NET, Ms Access, VBScript, VS 2010 (.NET 4.x), Windows
Date :
2015-02-25 13:34:01
By :
viperz4
View :
947
Reply :
1
check ก่อนว่ามียุรึป่าวครับ
Code (VB.NET)
For Each Form As Form In Me.MdiChildren
If Form.Text = "Form2" Then
Exit Sub
End If
Next
Dim f As New Form2()
f.MdiParent = Me
f.Show()
ลองทำเป็น tab control ดีไม๊ครับ
Code (C#)
#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
เวลาใช้งานก็ประมาณ
Code (C#)
private cntlSidTabControl cntlSidTabControl1 = new TORServices.cntlSidTabControl();
this.Controls.Add(this.cntlSidTabControl1);
cntlSidTabControl1.AddNewForm(new dbRecordData.frmALS_F_603());
Date :
2015-02-25 14:59:32
By :
lamaka.tor
Load balance : Server 04