Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I create overridable methods

Status
Not open for further replies.

andegre

MIS
Oct 20, 2005
275
US
I have a custom control at the top of many forms that contains customer information. The customer name is a link label that, when clicked, will open another form. How can I make that linklabel_click event overridable so I can have the overrided method on each of the forms that contains the custom control?

The reason I need this is because if the new form has any data changed, I want to save that data on the original form.

Thanks
 
You are going to want to look into delegates. If you can give me a few more details I can point you in a direction but I am thinking that a static event class would work.

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
You want the link label to do something different based on what form the link label is on? And you want that to be controlled at the form level?

If so, one way would be to have your custom control perform it's functionality based on its own properties and then set these properties at the form level.

For example you could have a property on your custom control called EnableLink and the linklabel_click method do something like:
Code:
if (this.EnableLink)
    OpenCustomerForm();
Then in your form if you want the link to open a customer form set the property to true.
Code:
myCustomCustomerControl.EnableLink = true;

Another way using overridable (virtual) methods mentioned in your post would be to have the linklabel_click method call a virtual method, for each set of functionality have a new custom control derived from the one you have that overrides the virtual method. For example:
Code:
public class MyCustomerControl : UserControl
{
    private void linklabel_click(object sender, EventArgs e)
    {
        OpenCustomerForm();
    } 

    protected virtual void OpenCustomerForm()
    {
        // standard functionality here.
    }
}

Then you would have your individual derived classes:
Code:
public class MyCustomerControlChild : MyCustomerControl
{
    protected overrides void OpenCustomerForm()
    {
        // specific functionality here.
    }
}
 
This sounds great, but my parent form is already inherting another class/form. I read someplace that you can't inherit multiple classes.

Is there another way around this?
 
In your project add a class called "ControlEvents" and add this code:
Code:
    public sealed class ControlEvents
    {
        private static ControlEvents m_instance = new ControlEvents();

        private ControlEvents() { }

        public static ControlEvents Instance
        {
            get { return m_instance; }
        }

        public event EventHandler ControlClicked;

        public void OnControlClicked(object sender, EventArgs e)
        {
            EventHandler tmp = ControlClicked;
            if (tmp != null)
                tmp(sender, e);
        }
    }

In the main form (Form1 in my case) add a button and this code:
Code:
    public partial class Form1 : Form
    {
        private ControlEvents MyControlEvents;

        public Form1()
        {
            InitializeComponent();
            MyControlEvents = ControlEvents.Instance;
            MyControlEvents.ControlClicked += new EventHandler(MyControlEvents_ControlClicked);
        }

        void MyControlEvents_ControlClicked(object sender, EventArgs e)
        {
            MessageBox.Show(sender.ToString() + " Event Fired");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show();
            Form3 frm3 = new Form3();
            frm3.Show();
        }
    }

in a second form (Form2 for this example) add a button and this code:
Code:
    public partial class Form2 : Form
    {
        private ControlEvents PassToMain;
        public Form2()
        {
            InitializeComponent();
            PassToMain = ControlEvents.Instance;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PassToMain.OnControlClicked(this, e);
        }
    }

and a third form (Form3 in this example) add a button and this code:
Code:
    public partial class Form3 : Form
    {
        private ControlEvents PassToMain;
        public Form3()
        {
            InitializeComponent();
            PassToMain = ControlEvents.Instance;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PassToMain.OnControlClicked(this, e);
        }
    }

The static event class (ControlEvents) will pass the events back to the listener form (Main Form in code example Form1)

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Let me know if it works out.

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Quote: "The reason I need this is because if the new form has any data changed, I want to save that data on the original form."

Just re-read your post. By the above quote do you mean this:

1. User is on your form and clicks the link label in your custom control that opens another form.
2. The user may make changes on this new form and save them.
3. The user closes this form.
4. The original form should now refresh and show the up to date information which may have just been altered on the other form.

If this is what you want you should just open the form as modal and do a refresh after. Here's an example of how you could do that:

Code:
interface IRefreshable
{
    void RefreshData();
}

public class MyForm : Form, IRefreshable
{
    public void RefreshData()
    {
        // Reload data on your form here.  If your custom control is on this form this 
        // method will get called automatically when the child form (from clicking the
        // linklabel gets closed.
    }
}
Code:
private void linklabel_click()
{
    if (this.Parent != null && this.Parent is IWin32Window)
    {
        MyCustomerForm form = new MyCustomerForm();
        form.ShowDialog(this.Parent);
    
        if (this.Parent is IRefreshable)
            ((IRefreshable)this.Parent).RefreshData();
    }   
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top