diff --git a/SunnyUI/Controls/DropItem/UIDateTimeItem.cs b/SunnyUI/Controls/DropItem/UIDateTimeItem.cs index 0fea4f65..258565c1 100644 --- a/SunnyUI/Controls/DropItem/UIDateTimeItem.cs +++ b/SunnyUI/Controls/DropItem/UIDateTimeItem.cs @@ -834,7 +834,6 @@ namespace Sunny.UI set { date = value; - TabControl.SelectPage(2); Year = date.Year; Month = date.Month; SetYearMonth(Year, Month); diff --git a/SunnyUI/Controls/UINavMenuHelper.cs b/SunnyUI/Controls/UINavMenuHelper.cs index be792270..9074f168 100644 --- a/SunnyUI/Controls/UINavMenuHelper.cs +++ b/SunnyUI/Controls/UINavMenuHelper.cs @@ -25,7 +25,6 @@ using System; using System.Collections.Concurrent; -using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; @@ -206,348 +205,6 @@ namespace Sunny.UI } } - public class UITabControlHelper - { - private readonly ConcurrentDictionary PageItems = new ConcurrentDictionary(); - - private readonly UITabControl tabControl; - - public UITabControlHelper(UITabControl ctrl) - { - tabControl = ctrl; - } - - public NavMenuItem this[int index] - { - get - { - if (index < 0 || index >= tabControl.TabPages.Count) - { - throw new ArgumentOutOfRangeException(); - } - - TabPage page = tabControl.TabPages[index]; - return PageItems.ContainsKey(page) ? PageItems[page] : null; - } - } - - public void SetTipsText(int pageIndex, string tipsText) - { - TabPage tabPage = CreateTabIfNotExists(pageIndex); - tabControl.SetTipsText(tabPage, tipsText); - } - - public void SetTipsText(Guid pageGuid, string tipsText) - { - TabPage tabPage = CreateTabIfNotExists(pageGuid); - tabControl.SetTipsText(tabPage, tipsText); - } - - public UIPage AddPage(int pageIndex, UIPage page) - { - page.PageIndex = pageIndex; - return AddPage(page); - } - - public UIPage AddPage(Guid pageGuid, UIPage page) - { - page.PageGuid = pageGuid; - return AddPage(page); - } - - public UIPage AddPage(UIPage page) - { - if (page.PageGuid == Guid.Empty && page.PageIndex < 0) - { - page.PageGuid = Guid.NewGuid(); - } - - TabPage tabPage = CreateTabIfNotExists(new NavMenuItem(page)); - page.Dock = DockStyle.Fill; - page.TabPage = tabPage; - tabPage.Controls.Add(page); - tabPage.Text = page.Text; - page.Show(); - - return page; - } - - public void AddPages(params UIPage[] pages) - { - foreach (var page in pages) - { - AddPage(page); - } - } - - public void AddPage(int index, UITabControlMenu page) - { - TabPage tabPage = CreateTabIfNotExists(index); - tabPage.Controls.Add(page); - page.Dock = DockStyle.Fill; - page.Show(); - } - - public void AddPage(int index, UITabControl page) - { - TabPage tabPage = CreateTabIfNotExists(index); - tabPage.Controls.Add(page); - page.Dock = DockStyle.Fill; - page.Show(); - } - - public void AddPage(Guid guid, UITabControlMenu page) - { - TabPage tabPage = CreateTabIfNotExists(guid); - tabPage.Controls.Add(page); - page.Dock = DockStyle.Fill; - page.Show(); - } - - public void AddPage(Guid guid, UITabControl page) - { - TabPage tabPage = CreateTabIfNotExists(guid); - tabPage.Controls.Add(page); - page.Dock = DockStyle.Fill; - page.Show(); - } - - private TabPage CreateTabIfNotExists(NavMenuItem item) - { - if (item == null) return null; - if (item.PageIndex < 0 && item.PageGuid == Guid.Empty) return null; - - for (int i = 0; i < tabControl.TabPages.Count; i++) - { - TabPage page = tabControl.TabPages[i]; - - if (!PageItems.ContainsKey(page)) - { - if (page.Controls.Count == 0) - { - PageItems.TryAdd(page, item); - return page; - } - } - else - { - if (item.PageGuid != Guid.Empty) - { - if (PageItems[page].PageGuid == item.PageGuid) return page; - } - else - { - if (item.PageIndex >= 0 && PageItems[page].PageIndex == item.PageIndex) return page; - } - } - } - - TabPage newPage = new TabPage(); - newPage.SuspendLayout(); - newPage.Text = "tabPage" + tabControl.TabPages.Count; - tabControl.Controls.Add(newPage); - PageItems.TryAdd(newPage, item); - newPage.ResumeLayout(); - return newPage; - } - - private TabPage CreateTabIfNotExists(int pageIndex) - { - return CreateTabIfNotExists(new NavMenuItem("", pageIndex)); - } - - private TabPage CreateTabIfNotExists(Guid guid) - { - return CreateTabIfNotExists(new NavMenuItem("", guid)); - } - - public event TabPageAndUIPageEventHandler TabPageAndUIPageChanged; - - public bool SelectPage(int pageIndex) - { - if (pageIndex < 0) - { - return false; - } - - List pages = tabControl.SelectedTab.GetControls(); - if (pages.Count == 1) - { - bool isCancel = pages[0].OnPageDeselecting(); - if (isCancel) return false; - pages[0].Translate(); - } - - foreach (var item in PageItems) - { - if (item.Value.PageIndex == pageIndex && item.Key != null) - { - if (tabControl.TabPages.Contains(item.Key)) - { - tabControl.SelectTab(item.Key); - TabPageAndUIPageChanged?.Invoke(this, new TabPageAndUIPageArgs(item.Key, item.Value.PageIndex, item.Value.PageGuid)); - return true; - } - } - } - - return false; - } - - public bool SelectPage(Guid guid) - { - if (guid == Guid.Empty) - { - return false; - } - - List pages = tabControl.SelectedTab.GetControls(); - if (pages.Count == 1) - { - bool isCancel = pages[0].OnPageDeselecting(); - if (isCancel) return false; - pages[0].Translate(); - } - - foreach (var item in PageItems) - { - if (item.Value.PageGuid == guid && item.Key != null) - { - if (tabControl.TabPages.Contains(item.Key)) - { - tabControl.SelectTab(item.Key); - TabPageAndUIPageChanged?.Invoke(this, new TabPageAndUIPageArgs(item.Key, item.Value.PageIndex, item.Value.PageGuid)); - return true; - } - } - } - - return false; - } - - public UIPage GetPage(int pageIndex) - { - var pages = GetPages(); - for (int i = 0; i < pages.Count; i++) - { - if (pages[i].PageIndex == pageIndex) - return pages[i]; - } - - 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; - var pages = GetPages(); - for (int i = 0; i < pages.Count; i++) - { - if (pages[i].PageGuid == guid) - return pages[i]; - } - - return null; - } - - public void RemoveAllPages(bool keepMainPage = true) - { - var pages = GetPages(); - foreach (var page in pages) - { - if (keepMainPage && page.TabPage?.Text == tabControl.MainPage) continue; - RemovePage(page.PageIndex); - } - } - - public bool RemovePage(int pageIndex) - { - foreach (var item in PageItems) - { - if (item.Value.PageIndex == pageIndex) - { - UIPage page = GetPage(pageIndex); - if (page != null) - { - TabPage tabpage = page.TabPage; - page.Final(); - page.Close(); - page.Dispose(); - page = null; - - if (tabpage != null) - { - tabpage.Parent = null; - tabpage.Dispose(); - tabpage = null; - } - } - - PageItems.TryRemove(item.Key, out _); - return true; - } - } - - return false; - } - - public bool RemovePage(Guid guid) - { - if (guid == Guid.Empty) return false; - foreach (var item in PageItems) - { - if (item.Value.PageGuid == guid) - { - UIPage page = GetPage(guid); - if (page != null) - { - TabPage tabpage = page.TabPage; - page.Dispose(); - page = null; - - if (tabpage != null) - { - tabpage.Parent = null; - tabpage.Dispose(); - tabpage = null; - } - } - - PageItems.TryRemove(item.Key, out _); - return true; - } - } - - return false; - } - } - public class NavMenuItem : ISymbol { public string Text { get; set; } @@ -623,21 +280,4 @@ namespace Sunny.UI Text = text; } } - - public class TabPageAndUIPageArgs : EventArgs - { - public TabPage TabPage { get; set; } - - public int PageIndex { get; set; } - public Guid PageGuid { get; set; } - - public TabPageAndUIPageArgs(TabPage tabPage, int pageIndex, Guid pageGuid) - { - TabPage = tabPage; - PageIndex = pageIndex; - PageGuid = pageGuid; - } - } - - public delegate void TabPageAndUIPageEventHandler(object sender, TabPageAndUIPageArgs e); } \ No newline at end of file diff --git a/SunnyUI/Controls/UITabControl.cs b/SunnyUI/Controls/UITabControl.cs index da6851c2..58a7bd76 100644 --- a/SunnyUI/Controls/UITabControl.cs +++ b/SunnyUI/Controls/UITabControl.cs @@ -73,9 +73,6 @@ namespace Sunny.UI timer.Interval = 500; timer.Tick += Timer_Tick; - DisposeTabPageAfterRemove = true; - AutoClosePage = true; - tabSelectedForeColor = UIStyles.Blue.TabControlTabSelectedColor; tabSelectedHighColor = UIStyles.Blue.TabControlTabSelectedColor; _fillColor = UIStyles.Blue.TabControlBackColor; @@ -86,13 +83,10 @@ namespace Sunny.UI TabPageAndUIPageChanged?.Invoke(this, e); } - public event TabPageAndUIPageEventHandler TabPageAndUIPageChanged; + public event EventHandler TabPageAndUIPageChanged; [Browsable(false), DefaultValue(null)] - public IFrame Frame - { - get; set; - } + public IFrame Frame { get; set; } /// /// 禁止控件跟随窗体缩放 @@ -251,17 +245,17 @@ namespace Sunny.UI public bool RemovePage(int pageIndex) => Helper.RemovePage(pageIndex); - public bool RemovePage(Guid guid) => Helper.RemovePage(guid); + public bool RemovePage(Guid pageGuid) => Helper.RemovePage(pageGuid); public void RemoveAllPages(bool keepMainPage = true) => Helper.RemoveAllPages(keepMainPage); public UIPage GetPage(int pageIndex) => Helper.GetPage(pageIndex); - public UIPage GetPage(Guid guid) => Helper.GetPage(guid); + public UIPage GetPage(Guid pageGuid) => Helper.GetPage(pageGuid); public void SetTipsText(int pageIndex, string tipsText) => Helper.SetTipsText(pageIndex, tipsText); - public void SetTipsText(Guid guid, string tipsText) => Helper.SetTipsText(guid, tipsText); + public void SetTipsText(Guid pageGuid, string tipsText) => Helper.SetTipsText(pageGuid, tipsText); public void AddPages(params UIPage[] pages) { @@ -277,14 +271,6 @@ namespace Sunny.UI internal event OnUIPageChanged PageAdded; internal event OnUIPageChanged PageRemoved; - public void AddPage(int pageIndex, UITabControl page) => Helper.AddPage(pageIndex, page); - - public void AddPage(int pageIndex, UITabControlMenu page) => Helper.AddPage(pageIndex, page); - - public void AddPage(Guid guid, UITabControl page) => Helper.AddPage(guid, page); - - 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(); @@ -672,9 +658,10 @@ namespace Sunny.UI e.Graphics.DrawString(TabPages[index].Text, Font, index == SelectedIndex ? tabSelectedForeColor : TabUnSelectedForeColor, new Rectangle(TabRect.Left + textLeft, TabRect.Top, TabRect.Width, TabRect.Height), ContentAlignment.MiddleLeft); - var menuItem = Helper[index]; - bool show1 = TabPages[index].Text != MainPage; - bool show2 = menuItem == null || !menuItem.AlwaysOpen; + TabPage tabPage = TabPages[index]; + UIPage uiPage = Helper.GetPage(tabPage); + bool show1 = tabPage.Text != MainPage; + bool show2 = uiPage == null || !uiPage.AlwaysOpen; bool showButton = show1 && show2; if (showButton) @@ -768,9 +755,10 @@ namespace Sunny.UI return; } - var menuItem = Helper[removeIndex]; - bool show1 = TabPages[removeIndex].Text != MainPage; - bool show2 = menuItem == null || !menuItem.AlwaysOpen; + TabPage tabPage = TabPages[removeIndex]; + UIPage uiPage = Helper.GetPage(tabPage); + bool show1 = tabPage.Text != MainPage; + bool show2 = uiPage == null || !uiPage.AlwaysOpen; bool showButton = show1 && show2; if (showButton) { @@ -802,20 +790,6 @@ namespace Sunny.UI public event OnAfterRemoveTabPage AfterRemoveTabPage; - [DefaultValue(true)] - [Description("多页面框架时,包含UIPage,在点击Tab页关闭时关闭UIPage"), Category("SunnyUI")] - public bool AutoClosePage - { - get; set; - } - - [DefaultValue(true)] - [Description("移除TabPage后,是否自动销毁TabPage"), Category("SunnyUI")] - public bool DisposeTabPageAfterRemove - { - get; set; - } - internal void RemoveTabPage(int index) { if (index < 0 || index >= TabCount) @@ -824,44 +798,24 @@ namespace Sunny.UI } TabPage tabPage = TabPages[index]; - var pages = tabPage.GetControls(); - for (int i = 0; i < pages.Count; i++) + UIPage uiPage = Helper.GetPage(tabPage); + if (uiPage != null) { - if (AutoClosePage) - { - PageRemoved?.Invoke(this, new UIPageEventArgs(pages[i])); - - try - { - pages[i].Final(); - pages[i].Close(); - pages[i].Dispose(); - pages[i] = null; - } - catch - { } - } - else - { - pages[i].Parent = null; - } + PageRemoved?.Invoke(this, new UIPageEventArgs(uiPage)); + Helper.RemovePage(uiPage); + AfterRemoveTabPage?.Invoke(this, index); + } + else + { + tabPage.Parent = null; + tabPage.Dispose(); + tabPage = null; } if (TabCount > 1 && index > 0) { SelectedTab = TabPages[index - 1]; } - - TabPages.Remove(tabPage); - AfterRemoveTabPage?.Invoke(this, index); - - //if (TabCount > 0) - //{ - // if (index == 0) SelectedIndex = 0; - // if (index > 0) SelectedIndex = index - 1; - //} - - if (DisposeTabPageAfterRemove) tabPage.Dispose(); } public enum UITabPosition @@ -901,8 +855,6 @@ namespace Sunny.UI timer?.Start(); } - //private int LastIndex; - public void Init() { if (SelectedIndex < 0 || SelectedIndex >= TabPages.Count) diff --git a/SunnyUI/Controls/UITabControlHelper.cs b/SunnyUI/Controls/UITabControlHelper.cs new file mode 100644 index 00000000..cabddcaf --- /dev/null +++ b/SunnyUI/Controls/UITabControlHelper.cs @@ -0,0 +1,216 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Windows.Forms; + +namespace Sunny.UI +{ + public class UITabControlHelper + { + private readonly ConcurrentDictionary PageItems = new(); + + private readonly UITabControl tabControl; + + public UITabControlHelper(UITabControl ctrl) + { + tabControl = ctrl; + } + + public void SetTipsText(int pageIndex, string tipsText) + { + TabPage tabPage = GetTabPage(pageIndex); + if (tabPage != null) tabControl.SetTipsText(tabPage, tipsText); + } + + public void SetTipsText(Guid pageGuid, string tipsText) + { + TabPage tabPage = GetTabPage(pageGuid); + if (tabPage != null) tabControl.SetTipsText(tabPage, tipsText); + } + + public UIPage AddPage(UIPage uiPage) + { + if (uiPage == null) throw new ArgumentNullException("This UIPage can't be empty."); + + if (uiPage.PageGuid == Guid.Empty && uiPage.PageIndex < 0) + { + uiPage.PageGuid = Guid.NewGuid(); + } + + if (PageItems != null && PageItems.Count > 0) + { + if (PageItems.Values.Where(p => p == uiPage).FirstOrDefault() != null) + { + throw new ArgumentException("This UIPage is already exists."); + } + + if (uiPage.PageGuid != Guid.Empty) + { + if (GetPage(uiPage.PageGuid) != null) + { + throw new ArgumentException("This UIPage is already exists."); + } + } + else if (uiPage.PageIndex >= 0) + { + if (GetPage(uiPage.PageIndex) != null) + { + throw new ArgumentException("This UIPage is already exists."); + } + } + } + + TabPage tabPage = CreateTabIfNotExists(uiPage); + uiPage.Dock = DockStyle.Fill; + uiPage.TabPage = tabPage; + tabPage.Controls.Add(uiPage); + tabPage.Text = uiPage.Text; + uiPage.Show(); + + return uiPage; + } + + private TabPage CreateTabIfNotExists(UIPage uiPage) + { + foreach (TabPage tabPage in tabControl.TabPages) + { + if (tabPage.Controls.Count == 0) + { + PageItems.TryAdd(tabPage, uiPage); + return tabPage; + } + } + + TabPage newPage = new TabPage(); + newPage.SuspendLayout(); + tabControl.Controls.Add(newPage); + PageItems.TryAdd(newPage, uiPage); + newPage.ResumeLayout(); + return newPage; + } + + private TabPage GetTabPage(int pageIndex) => GetPage(pageIndex)?.TabPage; + + private TabPage GetTabPage(Guid pageGuid) => GetPage(pageGuid)?.TabPage; + + public event EventHandler TabPageAndUIPageChanged; + + public bool SelectPage(int pageIndex) + { + if (pageIndex < 0) return false; + + if (PageItems.TryGetValue(tabControl.SelectedTab, out var fromPage)) + { + bool isCancel = fromPage.OnPageDeselecting(); + if (isCancel) return false; + } + + TabPage tabPage = GetTabPage(pageIndex); + if (tabPage != null && PageItems.TryGetValue(tabPage, out var toPage)) + { + toPage.Translate(); + tabControl.SelectTab(tabPage); + TabPageAndUIPageChanged?.Invoke(this, new TabPageAndUIPageArgs(tabPage, toPage)); + return true; + } + + return false; + } + + public bool SelectPage(Guid pageGuid) + { + if (pageGuid == Guid.Empty) return false; + + if (PageItems.TryGetValue(tabControl.SelectedTab, out var fromPage)) + { + bool isCancel = fromPage.OnPageDeselecting(); + if (isCancel) return false; + } + + TabPage tabPage = GetTabPage(pageGuid); + if (tabPage != null && PageItems.TryGetValue(tabPage, out var toPage)) + { + toPage.Translate(); + tabControl.SelectTab(tabPage); + TabPageAndUIPageChanged?.Invoke(this, new TabPageAndUIPageArgs(tabPage, toPage)); + return true; + } + + return false; + } + + public UIPage GetPage(TabPage tabPage) + { + if (PageItems.TryGetValue(tabControl.SelectedTab, out var uiPage)) + return uiPage; + return null; + } + + public UIPage GetPage(int pageIndex) => + PageItems.Values.Where(p => p.PageIndex == pageIndex).FirstOrDefault(); + + public UIPage GetPage(Guid pageGuid) => + PageItems.Values.Where(p => p.PageGuid == pageGuid).FirstOrDefault(); + + public T GetPage() where T : UIPage => GetPages().FirstOrDefault(); + + public List GetPages() where T : UIPage + { + List result = new List(); + foreach (var item in PageItems.Values) + { + if (item is T pg) result.Add(pg); + } + + return result; + } + + public void RemoveAllPages(bool keepMainPage = true) + { + var pages = PageItems.Values; + foreach (var page in pages) + { + if (keepMainPage && page.TabPage?.Text == tabControl.MainPage) continue; + RemovePage(page); + } + } + + public bool RemovePage(UIPage uiPage) + { + if (uiPage == null) return false; + TabPage tabPage = uiPage.TabPage; + PageItems.TryRemove(tabPage, out _); + if (tabPage != null) + { + tabPage.Parent = null; + tabPage.Dispose(); + tabPage = null; + } + + uiPage.Final(); + uiPage.Close(); + uiPage.Dispose(); + uiPage = null; + + return true; + } + + public bool RemovePage(int pageIndex) => RemovePage(GetPage(pageIndex)); + + public bool RemovePage(Guid pageGuid) => RemovePage(GetPage(pageGuid)); + } + + public class TabPageAndUIPageArgs : EventArgs + { + public TabPage TabPage { get; set; } + + public UIPage UIPage { get; set; } + + public TabPageAndUIPageArgs(TabPage tabPage, UIPage uiPage) + { + TabPage = tabPage; + UIPage = uiPage; + } + } +} diff --git a/SunnyUI/Forms/UIBaseForm.cs b/SunnyUI/Forms/UIBaseForm.cs index a92c4709..04310abf 100644 --- a/SunnyUI/Forms/UIBaseForm.cs +++ b/SunnyUI/Forms/UIBaseForm.cs @@ -625,7 +625,7 @@ namespace Sunny.UI { get { - bool ReturnFlag = false; + bool ReturnFlag = DesignMode; if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) ReturnFlag = true; else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv") @@ -872,8 +872,7 @@ namespace Sunny.UI private void MainTabControl_TabPageAndUIPageChanged(object sender, TabPageAndUIPageArgs e) { - List pages = e.TabPage.GetControls(); - SelectedPage = pages.Count == 1 ? pages[0] : null; + SelectedPage = e.UIPage; } private void MainTabControl_Deselected(object sender, TabControlEventArgs e) @@ -921,6 +920,11 @@ namespace Sunny.UI { SetDefaultTabControl(); + if (page == null) + { + throw new ArgumentNullException(nameof(page)); + } + if (MainTabControl == null) { throw (new ApplicationException("未指定MainTabControl,无法承载多页面。")); @@ -1072,6 +1076,8 @@ namespace Sunny.UI } SelectedPage?.Translate(); + + if (IsDesignMode) return; this.TranslateOther(); } diff --git a/SunnyUI/Frames/UIMainFrame.cs b/SunnyUI/Frames/UIMainFrame.cs index 27d7d9c9..e1d4026d 100644 --- a/SunnyUI/Frames/UIMainFrame.cs +++ b/SunnyUI/Frames/UIMainFrame.cs @@ -41,14 +41,6 @@ namespace Sunny.UI MainContainer.AfterRemoveTabPage += MainContainer_AfterRemoveTabPage; } - [DefaultValue(false)] - [Description("多页面框架时,包含UIPage,在点击Tab页关闭时关闭UIPage"), Category("SunnyUI")] - public bool AutoClosePage - { - get => MainContainer.AutoClosePage; - set => MainContainer.AutoClosePage = value; - } - private void MainContainer_AfterRemoveTabPage(object sender, int index) { AfterRemoveTabPage?.Invoke(this, index); diff --git a/SunnyUI/Frames/UIPage.cs b/SunnyUI/Frames/UIPage.cs index 332d8f81..3dc0201d 100644 --- a/SunnyUI/Frames/UIPage.cs +++ b/SunnyUI/Frames/UIPage.cs @@ -1124,6 +1124,20 @@ namespace Sunny.UI ReceiveParams?.Invoke(this, e); } + protected bool IsDesignMode + { + get + { + bool ReturnFlag = DesignMode; + if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) + ReturnFlag = true; + else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv") + ReturnFlag = true; + + return ReturnFlag; + } + } + public virtual void Translate() { var controls = this.GetInterfaceControls(true); @@ -1132,6 +1146,7 @@ namespace Sunny.UI control.Translate(); } + if (IsDesignMode) return; this.TranslateOther(); }