1.在实际的开发过程中使用了DevExpress控件库,又使用的panel面板控件,自定义控件,把面板加载到指定的位置,比如上面,下面,左边,右边等位置。
加载面板最重要的是确保窗体是同一个对象,
1.定义一个单例类,在这个类中,所有的窗体都声明一个对象,然后在窗体初始化时,把这个对象赋值,确保了窗体只有唯一的对象
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DevExpress.XtraBars.Docking; using WindowsFormsApplication5; public class WorkUiService { private static WorkUiService _instance = null; private static object _lockObj = new object(); private WorkUiService() { } public static WorkUiService Instance { get { if (_instance == null) { lock (_lockObj) { if (_instance == null) _instance = new WorkUiService(); //单例模式,确保一个窗体在程序中只有一个对象 } } return _instance; } } public DockManager DockManager { get; set; } public FormMain formMain { get; set; } //主窗体 public UControl uc { get; set; } //用户控件 public DockPanel FillDockPanel { get; set; } <span style="font-family: Arial, Helvetica, sans-serif;"> //DockPanel加载的位置</span> public DockPanel RightDockPanel { get; set; } public DockPanel BottomDockPanel { get; set; } }2.给定对象赋值
如果需要加载到主窗体上面,在主窗体里赋值
WorkUiService.Instance.formMain = this;3.加载panel的位置,加载之前需要判断panel是否存在
假设加载在右边
if (WorkUiService.Instance.RightDockPanel == null || WorkUiService.Instance.RightDockPanel.IsDisposed) { WorkUiService.Instance.RightDockPanel = this.dockManager1.AddPanel(DockingStyle.Right); }4加载panel
if (WorkUiService.Instance.uc1 == null || WorkUiService.Instance.uc1.IsDisposed) { WorkUiService.Instance.RightDockPanel.Text = "XXX"; //加载的panel的名字 WorkUiService.Instance.RightDockPanel.Width = 300; //加载的panel的宽度 WorkUiService.Instance.uc1 = new UC1(); //加载用户控件 WorkUiService.Instance.RightDockPanel.Controls[0].Controls.Clear(); WorkUiService.Instance.RightDockPanel.Controls.Add(WorkUiService.Instance.uc1); //把用户控件加载到指定的位置,这里是右边 }
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (WorkUiService.Instance.RightDockPanel == null || WorkUiService.Instance.RightDockPanel.IsDisposed) { WorkUiService.Instance.RightDockPanel = this.dockManager1.AddPanel(DockingStyle.Right); //加载panel的位置 } if (WorkUiService.Instance.uc1 == null || WorkUiService.Instance.uc1.IsDisposed) { WorkUiService.Instance.RightDockPanel.Text = "XXX"; WorkUiService.Instance.RightDockPanel.Width = 300; WorkUiService.Instance.uc = new UControl(); WorkUiService.Instance.RightDockPanel.Controls[0].Controls.Clear(); WorkUiService.Instance.RightDockPanel.Controls.Add(WorkUiService.Instance.uc); } WorkUiService.Instance.RightDockPanel.Visibility = DockVisibility.Visible; }