I have a question regarding a somewhat simple problem, but I think with many solutions and I don't know what would be the best.
I have the following scenario including:
ApplicationForm
Proxy (Client -> Server side)
Possible error message dialog.
The form sends a request to do something on the server side and the form closes (this is the current implementation). I changed this so the method that is being called on the proxy, server side returns a string value that contains an error message if something fails.
The problem is that I want to display a message box when the response comes back if the message is not String.Empty
I do the following:
Call method and assign value returned to a property field.
Create a timer that has an event that fires after 4 seconds.
... Meanwhile the form closes...
Timer event fires, Check the property for a possible error in the string and fire another event that calls a method on the form and displays a message box.
The thing is, I would like to keep the form opened and after the user clicks OK on the message dialog box the form should close.
Any idea is good.
Related
I have a button that uses
OnClientClick="document.forms[0].target = '_blank'; window.setTimeout(fixform, 500);"
to allow the OnClick event to Response.Redirect to a new window. "fixform" sets the target back to normal. This works great when everything goes according to plan. How do I stop it from opening a new window when an exception is thrown on the OnClick event? I've tried Thread.Sleep for 500ms for the form to fix itself, but it still opens the current page in a new window, with the exception.
How do I stop it from opening a new window when an exception is thrown on the OnClick event?
You don't, because it doesn't execute that event until after the new window is opened. The client-side code and server-side code are executing at entirely different times on entirely different machines. The Thread.Sleep() for example happens on the server, after the request has already been made. What's happening here is:
Browser opens a new window
New window makes a request to the server
Server throws an error and responds to the request with the error
Without knowing more about the overall structure and user experience being achieved here, it's hard to advise. What should happen in the event of an error? Can the error be more meaningfully handled so that at least something useful is presented in the new window? (That is, the exception itself shouldn't be visible in the new window, and should never be visible to a user, but rather some page which handles it.)
If you want the new window to close in the event of an error, then one possibility could be to emit some JavaScript like this to the page in the event of an error:
window.close();
That way if there's no error, the window stays open. But if there is an error, the client-side code closes the window. (Since server-side code can't close the window, and indeed has no concept of a "window" in this case.)
Convert OnClick method to WebMethod, returning url instead of redirecting to it.
Call WebMethod from JavaScript via OnClientClick event, returning true/false to cause OnClick to fire only if the call succeeds, updating a hiddenfield value if it does
New OnClick event Response.Redirects to hiddenfields value
I try to show a MESSAGE to the user while an operation is executed. The MESSAGE won't show any button. Just a MESSAGE (text) and maybe a background image.
The problem is the following:
MessageBox does not seem to be the good control (because of button and it blocks the running process).
Form.ShowDialog() also blocks the running process. I don't know what to do.
I want to show the message, run my process, and dispose the message when the process is done.
How to achieve this in C# ?
Create a simple form with the message (or expose a public property to be able to change the message, or a constructor with message parameter to pass it in) and show the form using this Show overload. Then disable the (entire) original (owner) form (or just disable the controls you don't want accesible).
So, in your "main" form do this:
Form f = new MessageForm();
f.Show(this); //Make sure we're the owner
this.Enabled = false; //Disable ourselves
//Do processing here
this.Enabled = true; //We're done, enable ourselves
f.Close(); //Dispose message form
Also, consider using a BackgroundWorker.
create custom form, and write own behavior
Create a custom form set it to minimal styles etc. Make sure it knows when the process is complete, say by passing in a reference to the process and checking it every now and then.
One way you could do it is create a worker class that raises an event when it is finished.
execute the worker class in a new thead so it runs it the backgroud.
create the modal form with a method to close it as the event handler for the "finished" event.
This way the ui still works and responds and the form will close once the worker thread is finished.
I have datagrid, when press update button I made popup window. I want to pause update process while user fills popup window.
You should start the Update only when you have all required info, not before the user fills in data in the popup window.
the first phase would be data capturing from user input, then you validate the data are correct, valid and sufficient and only then you start the update process whatever it means to you (you have given too few details...)
You can do it with Javascript. First you should capture the event and return false. This prevents a postback. When the dialog is completely filled out and closed you can send the information to the server via as ajax call or trigger a traditional post.
Using C#: I have a class "MsgBox", a special Message Box formatted to my application. It runs on an instance, the point was so that when I called: 'new MsgBox("Message Text");' it would pause the calling thread until the form gets a response/closes or whatever. I tried running the Form on another thread and using Thread.Suspend()/Resume() and it freezes the whole computer for a two or three seconds, then works fine. Same with Thread.Join(). Is there a more efficient way to hold the thread?
You can use Form.ShowDialog method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed.
The dialog box can be assigned one of the values of the DialogResult enumeration by assigning it to the DialogResult property of a Button on the form or by setting the DialogResult property of the form in code. This value is then returned by this method. You can use this return value to determine how to process the actions that occurred in the dialog box. For example, if the dialog box was closed and returned the DialogResult.Cancel value through this method, you could prevent code following the call to ShowDialog from executing.
I'm searching on everywhere but just couldn't find a good example or text about this subject.
I would like to check for example the username and password validity when a user presses the OK button in a dialog.
Should I do this in the closing event, and cancel the dialog close if the validation fails? Or set the DialogResult to none instead of OK. These all seem kinda the wrong way to do it. I also saw the Validated and Validating events but aren't those for only validating a single control for valid input?
How can I check more controls together when the OK button is pressed, and cancel the form closing?
It depends on what you are trying to do. If you want to verify that the user entered something that could possibly be a valid username/password, you could use the Validating events (e.g. make sure it is long enough, etc). If you want to verify that the username/password corresponds to a valid account, then you have to wait until they hit the OK button and check the credentials. If they are bad then you can throw up a message box (or whatever) and prevent the dialog from closing (DialogResult.None).
Each control offers Validating event. In this event you can implement validation of a single control. By default this validation is triggered when control lose focus. In contrast to Validated event, handler of this event receives CancelEventArgs so if validation fails you can cancel current operation (losing focus).
When you want to deal with complex validations you can set AutoValidate property of your form to AutoValidate.Disable. This will disable implicit validation (default behavior described before). Instead you will have to call ValidateChildren to trigger explicit validation of all child controls.