Updating UI objects in windows forms - c#

Pre .net I was using MFC, ON_UPDATE_COMMAND_UI, and the CCmdUI class to update the state of my windows UI.
From the older MFC/Win32 reference:
Typically, menu items and toolbar buttons have more than one state. For
example, a menu item is grayed
(dimmed) if it is unavailable in the
present context. Menu items can also
be checked or unchecked. A toolbar
button can also be disabled if
unavailable, or it can be checked.
Who updates the state of these items
as program conditions change?
Logically, if a menu item generates a
command that is handled by, say, a
document, it makes sense to have the
document update the menu item. The
document probably contains the
information on which the update is
based.
If a command has multiple
user-interface objects (perhaps a menu
item and a toolbar button), both are
routed to the same handler function.
This encapsulates your user-interface
update code for all of the equivalent
user-interface objects in a single
place.
The framework provides a convenient
interface for automatically updating
user-interface objects. You can choose
to do the updating in some other way,
but the interface provided is
efficient and easy to use.
What is the guidance for .net Windows Forms? I am using an Application.Idle handler in the main form but am not sure this is the best way to do this. About the time I put all my UI updates in the Idle event handler my app started to show some performance problems, and I don't have the metrics to track this down yet. Not sure if it's related.

I've found it easiest to have the menu item event handler spawn a background thread that disables the menu item, does the work, and then re-enables the menu item. That way, the UI is available to handle other UI requests, and I don't need to poll for when the operation is complete.
I usually include logic that prevents more than one operation that uses the same resources to happen simultaneously. This means creating a function to disable/enable all similar resources at once. e.g. I might only allow 1 file operation to happen at a time, so I would create a function to disable/enable all the menu items associated with file operations and call it from every one of those menu items.

Just change their property, e.g.
obj.Enabled = true;
or
obj.Enabled = false;
The property of that object will automatically call .Invalidate() or .Refresh() for you, so the control should be repainted automatically.
If you want to do a BIG task which would block the UI for multiple seconds, it's worth using Threads + Delegates.

AFAIK, in the standard .NET System.Windows.Forms world, this functionality is not available out the box.
This problem can be answered in a few ways. The links below are useful resources:
•OnUpdate equivalent
•ActionLists for Windows forms
•Command UI Updating Windows Forms in C#

Related

Is there a way to execute Undo and Redo processes in a WPF window for every containing controls (not just focused control)

I have a report part in my WPF application which contains some different WPF Pages. I need my report pages to be edittable by user. Each report page contains different Textbox and RichTextBox controls.
Whenever some control's value is changed, if I press ctrl+z and ctrl+y on my keyboard, Undo and Redo will happen only if the control is focused.
Is there any way to extend the functionality of these key combinations to execute Undo and Redo process on all of existing controls in the page without needing to focus on them?
If you lock your effects under a command pattern you can handle the undo/redo events yourself. As long as the wpf controls involved are bound to the data that is done/undone/redone. That makes it possible.
see Undo/Redo Implementation For Multiple Variables
You could use the brute force approach.
Assuming you're using MVVM ( if you're not then you should learn and use it ).
Handle propertchanged in the viewmodel(s) and persist before and after by serialising the entire viewmodel to a collection of viewmodels. One for each state. You then iterate backwards through that collection to go back and forwards to re-apply.
This is relatively easy to implement but the downside is the potential to lose changes between redo.
You could improve on that by adding a bit more sophistication. Retaining a property name, old and new value per propertychange.

Change Ribbon button without restarting Revit

I am making a plugin who changes depending on a text file.
Is it possible to change a Ribbon button name without restarting the software Revit ?
Yes, it is possible.
You typically construct your panels and buttons during ExternalApplication Startup.
If you store as properties/fields the buttons or other UI elements you need to change, it is possible to change those objects at runtime from your commands or other events.

Add controls to GUI in background worker

I have a form with a top and bottom panel.
The user will be making selections in the top panel, and each time they change a value on one key field I destroy the controls in the bottom panel, then make a time-consuming call to another application (via COM) and add a list of new controls being added. This refresh process takes several seconds.
I'd like to be able to disable the bottom panel while it's being refreshed, and allow the user to be able to keep working in the top panel. Of course, this is all one GUI running on one thread.
Illustrated:
I played with BackgroundWorker, but of course it cannot directly create controls on the main thread's GUI.
Is it possible for me to do this, i.e. allow one part of the GUI to be disabled and rebuilt in the background, while the user continues to work in on another part?
Is it possible for me to do this, i.e. allow one part of the GUI to be disabled and rebuilt in the background, while the user continues to work in on another part?
In general, no. GUI elements/controls must all be created and used on the UI thread, and nowhere else.
The best way to create this type of scenario is typically to use a BackgroundWorker or other technique to get the required data on a background thread, then build your UI after the data has been loaded completely.

Enhance UI Responsiveness in c#

I have created an POS application using Dynamic buttons, dynamic table layouts, and other controls in one form. The generated dynamic buttons is based on the number of data queried from the database(using OPEN ACCESS ORM).
My problem is whenever I run the application, the UI responsiveness is slow. For Example when I click on a category button, the sub category buttons appears (These are dynamic generated buttons), for it to appear, it would take few seconds to appear, then there are some little flash on the screen.
What will I do? What strategy should I perform.
Thank you. So much
Make sure the buttons do not have time consuming actions. And if they do:
use a waitcursor (simple solution, but not liked by most users)
use a thread to perform the action (might complicate other actions regarding thread safety).

Validating menu items

Something that I wanted to know for a long time already:
In a WinForms application you often have an edit menu with entries like Undo and Redo. These menu entries must be enabled or disabled depending on what control has the focus and what it's undo stack state is. For this validation you have to know when the focus changes. Unfortunately, there seems to be no way to accomplish that in a standard C# WinForms application. What I did so far was to add an OnEnter event handler for each control, however this is nasty and doesn't work for controls added by backend code (e.g. in a C++/CLI layer). Another approach is to use a message filter, but WM_FOCUS is not sent through such a filter. Overwriting ActiveControl does not work either, since it is not virtual. So, what else can be done to validate menu items on each focus change?
Note: I have read the article at http://msdn.microsoft.com/en-us/magazine/cc188928.aspx but this approach is way to complicated for such a simple task. I have all my validation code in place already. I only have to trigger it when focus goes to a new control.

Categories