Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I Customize a TextBox Control in my form by define methods in mousedown ,keydown,TextChanged and ....
i need other same controls in other forms act same behavior and
write this codes in events once for all other controls
How do this?
Posted
Comments
DamithSL 8-Nov-14 1:52am    
What you mean by all other controls? ( all TextBox Controls?)
Hekmat90 8-Nov-14 2:19am    
yes other TextBox controls
Sergey Alexandrovich Kryukov 8-Nov-14 1:52am    
And how can it be a problem? If some handlers (or part of handler's code) should do the same thing for different controls, write it as a function and use in different controls.
—SA

1.If you want to reuse the same/similar TextBox controls in others forms you should define and implement the common behaviors into a base class, named for example BasicTextBox and derived from TextBox, then use this class or children of it (classes derived from it) in your forms/controls in place of the system TextBox objects.

2.This idea, explained above, could be extended also for other controls, by using similar steps.

3.Because your BasicTextBox class will extend the system TextBox control, it will inherit all its methods and properties. In order to add your custom behavior in any event you have to overwrite it in your BasicTextBox class like in the next example:
C#
protected override void OnClick(EventArgs e)
{
    //Add your custom behavior here:
    //this.SelectionStart = 0;
    //this.SelectionLength = this.Text.Lenght;
    this.SelectAll();
    //
    // You must invoke the existing functionality from parent!
    base.OnClick(e);
}
 
Share this answer
 
v3
Comments
Hekmat90 8-Nov-14 2:28am    
thnak's ,suppose i have textbox and write This code in click event:
textBox1.SelectionStart = 0;
textBox1.SelectionLength = textBox1.Text.Lenght;
and i want other text box inherit this behavior from above text box.
by example describe me how do this?
Raul Iloc 9-Nov-14 2:47am    
See the update (3rd point) in my solution above!
you can create custom textbox control and use it all the places in your project
check below article :Writing your Custom Control: step by step[^]
 
Share this answer
 
If you select several Controls at design-time in Visual Studio, and use the context-menu, or the F4 key, to bring up the Property Browser, then select 'Events:

You'll see the list of Events that all selected Controls share.

You can double-click one of those Events, and Visual Studio will create a "stub" of an EventHandler for you with the required signature (object sender, SomeKindOfEventArgs e).

Or, you could write the EventHandler in code, then select the Controls, then open the Property Browser, then choose the EventHandler you wrote to bind to the selected Controls.

You do this with Controls inside different Container Controls, like one Control on the Form "surface," another Control inside a Panel inside another Panel, etc.

Or, in your code, you can assign multiple Controls to use the same EventHandler: what if I want to record an action every time any Button in a Panel in a Form is clicked ? And, let's imagine I have five Buttons, button1~button5 in 'panel1:
C#
private void Form1_Load(object sender, EventArgs e)
{
    foreach (Button aButton in panel1.Controls.OfType<Button>())
    {
        aButton.MouseClick += AButtonOnMouseClick;
    }
}

private void AButtonOnMouseClick(object sender, MouseEventArgs mouseEventArgs)
{
    Button theButton = sender as Button;

    // do something here with every Button ?
    // ????

    // do something here with specific Button(s) 
    switch (theButton.Name)
    {
        case "button1":
            MessageBox.Show("button 1");
            break;
        case "button2":
        case "button3":
            MessageBox.Show("button 2, or 3");
            break;
        default:
            MessageBox.Show("any button not 1,2,3");
            break;
    }
}
What if I wanted to use the same EventHandler for both the Buttons, and two Labels in 'panel1:
C#
private void Form1_Load(object sender, EventArgs e)
{

    foreach (Button aButton in panel1.Controls.OfType<Button>())
    {
        aButton.MouseClick += SomeControlOnMouseClick;
    }

    foreach (Label aLabel in panel1.Controls.OfType<Label>())
    {
        aLabel.MouseClick += SomeControlOnMouseClick;
    }
}

private void SomeControlOnMouseClick(object sender, MouseEventArgs mouseEventArgs)
{
    if (sender is Label) handleLabel(sender as Label);
    if (sender is Button) handleButton(sender as Button);
}

private void handleLabel(Label theLabel)
{
    MessageBox.Show("clicked on Label: " + theLabel.Name);
}

private void handleButton(Button theButton)
{
    // do something here with every Button ?
    // ????

    // do something here with specific Button(s) 
    switch (theButton.Name)
    {
        case "button1":
            MessageBox.Show("button 1");
            break;
        case "button2":
        case "button3":
            MessageBox.Show("button 2, or 3");
            break;
        default:
            MessageBox.Show("any button not 1,2,3");
            break;
    }
}
Note there are any number of ways you could structure this kind of code: my preference would be to define one EventHandler for each Control Type that I wanted to share an EventHandler: I feel that created more maintainable code, as well as avoiding having to test for the 'Type of the Control in order to decide which code to execute.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900