Note that there are some explanatory texts on larger screens.

plurals
  1. POHow bad is the following snippet?
    text
    copied!<p>My question is simple: how bad is the following snippet of code? How would <em>you</em> do it?</p> <pre><code>CancelEventHandler _windowClosing; private CancelEventHandler WindowClosing { set { clearEventHandlerList(); this.Closing += value; _windowClosing = value; /* * if calling the method with null parameters, * it will set up itself as the primary control on the Window */ _windowClosing(null,null); } get { return _windowClosing; } } private readonly CancelEventHandler[] CONTROLS = null; private int current = 0; public InitializerForm() { InitializeComponent(); /* * these are the handlers for the different controls, * in the order of appereance to the user */ STATES = new CancelEventHandler[] { handler1, handler2, etc. }; WindowClosing = CONTROLS[0]; } private void clearEventHandlerList() { foreach (CancelEventHandler c in CONTROLS) { this.Closing -= c; } } private void handler1(object obj, CancelEventArgs e) { if (obj == null) { //hide every other control, but this one, also set up if necessary } else { //do something WindowClosing = CONTROLS[++current]; // set the next control to show e.Cancel = true; } } </code></pre> <p>The point would be that the code wouldn't close a form, but instead show another component on it, and the set the way to handle that (this is mobile platform, so clicking OK button on the top generates a closing event). This is because showing several forms (4 or 5) one after another to the user makes the app blink, and also very annoying, while replacing just components is much smoother. This model works, but seems very nasty, and I would like a cleaner way to handle this.</p> <p><strong>Update:</strong> I updated the code sample so that variable names are somewhat speaky. Still, I'm convinced this is awful, (a) but not sure how much, and more importantly, (b) how to do it better.</p> <p><strong>Update 2:</strong> So, it seems that the code is still a bit mysterious.</p> <p>Now here's what the problem is: I show the user a form, which instructs him what to do in several languages. He proceeds by clicking OK on the window. Next, I ask for his language, and then a few questions (where his/her GPS is, etc.) like this. After he could answer the questions (this shouldn't take more than a few seconds each), I show him a splash screen (I load stuff in a separate thread meanwhile), which has a picture. Showing these forms one after another makes the whole application start slow, and filled with UI lags.</p> <p>Here's what I do to work around the lags: I put the content of the windows into panels, and put those panels one on another, and hide every one of them but the one that should be visible to the user. (<code>current</code> variable) Each of the windows does different things, so I need to change handler of the window <code>closing</code> event in addition. In this code the part which enables the panel is in the same function (<code>handler1</code>, <code>handler2</code>, etc.) with the part which handles the window closing event. If the arguments are <code>null</code>, it does the former, if it isn't (that means it was triggered by the user) it does the latter.</p> <p>I need an extensible solution to this so that I can insert and remove dialogs anytime I want (the order and the pointers to the functions are stored in the <code>CONTROLS</code> field, and this seems to be very convenient, if you actually understand it. Although it is never easy to change the entire content of a form, there ought to be a simpler way to do this, as well a nicer one, that is what I'm looking for.</p> <p>I hope this time I could explain how the model works.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload