Hi folks,

I am working on a Windows Form project that all things in it are dynamically created during run time.

So all controls are created during running application and also events are assigned to controls. Also I have to mention all controls are user defined controls.

All event functions are already written and during run time just I assign them to proper control event handler as usual:

sampleButton.Click += new System.EventHandler(vExitApplication);

My problem is I cant recognize the result of assigned function within control. Is there any method to understand what is that function result for example is there any exception or not?

Thanks

What, exactly, do you want to have happen. Does the method run on that event? Is it supposed to change the state of the control? Does it change the state of the control? Does the control reflect the changed state?

Most 'Events' don't return a value.
If they do, then it is via the EventArgs object.
E.g. Cancel property of a CancelEventArgs object.

Exception handling should be done in the called routine. (i.e. in vExitApplication)

If this does not answer your question then please explain better what you mean by:
"I cant recognize the result of assigned function within control."

What, exactly, do you want to have happen. Does the method run on that event? Is it supposed to change the state of the control? Does it change the state of the control? Does the control reflect the changed state?

I assigned a method to (for example) Click event handler then I want to know what happen in that method. I overrode the OnClick and want to change the state of control if method is succeeded. if not the state must not be changed.

protected override void OnClick(EventArgs xEventArgs)
{
base.OnClick(xEventArgs);
if( bOkTheAction==true)
{
ChangeStateOfControl();
}
}

Most 'Events' don't return a value.
If they do, then it is via the EventArgs object.
E.g. Cancel property of a CancelEventArgs object.

Exception handling should be done in the called routine. (i.e. in vExitApplication)

If this does not answer your question then please explain better what you mean by:
"I cant recognize the result of assigned function within control."

I have access to EventArgs in method but I want to know what happen in method in control.

I assign methods (function I am old C programmer so sometimes I am using the old worlds :) ) to event handler of control and want to know what happen in method but in control not outside of the controls. Because all these methods are defined outside of the controls and all of them can use the same methods if conditions is proper.

What determines success of the method?
If it is exceptions then just put a try-catch around the base.OnClick(..).

Something like this:

bool callOK = true;
try 
{ 
	base.OnClick(xEventArgs);
}
catch
{
	callOK = false;
}
if (callOK)
	ChangeStateOfControl();

What determines success of the method?
If it is exceptions then just put a try-catch around the base.OnClick(..).

Something like this:

bool callOK = true;
try 
{ 
	base.OnClick(xEventArgs);
}
catch
{
	callOK = false;
}
if (callOK)
	ChangeStateOfControl();

Thank you. I can throw an exception in method. But is there any other way?

As it seems you have control of both the called method and the UserControl override that calls the method you can choose where to do the error handling.

Remember that when a handler is called the Object sender parameter is the control that raised the event, so you could update the control state in the handler routine rather than in the controls event override.

private void textBox_TextChanged(object sender, EventArgs e)
{
	var textbox = sender as TextBox;
	if (textbox == null)
		return;
	// do something with textbox.
}

Or you could pass your own event args object (derived from the EventArgs object required for the handler).

// Your own custom EventArgs class (must derive from the class needed for the event called)
public class MyTextEventArgs : EventArgs
{
	public bool OK { get; set; }
}

//Event override in UserControl
protected override void OnTextChanged(EventArgs e)
{
	var args = new MyTextEventArgs();
	base.OnTextChanged(args);
	if (args.OK)
	{
		// do something
	}
}

// Event handler function that returns a value using custom EventArgs
private void textBox_TextChanged(object sender, EventArgs e)
{
	var args = e as MyTextEventArgs;
	if (args == null)
		return;
	args.OK = true; // or false
}

As it seems you have control of both the called method and the UserControl override that calls the method you can choose where to do the error handling.

Remember that when a handler is called the Object sender parameter is the control that raised the event, so you could update the control state in the handler routine rather than in the controls event override.

private void textBox_TextChanged(object sender, EventArgs e)
{
	var textbox = sender as TextBox;
	if (textbox == null)
		return;
	// do something with textbox.
}

Or you could pass your own event args object (derived from the EventArgs object required for the handler).

// Your own custom EventArgs class (must derive from the class needed for the event called)
public class MyTextEventArgs : EventArgs
{
	public bool OK { get; set; }
}

//Event override in UserControl
protected override void OnTextChanged(EventArgs e)
{
	var args = new MyTextEventArgs();
	base.OnTextChanged(args);
	if (args.OK)
	{
		// do something
	}
}

// Event handler function that returns a value using custom EventArgs
private void textBox_TextChanged(object sender, EventArgs e)
{
	var args = e as MyTextEventArgs;
	if (args == null)
		return;
	args.OK = true; // or false
}

Thanks a lot dude ;)

This is what I am looking for ? :)

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.