Éteignez la mise à jour de l'écran et rallumez-la si c'était sur pour commencer?

Lorsque vous exécutez des procédures qui utilisent Excel, je supprime typiquement certains parameters d'application au début de la procédure, puis rallumez-les à la fin de la procédure.

Code pour désactiver ou désactiver les parameters de l'application:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace XLTimeTracker { class API { public static void TurnAppSettingsOff() { AddinModule.CurrentInstance.ExcelApp.EnableEvents = false; AddinModule.CurrentInstance.ExcelApp.ScreenUpdating = false; } public static void TurnAppSettingsOn() { if (AddinModule.CurrentInstance.ExcelApp == null) return; AddinModule.CurrentInstance.ExcelApp.EnableEvents = true; AddinModule.CurrentInstance.ExcelApp.ScreenUpdating = true; } } } 

J'appelle ces procédures de la manière suivante:

 API.TurnAppSettingsOff(); // my code API.TurnAppSettingsOn(); 

Cela fonctionne bien.

Mais dis que je veux seulement activer les parameters de l'application qui étaient sur avant d' API.TurnAppSettingsOff() . Quelle serait une bonne façon de coder cela?

Quelques idées:

  • Je suppose que je devrais sauvegarder l'état précédent des parameters de l'application en quelque sorte. Par exemple, en écrivant:

Boolean screenUpdating = AddinModule.CurrentInstance.ExcelApp.ScreenUpdating;

  • J'aimerais que le résultat final soit correct même si une fonction qui active ou désactive les applets appelle également une autre fonction qui active et désactive les parameters de l'application.

  • Je ne sais pas s'il est préférable de définir tous les parameters à l'aide d'une command telle que API.TurnAppSettingsOff() ou s'il serait plus API.TurnScreenUpdatingOff() pour l' API.TurnScreenUpdatingOff() user. API.TurnScreenUpdatingOff() et API.TurnEventsOff() .

Je voudrais simplement regrouper une class de gestionnaire interne qui gère tout ce qui est semblable au suivant:

 public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } { public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } { public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } } public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } { public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } { public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } } public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } { public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } } public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } } public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } { public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } { public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } { public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } } public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } { public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } } public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } } public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } } public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } { public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } } public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } { public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } { public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } } public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } } public sealed class ApplicationSettingsManager { readonly object app; readonly Dictionary<ssortingng, object> appSettings; public ApplicationSettingsManager(object app) { this.app = app; appSettings = new Dictionary<ssortingng, object>(); } public object Application { get { return app; } } public void SaveSetting(ssortingng settingName) { var propInfo = app.GetType().GetProperty(settingName); if (propInfo == null) throw new ArgumentException("Specified name is not a valid storable setting.", "setting"); var value = propInfo.GetValue(app); if (appSettings.ContainsKey(settingName)) { appSettings[settingName] = value; } else { appSettings.Add(settingName, value); } } public void SaveAllSettings() { var properties = app.GetType().GetProperties().Where(p => p.CanWrite && p.CanRead && p.SetMethod.IsPublic && p.GetMethod.IsPublic); foreach (var p in properties) { var value = p.GetValue(app); if (appSettings.ContainsKey(p.Name)) { appSettings[p.Name] = value; } else { appSettings.Add(p.Name, value); } } } public void RestoreSetting(ssortingng settingName) { if (!appSettings.ContainsKey(settingName)) throw new ArgumentException("Specified name does not correspond to a valid stored setting.", "settingName"); var propInfo = app.GetType().GetProperty(settingName); propInfo.SetValue(app, appSettings[settingName]); } public void RestoreAllSettingas() { foreach (var p in appSettings) { RestoreSetting(p.Key); } } } 

Cela devrait faire l'affaire. Vous l'utiliseriez comme suit;

 var excelSettingsManager = new ApplicationSettingsManager(AddinModule.CurrentInstance.ExcelApp); //store all settings you are going to tamper with... excelSettingsManager.SaveSetting("EnableEvents"); excelSettingsManager.SaveSetting("ScreenUpdating"); //change excel setting and do your thing... //... //when done, restore settings excelSettingsManager.RestoreAllSettings(); 

Et vous avez terminé!

Voici ma propre solution que j'ai proposée pour résoudre le problème. Il me semble plus simple que les autres solutions proposées. Faites-moi savoir si vous pensez que vous avez une meilleure solution!

(Mon code ci-dessous se trouve dans VB.NET)

Classe pour gérer le changement de parameters et le stockage de l'état d'origine:

 Public Class ApplicationSettings Implements IDisposable Private ScreenUpdating As Boolean Private Events As Boolean Private Production As Boolean = True Public Sub New() MyBase.New() ScreenUpdating = AddinModule.ExcelApp.ScreenUpdating Events = AddinModule.ExcelApp.EnableEvents End Sub Public Sub Dispose() Implements IDisposable.Dispose AddinModule.ExcelApp.ScreenUpdating = ScreenUpdating AddinModule.ExcelApp.EnableEvents = Events End Sub End Class 

Et voici comment je l'utilise dans le code:

 Private Sub AdxRibbonButton1_OnClick(sender As Object, control As IRibbonControl, pressed As Boolean) Handles AdxRibbonButton1.OnClick Using New ApplicationSettings 'My code End Using End Sub 

Je reorderais d'utiliser un integer pour représenter l'état et le cacher à l'aide d'un boolean. C'est ce que je veux dire:

 this.Working = true; try { // do something } finally { this.Working = false; } { this.Working = true; try { // do something } finally { this.Working = false; } } this.Working = true; try { // do something } finally { this.Working = false; } { this.Working = true; try { // do something } finally { this.Working = false; } 

Ensuite, appliquez la propriété Working comme ceci:

 private int working; public bool Working { get { return working > 0; } set { if (value) { working++; } else { working--; } } } { private int working; public bool Working { get { return working > 0; } set { if (value) { working++; } else { working--; } } } } private int working; public bool Working { get { return working > 0; } set { if (value) { working++; } else { working--; } } } { private int working; public bool Working { get { return working > 0; } set { if (value) { working++; } else { working--; } } } { private int working; public bool Working { get { return working > 0; } set { if (value) { working++; } else { working--; } } } } private int working; public bool Working { get { return working > 0; } set { if (value) { working++; } else { working--; } } } { private int working; public bool Working { get { return working > 0; } set { if (value) { working++; } else { working--; } } } } private int working; public bool Working { get { return working > 0; } set { if (value) { working++; } else { working--; } } } } private int working; public bool Working { get { return working > 0; } set { if (value) { working++; } else { working--; } } } 

En interne, il suffit de se callbacker combien de time fonctionnent comme défini, en utilisant cet entier. Si le working est 0 , vous revenez à la normale.

Vous pouvez définir Working autant de fois que vous le souhaitez. Tant qu'il sera > 0 il va revenir true . N'oubliez pas d'envelopper votre code dans un try...catch ou vous allez perdre le count.

Vous pouvez faire l'appel API si Working est défini sur false . Ensuite, définissez-le sur true :

 if (!this.Working) { // do API call this.TurnAppSettingsOff(); } this.Working = true; try { // do something } finally { this.Working = false; } if (!this.Working) { // do reset API call this.TurnAppSettingsOn(); } { if (!this.Working) { // do API call this.TurnAppSettingsOff(); } this.Working = true; try { // do something } finally { this.Working = false; } if (!this.Working) { // do reset API call this.TurnAppSettingsOn(); } } if (!this.Working) { // do API call this.TurnAppSettingsOff(); } this.Working = true; try { // do something } finally { this.Working = false; } if (!this.Working) { // do reset API call this.TurnAppSettingsOn(); } { if (!this.Working) { // do API call this.TurnAppSettingsOff(); } this.Working = true; try { // do something } finally { this.Working = false; } if (!this.Working) { // do reset API call this.TurnAppSettingsOn(); } } if (!this.Working) { // do API call this.TurnAppSettingsOff(); } this.Working = true; try { // do something } finally { this.Working = false; } if (!this.Working) { // do reset API call this.TurnAppSettingsOn(); } { if (!this.Working) { // do API call this.TurnAppSettingsOff(); } this.Working = true; try { // do something } finally { this.Working = false; } if (!this.Working) { // do reset API call this.TurnAppSettingsOn(); } } if (!this.Working) { // do API call this.TurnAppSettingsOff(); } this.Working = true; try { // do something } finally { this.Working = false; } if (!this.Working) { // do reset API call this.TurnAppSettingsOn(); } { if (!this.Working) { // do API call this.TurnAppSettingsOff(); } this.Working = true; try { // do something } finally { this.Working = false; } if (!this.Working) { // do reset API call this.TurnAppSettingsOn(); } 

Vous pouvez utiliser la class Stack<T> pour implémenter un comportement dernier-en-premier sorti (LIFO).

 struct ExcelEventSettings { public bool EnableEvents; public bool ScreenUpdating; } class Example { private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>(); // you can call this function as often as you called SaveAppSettings public void RestoreAppSettings() { if (settingStack.Count == 0) throw new Exception("There is no previous state!"); ExcelEventSettings prevState = settingStack.Pop(); setCurrentEnableEvents(prevState.EnableEvents); setCurrentScreenUpdating(prevState.ScreenUpdating); } public void SetAppSettings(bool enableEvents, bool screenUpdating) { ExcelEventSettings currentState; currentState.EnableEvents = getCurrentEnableEvents(); currentState.ScreenUpdating = getCurrentScreenUpdating(); settingStack.Push(currentState); setCurrentScreenUpdating(enableEvents); setCurrentEnableEvents(screenUpdating); } private bool getCurrentEnableEvents() { // Here you would call your Excel function } private bool getCurrentScreenUpdating() { // Here you would call your Excel function } private void setCurrentEnableEvents(bool value) { // Here you would call your Excel function } private void setCurrentScreenUpdating(bool value) { // Here you would call your Excel function } } { struct ExcelEventSettings { public bool EnableEvents; public bool ScreenUpdating; } class Example { private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>(); // you can call this function as often as you called SaveAppSettings public void RestoreAppSettings() { if (settingStack.Count == 0) throw new Exception("There is no previous state!"); ExcelEventSettings prevState = settingStack.Pop(); setCurrentEnableEvents(prevState.EnableEvents); setCurrentScreenUpdating(prevState.ScreenUpdating); } public void SetAppSettings(bool enableEvents, bool screenUpdating) { ExcelEventSettings currentState; currentState.EnableEvents = getCurrentEnableEvents(); currentState.ScreenUpdating = getCurrentScreenUpdating(); settingStack.Push(currentState); setCurrentScreenUpdating(enableEvents); setCurrentEnableEvents(screenUpdating); } private bool getCurrentEnableEvents() { // Here you would call your Excel function } private bool getCurrentScreenUpdating() { // Here you would call your Excel function } private void setCurrentEnableEvents(bool value) { // Here you would call your Excel function } private void setCurrentScreenUpdating(bool value) { // Here you would call your Excel function } } } struct ExcelEventSettings { public bool EnableEvents; public bool ScreenUpdating; } class Example { private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>(); // you can call this function as often as you called SaveAppSettings public void RestoreAppSettings() { if (settingStack.Count == 0) throw new Exception("There is no previous state!"); ExcelEventSettings prevState = settingStack.Pop(); setCurrentEnableEvents(prevState.EnableEvents); setCurrentScreenUpdating(prevState.ScreenUpdating); } public void SetAppSettings(bool enableEvents, bool screenUpdating) { ExcelEventSettings currentState; currentState.EnableEvents = getCurrentEnableEvents(); currentState.ScreenUpdating = getCurrentScreenUpdating(); settingStack.Push(currentState); setCurrentScreenUpdating(enableEvents); setCurrentEnableEvents(screenUpdating); } private bool getCurrentEnableEvents() { // Here you would call your Excel function } private bool getCurrentScreenUpdating() { // Here you would call your Excel function } private void setCurrentEnableEvents(bool value) { // Here you would call your Excel function } private void setCurrentScreenUpdating(bool value) { // Here you would call your Excel function } } { struct ExcelEventSettings { public bool EnableEvents; public bool ScreenUpdating; } class Example { private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>(); // you can call this function as often as you called SaveAppSettings public void RestoreAppSettings() { if (settingStack.Count == 0) throw new Exception("There is no previous state!"); ExcelEventSettings prevState = settingStack.Pop(); setCurrentEnableEvents(prevState.EnableEvents); setCurrentScreenUpdating(prevState.ScreenUpdating); } public void SetAppSettings(bool enableEvents, bool screenUpdating) { ExcelEventSettings currentState; currentState.EnableEvents = getCurrentEnableEvents(); currentState.ScreenUpdating = getCurrentScreenUpdating(); settingStack.Push(currentState); setCurrentScreenUpdating(enableEvents); setCurrentEnableEvents(screenUpdating); } private bool getCurrentEnableEvents() { // Here you would call your Excel function } private bool getCurrentScreenUpdating() { // Here you would call your Excel function } private void setCurrentEnableEvents(bool value) { // Here you would call your Excel function } private void setCurrentScreenUpdating(bool value) { // Here you would call your Excel function } } { struct ExcelEventSettings { public bool EnableEvents; public bool ScreenUpdating; } class Example { private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>(); // you can call this function as often as you called SaveAppSettings public void RestoreAppSettings() { if (settingStack.Count == 0) throw new Exception("There is no previous state!"); ExcelEventSettings prevState = settingStack.Pop(); setCurrentEnableEvents(prevState.EnableEvents); setCurrentScreenUpdating(prevState.ScreenUpdating); } public void SetAppSettings(bool enableEvents, bool screenUpdating) { ExcelEventSettings currentState; currentState.EnableEvents = getCurrentEnableEvents(); currentState.ScreenUpdating = getCurrentScreenUpdating(); settingStack.Push(currentState); setCurrentScreenUpdating(enableEvents); setCurrentEnableEvents(screenUpdating); } private bool getCurrentEnableEvents() { // Here you would call your Excel function } private bool getCurrentScreenUpdating() { // Here you would call your Excel function } private void setCurrentEnableEvents(bool value) { // Here you would call your Excel function } private void setCurrentScreenUpdating(bool value) { // Here you would call your Excel function } } } struct ExcelEventSettings { public bool EnableEvents; public bool ScreenUpdating; } class Example { private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>(); // you can call this function as often as you called SaveAppSettings public void RestoreAppSettings() { if (settingStack.Count == 0) throw new Exception("There is no previous state!"); ExcelEventSettings prevState = settingStack.Pop(); setCurrentEnableEvents(prevState.EnableEvents); setCurrentScreenUpdating(prevState.ScreenUpdating); } public void SetAppSettings(bool enableEvents, bool screenUpdating) { ExcelEventSettings currentState; currentState.EnableEvents = getCurrentEnableEvents(); currentState.ScreenUpdating = getCurrentScreenUpdating(); settingStack.Push(currentState); setCurrentScreenUpdating(enableEvents); setCurrentEnableEvents(screenUpdating); } private bool getCurrentEnableEvents() { // Here you would call your Excel function } private bool getCurrentScreenUpdating() { // Here you would call your Excel function } private void setCurrentEnableEvents(bool value) { // Here you would call your Excel function } private void setCurrentScreenUpdating(bool value) { // Here you would call your Excel function } } { struct ExcelEventSettings { public bool EnableEvents; public bool ScreenUpdating; } class Example { private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>(); // you can call this function as often as you called SaveAppSettings public void RestoreAppSettings() { if (settingStack.Count == 0) throw new Exception("There is no previous state!"); ExcelEventSettings prevState = settingStack.Pop(); setCurrentEnableEvents(prevState.EnableEvents); setCurrentScreenUpdating(prevState.ScreenUpdating); } public void SetAppSettings(bool enableEvents, bool screenUpdating) { ExcelEventSettings currentState; currentState.EnableEvents = getCurrentEnableEvents(); currentState.ScreenUpdating = getCurrentScreenUpdating(); settingStack.Push(currentState); setCurrentScreenUpdating(enableEvents); setCurrentEnableEvents(screenUpdating); } private bool getCurrentEnableEvents() { // Here you would call your Excel function } private bool getCurrentScreenUpdating() { // Here you would call your Excel function } private void setCurrentEnableEvents(bool value) { // Here you would call your Excel function } private void setCurrentScreenUpdating(bool value) { // Here you would call your Excel function } } } struct ExcelEventSettings { public bool EnableEvents; public bool ScreenUpdating; } class Example { private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>(); // you can call this function as often as you called SaveAppSettings public void RestoreAppSettings() { if (settingStack.Count == 0) throw new Exception("There is no previous state!"); ExcelEventSettings prevState = settingStack.Pop(); setCurrentEnableEvents(prevState.EnableEvents); setCurrentScreenUpdating(prevState.ScreenUpdating); } public void SetAppSettings(bool enableEvents, bool screenUpdating) { ExcelEventSettings currentState; currentState.EnableEvents = getCurrentEnableEvents(); currentState.ScreenUpdating = getCurrentScreenUpdating(); settingStack.Push(currentState); setCurrentScreenUpdating(enableEvents); setCurrentEnableEvents(screenUpdating); } private bool getCurrentEnableEvents() { // Here you would call your Excel function } private bool getCurrentScreenUpdating() { // Here you would call your Excel function } private void setCurrentEnableEvents(bool value) { // Here you would call your Excel function } private void setCurrentScreenUpdating(bool value) { // Here you would call your Excel function } } { struct ExcelEventSettings { public bool EnableEvents; public bool ScreenUpdating; } class Example { private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>(); // you can call this function as often as you called SaveAppSettings public void RestoreAppSettings() { if (settingStack.Count == 0) throw new Exception("There is no previous state!"); ExcelEventSettings prevState = settingStack.Pop(); setCurrentEnableEvents(prevState.EnableEvents); setCurrentScreenUpdating(prevState.ScreenUpdating); } public void SetAppSettings(bool enableEvents, bool screenUpdating) { ExcelEventSettings currentState; currentState.EnableEvents = getCurrentEnableEvents(); currentState.ScreenUpdating = getCurrentScreenUpdating(); settingStack.Push(currentState); setCurrentScreenUpdating(enableEvents); setCurrentEnableEvents(screenUpdating); } private bool getCurrentEnableEvents() { // Here you would call your Excel function } private bool getCurrentScreenUpdating() { // Here you would call your Excel function } private void setCurrentEnableEvents(bool value) { // Here you would call your Excel function } private void setCurrentScreenUpdating(bool value) { // Here you would call your Excel function } } } struct ExcelEventSettings { public bool EnableEvents; public bool ScreenUpdating; } class Example { private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>(); // you can call this function as often as you called SaveAppSettings public void RestoreAppSettings() { if (settingStack.Count == 0) throw new Exception("There is no previous state!"); ExcelEventSettings prevState = settingStack.Pop(); setCurrentEnableEvents(prevState.EnableEvents); setCurrentScreenUpdating(prevState.ScreenUpdating); } public void SetAppSettings(bool enableEvents, bool screenUpdating) { ExcelEventSettings currentState; currentState.EnableEvents = getCurrentEnableEvents(); currentState.ScreenUpdating = getCurrentScreenUpdating(); settingStack.Push(currentState); setCurrentScreenUpdating(enableEvents); setCurrentEnableEvents(screenUpdating); } private bool getCurrentEnableEvents() { // Here you would call your Excel function } private bool getCurrentScreenUpdating() { // Here you would call your Excel function } private void setCurrentEnableEvents(bool value) { // Here you would call your Excel function } private void setCurrentScreenUpdating(bool value) { // Here you would call your Excel function } } { struct ExcelEventSettings { public bool EnableEvents; public bool ScreenUpdating; } class Example { private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>(); // you can call this function as often as you called SaveAppSettings public void RestoreAppSettings() { if (settingStack.Count == 0) throw new Exception("There is no previous state!"); ExcelEventSettings prevState = settingStack.Pop(); setCurrentEnableEvents(prevState.EnableEvents); setCurrentScreenUpdating(prevState.ScreenUpdating); } public void SetAppSettings(bool enableEvents, bool screenUpdating) { ExcelEventSettings currentState; currentState.EnableEvents = getCurrentEnableEvents(); currentState.ScreenUpdating = getCurrentScreenUpdating(); settingStack.Push(currentState); setCurrentScreenUpdating(enableEvents); setCurrentEnableEvents(screenUpdating); } private bool getCurrentEnableEvents() { // Here you would call your Excel function } private bool getCurrentScreenUpdating() { // Here you would call your Excel function } private void setCurrentEnableEvents(bool value) { // Here you would call your Excel function } private void setCurrentScreenUpdating(bool value) { // Here you would call your Excel function } } } struct ExcelEventSettings { public bool EnableEvents; public bool ScreenUpdating; } class Example { private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>(); // you can call this function as often as you called SaveAppSettings public void RestoreAppSettings() { if (settingStack.Count == 0) throw new Exception("There is no previous state!"); ExcelEventSettings prevState = settingStack.Pop(); setCurrentEnableEvents(prevState.EnableEvents); setCurrentScreenUpdating(prevState.ScreenUpdating); } public void SetAppSettings(bool enableEvents, bool screenUpdating) { ExcelEventSettings currentState; currentState.EnableEvents = getCurrentEnableEvents(); currentState.ScreenUpdating = getCurrentScreenUpdating(); settingStack.Push(currentState); setCurrentScreenUpdating(enableEvents); setCurrentEnableEvents(screenUpdating); } private bool getCurrentEnableEvents() { // Here you would call your Excel function } private bool getCurrentScreenUpdating() { // Here you would call your Excel function } private void setCurrentEnableEvents(bool value) { // Here you would call your Excel function } private void setCurrentScreenUpdating(bool value) { // Here you would call your Excel function } } { struct ExcelEventSettings { public bool EnableEvents; public bool ScreenUpdating; } class Example { private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>(); // you can call this function as often as you called SaveAppSettings public void RestoreAppSettings() { if (settingStack.Count == 0) throw new Exception("There is no previous state!"); ExcelEventSettings prevState = settingStack.Pop(); setCurrentEnableEvents(prevState.EnableEvents); setCurrentScreenUpdating(prevState.ScreenUpdating); } public void SetAppSettings(bool enableEvents, bool screenUpdating) { ExcelEventSettings currentState; currentState.EnableEvents = getCurrentEnableEvents(); currentState.ScreenUpdating = getCurrentScreenUpdating(); settingStack.Push(currentState); setCurrentScreenUpdating(enableEvents); setCurrentEnableEvents(screenUpdating); } private bool getCurrentEnableEvents() { // Here you would call your Excel function } private bool getCurrentScreenUpdating() { // Here you would call your Excel function } private void setCurrentEnableEvents(bool value) { // Here you would call your Excel function } private void setCurrentScreenUpdating(bool value) { // Here you would call your Excel function } } } struct ExcelEventSettings { public bool EnableEvents; public bool ScreenUpdating; } class Example { private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>(); // you can call this function as often as you called SaveAppSettings public void RestoreAppSettings() { if (settingStack.Count == 0) throw new Exception("There is no previous state!"); ExcelEventSettings prevState = settingStack.Pop(); setCurrentEnableEvents(prevState.EnableEvents); setCurrentScreenUpdating(prevState.ScreenUpdating); } public void SetAppSettings(bool enableEvents, bool screenUpdating) { ExcelEventSettings currentState; currentState.EnableEvents = getCurrentEnableEvents(); currentState.ScreenUpdating = getCurrentScreenUpdating(); settingStack.Push(currentState); setCurrentScreenUpdating(enableEvents); setCurrentEnableEvents(screenUpdating); } private bool getCurrentEnableEvents() { // Here you would call your Excel function } private bool getCurrentScreenUpdating() { // Here you would call your Excel function } private void setCurrentEnableEvents(bool value) { // Here you would call your Excel function } private void setCurrentScreenUpdating(bool value) { // Here you would call your Excel function } } { struct ExcelEventSettings { public bool EnableEvents; public bool ScreenUpdating; } class Example { private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>(); // you can call this function as often as you called SaveAppSettings public void RestoreAppSettings() { if (settingStack.Count == 0) throw new Exception("There is no previous state!"); ExcelEventSettings prevState = settingStack.Pop(); setCurrentEnableEvents(prevState.EnableEvents); setCurrentScreenUpdating(prevState.ScreenUpdating); } public void SetAppSettings(bool enableEvents, bool screenUpdating) { ExcelEventSettings currentState; currentState.EnableEvents = getCurrentEnableEvents(); currentState.ScreenUpdating = getCurrentScreenUpdating(); settingStack.Push(currentState); setCurrentScreenUpdating(enableEvents); setCurrentEnableEvents(screenUpdating); } private bool getCurrentEnableEvents() { // Here you would call your Excel function } private bool getCurrentScreenUpdating() { // Here you would call your Excel function } private void setCurrentEnableEvents(bool value) { // Here you would call your Excel function } private void setCurrentScreenUpdating(bool value) { // Here you would call your Excel function } } } struct ExcelEventSettings { public bool EnableEvents; public bool ScreenUpdating; } class Example { private Stack<ExcelEventSettings> settingStack = new Stack<ExcelEventSettings>(); // you can call this function as often as you called SaveAppSettings public void RestoreAppSettings() { if (settingStack.Count == 0) throw new Exception("There is no previous state!"); ExcelEventSettings prevState = settingStack.Pop(); setCurrentEnableEvents(prevState.EnableEvents); setCurrentScreenUpdating(prevState.ScreenUpdating); } public void SetAppSettings(bool enableEvents, bool screenUpdating) { ExcelEventSettings currentState; currentState.EnableEvents = getCurrentEnableEvents(); currentState.ScreenUpdating = getCurrentScreenUpdating(); settingStack.Push(currentState); setCurrentScreenUpdating(enableEvents); setCurrentEnableEvents(screenUpdating); } private bool getCurrentEnableEvents() { // Here you would call your Excel function } private bool getCurrentScreenUpdating() { // Here you would call your Excel function } private void setCurrentEnableEvents(bool value) { // Here you would call your Excel function } private void setCurrentScreenUpdating(bool value) { // Here you would call your Excel function } }