bypassing the button event
-
hi friends, how can i bypass the button event on certain conditions?:confused: like i can in while() loop using break statement.
-
hi friends, how can i bypass the button event on certain conditions?:confused: like i can in while() loop using break statement.
You can attach and detach the eventhandler. If you look at the "Windows Form Designer generated code" you can see how the connection between the event and the code is done. If the code that should be triggered by the event is in button1_Click the code would be as follows: private void AttachEventHandler() { this.button1.Click += new System.EventHandler(button1_Click); } private void DetachEventHandler() { this.button1.Click -= new System.EventHandler(button1_Click); } But why don't you just check the conditions using an if statement in the event code?
-
You can attach and detach the eventhandler. If you look at the "Windows Form Designer generated code" you can see how the connection between the event and the code is done. If the code that should be triggered by the event is in button1_Click the code would be as follows: private void AttachEventHandler() { this.button1.Click += new System.EventHandler(button1_Click); } private void DetachEventHandler() { this.button1.Click -= new System.EventHandler(button1_Click); } But why don't you just check the conditions using an if statement in the event code?
-
thanks for replying i tried using this.button1.Click -= new System.EventHandler(button1_Click); inside the button_click event but the next time i click the button nothing happens.
Ofcourse. My example detach the link between the event and the code. You will have to use the attach function to restore the link. In .NET, the Event - Code relationship is not like in VB6. You can programmaticly set up what code to execute when an event is triggered. You can even add more than one function to be called. I assumed this was what you were trying to do. If this is not what you need, I think you should add an if statement in the button1_Click function that checks the conditions before executing the code.
-
Ofcourse. My example detach the link between the event and the code. You will have to use the attach function to restore the link. In .NET, the Event - Code relationship is not like in VB6. You can programmaticly set up what code to execute when an event is triggered. You can even add more than one function to be called. I assumed this was what you were trying to do. If this is not what you need, I think you should add an if statement in the button1_Click function that checks the conditions before executing the code.
actually i have a main form which calls the input form as modal. there is OK and cancel button on the input form. if the user presses cancel the form closes. if the user presses OK the software checks if all the inputs are correct. in the main form i am using the dialogresult property of the input form. i have set ok as dialogresult of the OK button. so irrespective ofthe validity of the inputs the input form returns the dislog result. this is why i was trying to exit the buttonOk_click event if the inputs were not valid.
-
actually i have a main form which calls the input form as modal. there is OK and cancel button on the input form. if the user presses cancel the form closes. if the user presses OK the software checks if all the inputs are correct. in the main form i am using the dialogresult property of the input form. i have set ok as dialogresult of the OK button. so irrespective ofthe validity of the inputs the input form returns the dislog result. this is why i was trying to exit the buttonOk_click event if the inputs were not valid.
This can be solved very easily, without having to fiddle with button event handlers. In your button event handler you could do something like this:
private void OKButton_Click(object sender, EventArgs e)
{
bool inputsOk = true;
// ... check your inputs and set inputsOk to false if one input
// is invalid
if ( !inputsOk )
this.DialogResult = DialogResult.None;
else
{
this.DialogResult = DialogResult.Ok;
this.Close();
}
}That way the Dialog will stay open unless all inputs are valid. mav
-
This can be solved very easily, without having to fiddle with button event handlers. In your button event handler you could do something like this:
private void OKButton_Click(object sender, EventArgs e)
{
bool inputsOk = true;
// ... check your inputs and set inputsOk to false if one input
// is invalid
if ( !inputsOk )
this.DialogResult = DialogResult.None;
else
{
this.DialogResult = DialogResult.Ok;
this.Close();
}
}That way the Dialog will stay open unless all inputs are valid. mav
i had tried te same thing but it does not work. in the ok button property i have set the dialog result property as none. i have to press the OK button twice to assign the dialog result property to the button
-
i had tried te same thing but it does not work. in the ok button property i have set the dialog result property as none. i have to press the OK button twice to assign the dialog result property to the button
Then you must have some other errors in your code. The
DialogResult
property of yourButton
just determines the value of yourForm
'sDialogResult
property when the button is pressed. At the end of your button event handler, the dialog is closed if theForm
'sDialogResult
property is notDialogResult.None
, simple as that. If you don't want the dialog to close you have to set it'sDialogResult
toDialogResult.None
. So in my example, the call tothis.Close();
is superfluous, but I wanted to show you where the dialog gets closed. mav -
actually i have a main form which calls the input form as modal. there is OK and cancel button on the input form. if the user presses cancel the form closes. if the user presses OK the software checks if all the inputs are correct. in the main form i am using the dialogresult property of the input form. i have set ok as dialogresult of the OK button. so irrespective ofthe validity of the inputs the input form returns the dislog result. this is why i was trying to exit the buttonOk_click event if the inputs were not valid.
U can overcome this by many ways firstly make checks in the main form after the dialog result returned OK and if the validity check fails showdialog the input form again otherwise to keep the main form free from the validity check mechanism check in the button click event ... since a modal dialog is not destroyed until disposed raise a flag that says "Wrong INput" and let main form check this flag and show the main form again ..... or set the dialog result for the form and the button to none and set the dialogresult for the form in the event if the check is alright so that clicking the button doesnt close the form but u do programitically when reqd and that the main for sees only the dialog result for the form .. take a look at this http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskretrievingdialoginformation.asp[^] Pushkar Pathak
-
Then you must have some other errors in your code. The
DialogResult
property of yourButton
just determines the value of yourForm
'sDialogResult
property when the button is pressed. At the end of your button event handler, the dialog is closed if theForm
'sDialogResult
property is notDialogResult.None
, simple as that. If you don't want the dialog to close you have to set it'sDialogResult
toDialogResult.None
. So in my example, the call tothis.Close();
is superfluous, but I wanted to show you where the dialog gets closed. mavSorry my bad ... I think mav.northwind has provided the ideal example although there are ways to do it this example is ideal ... i didnt read it before i wrote the reply Pushkar Pathak