Home > Net >  With code generation lable, how to register event for her?
With code generation lable, how to register event for her?

Time:09-30

With code generation lable, how to register event for her? For example:
Label lable=new Label ();//the new lable
For (int I=1; i <100; I++)
{
Lable. Name="lable" + I;//to lable named
Lable. AutoSize=false;
Lable. Location=new Point (2, 66);
Lable. Size=new Size (298, 64);
Lable. TextAlign=ContentAlignment. MiddleCenter;
Lable. Text="haole";
Lable. The Font=new Font (" tahoma ", 15);
Lable. BorderStyle=BorderStyle. Fixed3D;
This. Controls. The Add (lable);//form to create a lable
This. For the Location=new Point (lable. Location. X, lable. The Location, Y + 67);


}
I want to give the new lable I registered click event, how do I register?

CodePudding user response:

Lable. The OnClick +=press Tab twice

CodePudding user response:

I just tried it on and registered the event, but I want to change the color of the lable, found no this control

CodePudding user response:

https://blog.csdn.net/xianfajushi/article/details/83867006

https://blog.csdn.net/xianfajushi/article/details/53067028

CodePudding user response:

refer to the second floor qq_41222758 response:
I just tried it on, and registered the event, but I want to change the color of the lable, found no this control

In the event sender is the source control
(sender as Label) is available

CodePudding user response:

Sender is transformed into the label

CodePudding user response:

Whether need to put the Label lable=new Label (); Inside in the for

CodePudding user response:

In code add events to his ah,,,,
Is XXX. Click +=you define your own events

CodePudding user response:

Is a time when you drag and drop automatically increase the code by hand knock out

CodePudding user response:

https://blog.csdn.net/wsp2359482671/article/details/78679110

CodePudding user response:

There is something wrong with the code

First of all,

 
The Label Label=new Label ();
For (int I=1; i <100; I++)
{
Label. XXXX
}

You this way. The final page must be only one (last) label, according to override all other away?
Because from beginning to end only a label..

Secondly to label events can be used in a label. Click +=2 times the TAB to generate an event

Or

 
Label. Click +=(sender, event)=& gt; {
//your code
};


, of course, you click a label is executed when the label events so you need to convert the sender to label

 
Label. Click +=(sender, event)=& gt; {
Var lb=sender as Label;
//lb in properties for loop assignment is you...
};

CodePudding user response:

TO: love, a little, I use the code dynamically generated multiple lable, what is his name attribute? How do I like lable1, lable2 calls generated lable like that? To help solve, give score!

CodePudding user response:

The name is a control of a unique identifier to not repeat to you... No actual meaning

With fixed name + number.. Because you can cycle findcontrol

CodePudding user response:

11 references qq_41222758 response:
, love, and one more thing, I use the code dynamically generated multiple lable, what is his name attribute? How do I like lable1, lable2 calls generated lable like that? To help solve, give score!

Lablel code generation is basically can't use his name call, for him to only one list
  •  Tags:  
  • C#
  • Related