Home > Net >  How to write dynamically generated control group of the same event
How to write dynamically generated control group of the same event

Time:12-14

Dynamically generated 81 textbox, and put them into a control group, the control is called the name of txt1 to txt81, and each is randomly generated when generating a textbox 1-9 of the tag, want this 81 textbox has textchange events, when the event occurs, check the textbox text, if is not equal to the text. The tag, the empty input text,
The question now is: 1. How to make 81 a textbox Shared textchang events,
2. In the Shared textchange event occurs, how to say is textchang event name textbox,

CodePudding user response:

Control controls generated when busy assigned related event handler
Secondary processing method has two parameters, the first is the events of the control object, the second is the event related environmental parameters

CodePudding user response:

All the Control definition has the TextChanged event
 public event EventHandler the TextChanged; 
you can register your own entrust
 EventHandler proc=new EventHandler ((SDRS, arg)=& gt; 
{
If (the SDR is a TextBox TXT & amp; & TXT. Tag!=null & amp; & TXT. Text==TXT. Tag. The ToString ())
{
TXT. Text=string. The Empty;
The Debug. WriteLine ($" I cleaned up the {TXT. Name} ");
}
});

The foreach (var c in yourControls)
C.T extChanged +=proc;

CodePudding user response:

Of course you can also write
 var proc=new EventHandler ((SDRS, arg)=& gt; 
{
If (the SDR is a TextBox TXT & amp; & TXT. Tag!=null & amp; & TXT. Text==TXT. Tag. The ToString ())
{
TXT. Text=string. The Empty;
The Debug. WriteLine ($" I cleaned up the {TXT. Name} ");
}
});

C in yourControls foreach (var. OfType ())
C.T extChanged +=proc;

Use OfType<> Generics to filter control types, to avoid the TextBox type control register,



"Let 81 textbox sharing textchang events," the logic is wrong, event definitions on the specific object type (this is the Control), is a specific object instance (here the textbox instance) is triggered, there is no "sharing", this statement a delegate, which is a process, it can be registered for each Control, when the textbox the TextChanged event occurs, the event object independently, just used to call it a process, but the incoming call process parameters are different, the customer (caller) is also different, so there was not a "share the same event", is only Shared a method instance,
  •  Tags:  
  • C#
  • Related