This is a migrated thread and some comments may be shown as answers.

RadButton base.OnClick problem

4 Answers 239 Views
Buttons, RadioButton, CheckBox, etc
This is a migrated thread and some comments may be shown as answers.
Hans
Top achievements
Rank 1
Hans asked on 08 Oct 2014, 10:16 AM
When I override Onclick on RadButton, the code first finishes the whole onclick method before firing base.OnClick.
For a standard Button, this works fine.
In the example below, the OnClick will first finish completely before the base.OnClick will be executed, but is should not.
How to fix this ?

public class QButton : RadButton
{
protected override void OnClick(EventArgs e)
{
var form = FindForm();
try
{
// set wait cursor 
form.Cursor = Cursors.WaitCursor;

// execute base click code
base.OnClick(e);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// set arrow cursor
form.Cursor = Cursors.Default;
}
}
}

4 Answers, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 08 Oct 2014, 12:25 PM
Hello Hans,

Thank you for writing.

The Click event won't be fired when you are calling the base OnClick method because this is actually handled by the button's elements. So to properly fire the event you should call the element's PerformClick method:
protected override void OnClick(EventArgs e)
{
    var form = FindForm();
    try
    {
        // set wait cursor
        form.Cursor = Cursors.WaitCursor;
 
        // execute base click code
        this.ButtonElement.PerformClick();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        // set arrow cursor
        form.Cursor = Cursors.Default;
    }
}

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Hans
Top achievements
Rank 1
answered on 08 Oct 2014, 12:37 PM
Yes, perfect.  Thank you.

Hans
0
Hans
Top achievements
Rank 1
answered on 17 Oct 2014, 07:55 AM
Oops.  It turns out that now the click-event-code gets fired twice !!
0
Dimitar
Telerik team
answered on 17 Oct 2014, 12:34 PM
Hello Hans,

Thank you writing back.

This can be avoided by creating a custom element as well. This will allow you to override its OnClick method and use your logic there:
public class MyButton : RadButton
{
    protected override RadButtonElement CreateButtonElement()
    {
        return new MyButtonElement(this);
    }
}
 
public class MyButtonElement : RadButtonElement
{
    RadButton owner;
     
    public MyButtonElement(RadButton owner)
    {
        this.owner = owner;
    }
     
    protected override void OnClick(EventArgs e)
    {
        var form = owner.FindForm();
        try
        {
            // set wait cursor
            form.Cursor = Cursors.WaitCursor;
             
            // execute base click code
            base.OnClick(e);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            // set arrow cursor
            form.Cursor = Cursors.Default;
        }
    }
     
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(RadButtonElement);
        }
    }
}

I hope this helps.

Regards,
Dimitar
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Buttons, RadioButton, CheckBox, etc
Asked by
Hans
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Hans
Top achievements
Rank 1
Share this question
or