开发者

How do I create a custom C# control that has some common functionality?

I am very new to C# programming. I come from autoit, and other scripting languages, the transition has been most difficult. Anyway I am working on a control in a windows form, basically I want it to be a LinkLabel control, that when you click it, it will become a textbox, once you enter your name, and either hit enter, or tab, it will set your name as the linklabel. But, I will have 10 of these controls on a form, and the way I have done it, it has taken three methods per control, so that is a lot of code, I'm sure I'm just doing it wrong, but here is what i have:

namespace Program
{
    public partial class formMain : Form
    {
   开发者_如何转开发     public formMain()
        {
            InitializeComponent();
        }

        private void linkLabelPlayerName1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.linkLabelPlayerName1.Hide();

            this.textBoxPlayerName1.Show();
            this.textBoxPlayerName1.Focus();
            this.textBoxPlayerName1.KeyPress += new KeyPressEventHandler(textBoxPlayerName1_KeyPress);
            this.textBoxPlayerName1.LostFocus += new EventHandler(textBoxPlayerName1_LostFocus);
        }

        private void textBoxPlayerName1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                this.linkLabelPlayerName1.Text = this.textBoxPlayerName1.Text;
                this.textBoxPlayerName1.Hide();
                this.linkLabelPlayerName1.Show();
            }
        }

        private void textBoxPlayerName1_LostFocus(object sender, EventArgs e)
        {
            this.linkLabelPlayerName1.Text = this.textBoxPlayerName1.Text;
            this.textBoxPlayerName1.Hide();
            this.linkLabelPlayerName1.Show();
        }
    }
}

I'm sure there is a way to use the last two methods between all 10 controls, so they don't have to be rewritten for each control. That is, textBoxPlayerName1_LostFocus() and textBoxPlayerName2_LostFocus().


Welcome to object orientated programming :).

You should created a derived class to encapsulate the functionality. For example:

class EditableText : UserControl
{
    private LinkLabel lblName;
    private TextBox txtName;

    public EditableText()
    {
        // Construct objects, attach events and add them
        // as children to this object
    }

    // Return the text of encapsulated TextBox
    public string Text
    {
       get { return txtName.Text; }
    }
}

Now you are able re-use this class in different areas, that's what object orientated programing all about!


  1. Right-click on the windows forms application in the Solution Exporer, and select Add, then User Control...
  2. Type in a name for the new control, like LinkLabelTextBox

This will give you a space to work in that looks like a Form, but without borders. This is your new control. Put your LinkLable and TextBox on this new control exactly as you put them in the window, and give them the functionality that you want. Then replace all your existing controls with instances of this new control. You will create 10 of these, instead of creating ten LinkLabels and ten TextBoxes. And all of the functionality that you want will be built-in to your new control, so that code does not need to be repeated.

Instead of a linkLabelPlayerName1 and textBoxPlayerName1, you will have a linkLabelTextBoxPlayerName1, and none of the Show, Hide, Focus stuff will clutter your form code.

Also, be sure to include a public Text property so you can get the value that the user typed out of this control.


Create your own Control with the functionality.


Your code is correct.

To make it cleaner, you should move it to a separate UserControl, and set the Text property to the textbox's value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜