diff --git a/Bin/net40/SunnyUI.Demo.exe b/Bin/net40/SunnyUI.Demo.exe index 51e2e86a..6918bc49 100644 Binary files a/Bin/net40/SunnyUI.Demo.exe and b/Bin/net40/SunnyUI.Demo.exe differ diff --git a/Bin/net40/SunnyUI.dll b/Bin/net40/SunnyUI.dll index d9ad8a42..2e885537 100644 Binary files a/Bin/net40/SunnyUI.dll and b/Bin/net40/SunnyUI.dll differ diff --git a/SunnyUI.Demo/FMain.cs b/SunnyUI.Demo/FMain.cs index e7e11e37..d21b79d2 100644 --- a/SunnyUI.Demo/FMain.cs +++ b/SunnyUI.Demo/FMain.cs @@ -88,6 +88,14 @@ namespace Sunny.UI.Demo Text = Version + " Build " + Properties.Resources.BuildDate; RegisterHotKey(UI.ModifierKeys.Shift, Keys.F8); + + //根据页面类型获取页面 + FButton page = GetPage(); + page?.Text.WriteConsole(); + + //根据页面索引获取页面 + UIPage page1 = GetPage(1002); + page1?.Text.WriteConsole(); } /// diff --git a/SunnyUI.Demo/Forms/Frames/FCustomMain.cs b/SunnyUI.Demo/Forms/Frames/FCustomMain.cs index 79b49542..1c851925 100644 --- a/SunnyUI.Demo/Forms/Frames/FCustomMain.cs +++ b/SunnyUI.Demo/Forms/Frames/FCustomMain.cs @@ -8,7 +8,8 @@ //新建一个窗体,改继承Form为UIForm //窗体上只要放一个UITabControl,并关联到UIForm,这样多页面框架就已经打好了,其余的可以自由发挥 - MainTabControl = uiTabControl1; + //窗体上如果只有一个UITabControl,也会自动关联,超过一个需要手动关联 + //MainTabControl = uiTabControl1; //有三个UIPage,分别为: //FPage1,其属性PageIndex为1001 @@ -17,7 +18,7 @@ //设置FTitlePage1为主页面,不能被关闭 var mainPage = new FPage1(); - MainTabControl.MainPage = mainPage.Text = "主页"; + uiTabControl1.MainPage = mainPage.Text = "主页"; AddPage(mainPage); } diff --git a/SunnyUI/Common/UControl.cs b/SunnyUI/Common/UControl.cs index faa4d39f..a3f61318 100644 --- a/SunnyUI/Common/UControl.cs +++ b/SunnyUI/Common/UControl.cs @@ -36,10 +36,11 @@ namespace Sunny.UI /// public static class ControlEx { - public static void ReStart(this Timer timer) + public static Timer ReStart(this Timer timer) { timer.Stop(); timer.Start(); + return timer; } public static Rectangle ScreenRectangle(this Control ctrl) @@ -63,6 +64,27 @@ namespace Sunny.UI return ctrl as Form; } + public static Control SettingToCenter(this Control ctrl) + { + return ctrl.SettingToXCenter().SettingToYCenter(); + } + + public static Control SettingToXCenter(this Control ctrl) + { + if (ctrl != null && ctrl.Parent != null) + ctrl.Left = (ctrl.Parent.Width - ctrl.Width) / 2; + + return ctrl; + } + + public static Control SettingToYCenter(this Control ctrl) + { + if (ctrl != null && ctrl.Parent != null) + ctrl.Top = (ctrl.Parent.Height - ctrl.Height) / 2; + + return ctrl; + } + public static Form GetParentForm(this Control ctrl) { while (!IsForm(ctrl.Parent)) @@ -124,15 +146,17 @@ namespace Sunny.UI return pt; } - public static void ShowContextMenuStrip(this Control ctrl, ContextMenuStrip menu, Point offset) + public static ContextMenuStrip ShowContextMenuStrip(this Control ctrl, ContextMenuStrip menu, Point offset) { //设置显示的位置为鼠标所在的位置 menu.Show(ctrl, offset); + return menu; } - public static void ShowContextMenuStrip(this Control ctrl, ContextMenuStrip menu, int offsetX, int offsetY) + public static ContextMenuStrip ShowContextMenuStrip(this Control ctrl, ContextMenuStrip menu, int offsetX, int offsetY) { menu.Show(ctrl, offsetX, offsetY); + return menu; } //https://www.codeproject.com/Tips/1264882/Extension-Methods-for-Multiple-Control-Tags diff --git a/SunnyUI/Controls/UIDataGridView.cs b/SunnyUI/Controls/UIDataGridView.cs index 44becb28..66d383ef 100644 --- a/SunnyUI/Controls/UIDataGridView.cs +++ b/SunnyUI/Controls/UIDataGridView.cs @@ -783,5 +783,10 @@ namespace Sunny.UI column.SortMode = sortMode; return column; } + + public static bool IsDBNull(this DataGridViewCell cell) + { + return cell.Value is DBNull; + } } } \ No newline at end of file diff --git a/SunnyUI/Controls/UINavMenuHelper.cs b/SunnyUI/Controls/UINavMenuHelper.cs index df9b3091..7b93d9d4 100644 --- a/SunnyUI/Controls/UINavMenuHelper.cs +++ b/SunnyUI/Controls/UINavMenuHelper.cs @@ -22,6 +22,7 @@ using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; @@ -379,6 +380,32 @@ namespace Sunny.UI return null; } + public T GetPage() where T : UIPage + { + List result = GetPages(); + return result.Count > 0 ? result[0] : null; + } + + public List GetPages() where T : UIPage + { + List result = new List(); + foreach (var item in PageItems) + { + if (item.Key != null) + { + var tabPage = item.Key; + var pages = tabPage.GetControls(); + for (int i = 0; i < pages.Count; i++) + { + if (pages[i] is T pg) + result.Add(pg); + } + } + } + + return result; + } + public UIPage GetPage(Guid guid) { if (guid == Guid.Empty) return null; diff --git a/SunnyUI/Controls/UITabControl.cs b/SunnyUI/Controls/UITabControl.cs index 9043c742..a641c941 100644 --- a/SunnyUI/Controls/UITabControl.cs +++ b/SunnyUI/Controls/UITabControl.cs @@ -223,6 +223,10 @@ namespace Sunny.UI public void AddPage(Guid guid, UITabControlMenu page) => Helper.AddPage(guid, page); + public T GetPage() where T : UIPage => Helper.GetPage(); + + public List GetPages() where T : UIPage => Helper.GetPages(); + public string Version { get; diff --git a/SunnyUI/Forms/UIForm.cs b/SunnyUI/Forms/UIForm.cs index 03328788..ef6297d6 100644 --- a/SunnyUI/Forms/UIForm.cs +++ b/SunnyUI/Forms/UIForm.cs @@ -30,6 +30,7 @@ * 2022-01-03: V3.0.9 标题栏按钮可以设置颜色 * 2022-02-09: V3.1.0 增加页面间传值方法SetParamToPage * 2022-03-19: V3.1.1 重构主题配色 + * 2022-03-28: V3.1.1 增加了查找页面的方法 ******************************************************************************/ using System; @@ -1978,57 +1979,44 @@ namespace Sunny.UI public UIPage AddPage(UIPage page) { + SetDefaultTabControl(); page.Frame = this; MainTabControl?.AddPage(page); return page; } - public virtual void SelectPage(int pageIndex) + private UIForm SetDefaultTabControl() { - MainTabControl?.SelectPage(pageIndex); + if (MainTabControl == null) + { + List ctrls = this.GetControls(); + if (ctrls.Count == 1) MainTabControl = ctrls[0]; + } + + return this; } - public virtual void SelectPage(Guid guid) - { - MainTabControl?.SelectPage(guid); - } + public virtual void SelectPage(int pageIndex) => SetDefaultTabControl().MainTabControl?.SelectPage(pageIndex); - public bool RemovePage(int pageIndex) - { - return MainTabControl?.RemovePage(pageIndex) ?? false; - } + public virtual void SelectPage(Guid guid) => SetDefaultTabControl().MainTabControl?.SelectPage(guid); - public bool RemovePage(Guid guid) - { - return MainTabControl?.RemovePage(guid) ?? false; - } + public bool RemovePage(int pageIndex) => MainTabControl?.RemovePage(pageIndex) ?? false; - public virtual void FeedbackFormPage(int fromPageIndex, params object[] objects) - { - } + public bool RemovePage(Guid guid) => MainTabControl?.RemovePage(guid) ?? false; - public UIPage GetPage(int pageIndex) - { - return MainTabControl?.GetPage(pageIndex); - } + public virtual void FeedbackFormPage(int fromPageIndex, params object[] objects) { } - public UIPage GetPage(Guid guid) - { - return MainTabControl?.GetPage(guid); - } + public UIPage GetPage(int pageIndex) => SetDefaultTabControl().MainTabControl?.GetPage(pageIndex); - public bool ExistPage(int pageIndex) - { - return GetPage(pageIndex) != null; - } + public UIPage GetPage(Guid guid) => SetDefaultTabControl().MainTabControl?.GetPage(guid); - public bool ExistPage(Guid guid) - { - return GetPage(guid) != null; - } + public bool ExistPage(int pageIndex) => GetPage(pageIndex) != null; + + public bool ExistPage(Guid guid) => GetPage(guid) != null; public bool SetParamToPage(int toPageIndex, int fromPageIndex, params object[] objects) { + SetDefaultTabControl(); UIPage page = GetPage(toPageIndex); if (page == null) return false; return page.SetParam(fromPageIndex, objects); @@ -2036,11 +2024,16 @@ namespace Sunny.UI public bool SetParamToPage(Guid toPageGuid, Guid fromPageGuid, params object[] objects) { + SetDefaultTabControl(); UIPage page = GetPage(toPageGuid); if (page == null) return false; return page.SetParam(fromPageGuid, objects); } + public T GetPage() where T : UIPage => SetDefaultTabControl().MainTabControl?.GetPage(); + + public List GetPages() where T : UIPage => SetDefaultTabControl().MainTabControl?.GetPages(); + #endregion IFrame实现 } } \ No newline at end of file