* UIForm: 增加了查找页面的方法

This commit is contained in:
Sunny 2022-03-28 15:39:08 +08:00
parent 6055450e7e
commit 188151b58a
9 changed files with 100 additions and 38 deletions

Binary file not shown.

Binary file not shown.

View File

@ -88,6 +88,14 @@ namespace Sunny.UI.Demo
Text = Version + " Build " + Properties.Resources.BuildDate; Text = Version + " Build " + Properties.Resources.BuildDate;
RegisterHotKey(UI.ModifierKeys.Shift, Keys.F8); RegisterHotKey(UI.ModifierKeys.Shift, Keys.F8);
//根据页面类型获取页面
FButton page = GetPage<FButton>();
page?.Text.WriteConsole();
//根据页面索引获取页面
UIPage page1 = GetPage(1002);
page1?.Text.WriteConsole();
} }
/// <summary> /// <summary>

View File

@ -8,7 +8,8 @@
//新建一个窗体改继承Form为UIForm //新建一个窗体改继承Form为UIForm
//窗体上只要放一个UITabControl并关联到UIForm这样多页面框架就已经打好了其余的可以自由发挥 //窗体上只要放一个UITabControl并关联到UIForm这样多页面框架就已经打好了其余的可以自由发挥
MainTabControl = uiTabControl1; //窗体上如果只有一个UITabControl也会自动关联超过一个需要手动关联
//MainTabControl = uiTabControl1;
//有三个UIPage分别为 //有三个UIPage分别为
//FPage1其属性PageIndex为1001 //FPage1其属性PageIndex为1001
@ -17,7 +18,7 @@
//设置FTitlePage1为主页面不能被关闭 //设置FTitlePage1为主页面不能被关闭
var mainPage = new FPage1(); var mainPage = new FPage1();
MainTabControl.MainPage = mainPage.Text = "主页"; uiTabControl1.MainPage = mainPage.Text = "主页";
AddPage(mainPage); AddPage(mainPage);
} }

View File

@ -36,10 +36,11 @@ namespace Sunny.UI
/// </summary> /// </summary>
public static class ControlEx public static class ControlEx
{ {
public static void ReStart(this Timer timer) public static Timer ReStart(this Timer timer)
{ {
timer.Stop(); timer.Stop();
timer.Start(); timer.Start();
return timer;
} }
public static Rectangle ScreenRectangle(this Control ctrl) public static Rectangle ScreenRectangle(this Control ctrl)
@ -63,6 +64,27 @@ namespace Sunny.UI
return ctrl as Form; 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) public static Form GetParentForm(this Control ctrl)
{ {
while (!IsForm(ctrl.Parent)) while (!IsForm(ctrl.Parent))
@ -124,15 +146,17 @@ namespace Sunny.UI
return pt; 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); 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); menu.Show(ctrl, offsetX, offsetY);
return menu;
} }
//https://www.codeproject.com/Tips/1264882/Extension-Methods-for-Multiple-Control-Tags //https://www.codeproject.com/Tips/1264882/Extension-Methods-for-Multiple-Control-Tags

View File

@ -783,5 +783,10 @@ namespace Sunny.UI
column.SortMode = sortMode; column.SortMode = sortMode;
return column; return column;
} }
public static bool IsDBNull(this DataGridViewCell cell)
{
return cell.Value is DBNull;
}
} }
} }

View File

@ -22,6 +22,7 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
@ -379,6 +380,32 @@ namespace Sunny.UI
return null; return null;
} }
public T GetPage<T>() where T : UIPage
{
List<T> result = GetPages<T>();
return result.Count > 0 ? result[0] : null;
}
public List<T> GetPages<T>() where T : UIPage
{
List<T> result = new List<T>();
foreach (var item in PageItems)
{
if (item.Key != null)
{
var tabPage = item.Key;
var pages = tabPage.GetControls<UIPage>();
for (int i = 0; i < pages.Count; i++)
{
if (pages[i] is T pg)
result.Add(pg);
}
}
}
return result;
}
public UIPage GetPage(Guid guid) public UIPage GetPage(Guid guid)
{ {
if (guid == Guid.Empty) return null; if (guid == Guid.Empty) return null;

View File

@ -223,6 +223,10 @@ namespace Sunny.UI
public void AddPage(Guid guid, UITabControlMenu page) => Helper.AddPage(guid, page); public void AddPage(Guid guid, UITabControlMenu page) => Helper.AddPage(guid, page);
public T GetPage<T>() where T : UIPage => Helper.GetPage<T>();
public List<T> GetPages<T>() where T : UIPage => Helper.GetPages<T>();
public string Version public string Version
{ {
get; get;

View File

@ -30,6 +30,7 @@
* 2022-01-03: V3.0.9 * 2022-01-03: V3.0.9
* 2022-02-09: V3.1.0 SetParamToPage * 2022-02-09: V3.1.0 SetParamToPage
* 2022-03-19: V3.1.1 * 2022-03-19: V3.1.1
* 2022-03-28: V3.1.1
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -1978,57 +1979,44 @@ namespace Sunny.UI
public UIPage AddPage(UIPage page) public UIPage AddPage(UIPage page)
{ {
SetDefaultTabControl();
page.Frame = this; page.Frame = this;
MainTabControl?.AddPage(page); MainTabControl?.AddPage(page);
return page; return page;
} }
public virtual void SelectPage(int pageIndex) private UIForm SetDefaultTabControl()
{ {
MainTabControl?.SelectPage(pageIndex); if (MainTabControl == null)
{
List<UITabControl> ctrls = this.GetControls<UITabControl>();
if (ctrls.Count == 1) MainTabControl = ctrls[0];
} }
public virtual void SelectPage(Guid guid) return this;
{
MainTabControl?.SelectPage(guid);
} }
public bool RemovePage(int pageIndex) public virtual void SelectPage(int pageIndex) => SetDefaultTabControl().MainTabControl?.SelectPage(pageIndex);
{
return MainTabControl?.RemovePage(pageIndex) ?? false;
}
public bool RemovePage(Guid guid) public virtual void SelectPage(Guid guid) => SetDefaultTabControl().MainTabControl?.SelectPage(guid);
{
return MainTabControl?.RemovePage(guid) ?? false;
}
public virtual void FeedbackFormPage(int fromPageIndex, params object[] objects) public bool RemovePage(int pageIndex) => MainTabControl?.RemovePage(pageIndex) ?? false;
{
}
public UIPage GetPage(int pageIndex) public bool RemovePage(Guid guid) => MainTabControl?.RemovePage(guid) ?? false;
{
return MainTabControl?.GetPage(pageIndex);
}
public UIPage GetPage(Guid guid) public virtual void FeedbackFormPage(int fromPageIndex, params object[] objects) { }
{
return MainTabControl?.GetPage(guid);
}
public bool ExistPage(int pageIndex) public UIPage GetPage(int pageIndex) => SetDefaultTabControl().MainTabControl?.GetPage(pageIndex);
{
return GetPage(pageIndex) != null;
}
public bool ExistPage(Guid guid) public UIPage GetPage(Guid guid) => SetDefaultTabControl().MainTabControl?.GetPage(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) public bool SetParamToPage(int toPageIndex, int fromPageIndex, params object[] objects)
{ {
SetDefaultTabControl();
UIPage page = GetPage(toPageIndex); UIPage page = GetPage(toPageIndex);
if (page == null) return false; if (page == null) return false;
return page.SetParam(fromPageIndex, objects); return page.SetParam(fromPageIndex, objects);
@ -2036,11 +2024,16 @@ namespace Sunny.UI
public bool SetParamToPage(Guid toPageGuid, Guid fromPageGuid, params object[] objects) public bool SetParamToPage(Guid toPageGuid, Guid fromPageGuid, params object[] objects)
{ {
SetDefaultTabControl();
UIPage page = GetPage(toPageGuid); UIPage page = GetPage(toPageGuid);
if (page == null) return false; if (page == null) return false;
return page.SetParam(fromPageGuid, objects); return page.SetParam(fromPageGuid, objects);
} }
public T GetPage<T>() where T : UIPage => SetDefaultTabControl().MainTabControl?.GetPage<T>();
public List<T> GetPages<T>() where T : UIPage => SetDefaultTabControl().MainTabControl?.GetPages<T>();
#endregion IFrame实现 #endregion IFrame实现
} }
} }