From e67d94f1995f3de8c4d83eeddf71816589c6bcb5 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Sat, 27 May 2023 08:50:30 +0300 Subject: [PATCH] Multiple buttons refreshing --- .../Controllers/RibbonController.cs | 51 ++++++------------- RhSolutions.AddIn/Tools/EventsUtil.cs | 29 +++-------- 2 files changed, 23 insertions(+), 57 deletions(-) diff --git a/RhSolutions.AddIn/Controllers/RibbonController.cs b/RhSolutions.AddIn/Controllers/RibbonController.cs index 140ba10..b5edde0 100644 --- a/RhSolutions.AddIn/Controllers/RibbonController.cs +++ b/RhSolutions.AddIn/Controllers/RibbonController.cs @@ -15,6 +15,7 @@ namespace RhSolutions.Controllers; public class RibbonController : ExcelRibbon { private static IRibbonUI ribbonUi; + private static bool _workbookIsValid; public override string GetCustomUI(string RibbonID) { @@ -81,17 +82,9 @@ public class RibbonController : ExcelRibbon } } - public bool GetConvertEnabled(IRibbonControl control) - { - if (RhSolutionsAddIn.Excel.ActiveWorkbook == null) - return false; - - else - { - Worksheet worksheet = RhSolutionsAddIn.Excel.ActiveWorkbook.ActiveSheet; - return worksheet.IsValidSource(); - } - } + public bool GetConvertEnabled(IRibbonControl control) => _workbookIsValid; + public bool GetDxfEnabled(IRibbonControl control) => _workbookIsValid; + public bool GetGuessEnabled(IRibbonControl control) => RhSolutionsAddIn.Excel.ActiveWorkbook == null ? false : !_workbookIsValid; public bool GetExportEnabled(IRibbonControl control) { @@ -105,30 +98,6 @@ public class RibbonController : ExcelRibbon } } - public bool GetDxfEnabled(IRibbonControl control) - { - if (RhSolutionsAddIn.Excel.ActiveWorkbook == null) - return false; - - else - { - Worksheet worksheet = RhSolutionsAddIn.Excel.ActiveWorkbook.ActiveSheet; - return worksheet.IsValidSource(); - } - } - - public bool GetGuessEnabled(IRibbonControl control) - { - if (RhSolutionsAddIn.Excel.ActiveWorkbook == null) - return false; - - else - { - Worksheet worksheet = RhSolutionsAddIn.Excel.ActiveWorkbook.ActiveSheet; - return !worksheet.IsValidSource(); - } - } - public string GetVersionLabel(IRibbonControl control) { string version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); @@ -140,4 +109,16 @@ public class RibbonController : ExcelRibbon string name = RhSolutionsAddIn.Configuration.GetPriceListFileName(); return string.IsNullOrEmpty(name) ? "Шаблонный файл" : name; } + + public static void UpdateWorkbookValidation() + { + if (RhSolutionsAddIn.Excel.ActiveWorkbook == null) + _workbookIsValid = false; + + else + { + Worksheet worksheet = RhSolutionsAddIn.Excel.ActiveWorkbook.ActiveSheet; + _workbookIsValid = worksheet.IsValidSource(); + } + } } diff --git a/RhSolutions.AddIn/Tools/EventsUtil.cs b/RhSolutions.AddIn/Tools/EventsUtil.cs index 92aa717..07da3e5 100644 --- a/RhSolutions.AddIn/Tools/EventsUtil.cs +++ b/RhSolutions.AddIn/Tools/EventsUtil.cs @@ -16,35 +16,25 @@ namespace RhSolutions.Tools public static void Initialize() { RhSolutionsAddIn.Excel.SheetSelectionChange += RefreshExportButton; - RhSolutionsAddIn.Excel.SheetActivate += RefreshConvertButton; - RhSolutionsAddIn.Excel.SheetActivate += RefreshDxfButton; - RhSolutionsAddIn.Excel.SheetActivate += RefreshGuessButton; - RhSolutionsAddIn.Excel.WorkbookActivate += RefreshConvertButton; - RhSolutionsAddIn.Excel.WorkbookActivate += RefreshDxfButton; - RhSolutionsAddIn.Excel.WorkbookActivate += RefreshGuessButton; + RhSolutionsAddIn.Excel.SheetActivate += RefreshButtons; + RhSolutionsAddIn.Excel.WorkbookActivate += RefreshButtons; RhSolutionsAddIn.Configuration.OnSettingsChange += RefreshSettingTitle; } public static void Uninitialize() { RhSolutionsAddIn.Excel.SheetSelectionChange -= RefreshExportButton; - RhSolutionsAddIn.Excel.SheetActivate -= RefreshConvertButton; - RhSolutionsAddIn.Excel.SheetActivate -= RefreshDxfButton; - RhSolutionsAddIn.Excel.SheetActivate -= RefreshGuessButton; - RhSolutionsAddIn.Excel.WorkbookActivate -= RefreshConvertButton; - RhSolutionsAddIn.Excel.WorkbookActivate -= RefreshDxfButton; - RhSolutionsAddIn.Excel.WorkbookActivate -= RefreshGuessButton; + RhSolutionsAddIn.Excel.SheetActivate -= RefreshButtons; + RhSolutionsAddIn.Excel.WorkbookActivate -= RefreshButtons; RhSolutionsAddIn.Configuration.OnSettingsChange -= RefreshSettingTitle; } - private static void RefreshConvertButton(object sh) + private static void RefreshButtons(object sh) { + RibbonController.UpdateWorkbookValidation(); RibbonController.RefreshControl("convert"); - } - - private static void RefreshDxfButton(object sh) - { RibbonController.RefreshControl("dxfexport"); + RibbonController.RefreshControl("Guessexport"); } private static void RefreshExportButton(object sh, Range target) @@ -52,11 +42,6 @@ namespace RhSolutions.Tools RibbonController.RefreshControl("export"); } - private static void RefreshGuessButton(object sh) - { - RibbonController.RefreshControl("Guessexport"); - } - private static void RefreshSettingTitle(object sender, SettingChangingEventArgs e) { RibbonController.RefreshControl("setPriceList");