Home > Net >  The for loop used in the c # winform dynamically add label control how from another function call an
The for loop used in the c # winform dynamically add label control how from another function call an

Time:11-25

If inscribe
Private void btn_st [/code] art_Click (object sender, EventArgs e)
{
UI_unvisible ();
DrawGame ();
AddLabel ();
}

Private void DrawGame ()
{
Graphics g=CreateGraphics ();
Pen Pen=new Pen (Color DarkOrange, 3);
A Rectangle rMax=new Rectangle (195, 195, 210, 210);
A Rectangle [] r=new Rectangle [16].
for (int i=0; I & lt; 16. I++)
{
R [I]. X=205 + (I % 4) * 50.
R [I] Y=205 + (I/4) * 50.
R [I]. Width=40;
R [I]. Height=40;
G.D rawRectangle (pen, r [I]);
}
G.D rawRectangle (pen, rMax);

}

Public void AddLabel ()
{
for (int i=0; I & lt; 16. I++)
{
The Label l=new Label ();
L.N ame="labNum_" + i.T oString ();
L.F ont=new Font (" black ", 15);
L.T ext="2";
L.L ocation=new Point (215 + (I % 4) * 50, 215 + (I/4) * 50);
L.S considering=new Size (20, 20).
This. Controls. The Add (l);
}
}
How to add another function call control and modify properties of l l

CodePudding user response:

L.N ame="labNum_" + i.T oString ();
In this case the
You can use the

Label=l (form. Controls [" labNum_ digital "] as Label).
L.T ext=XXX;

CodePudding user response:

If it is the individual controls, # 1, if it is full, it can be:
 
This. Controls. OfType

CodePudding user response:

I can in other functions such as DrawGame () used in labNum_ [I] Text="xx"; Modify its properties?

CodePudding user response:

reference cw_dream reply: 3/f
I can in other functions such as DrawGame () used in labNum_ [I] Text="xx"; Modify its properties?


As long as it is in the same class can, create label is private, reference is less than in the other classes!

CodePudding user response:

reference 4 floor wxstar8 response:
Quote: reference cw_dream reply: 3/f

I can in other functions such as DrawGame () used in labNum_ [I] Text="xx"; Modify its properties?


As long as it is in the same class can, create label is private, reference is less than in the other classes!

O the results don't let me reference?

CodePudding user response:

You wrote, system to judge the current does not exist the labNum_3 this control, error,
You can check for label Controls, find out the type and order for the third, to modify it

CodePudding user response:

refer to 6th floor wxstar8 response:
you wrote, system to judge the current does not exist the labNum_3 this control, error,
You can check for label Controls, find out the type and order for the third, to modify it

I suddenly thought of using generic methods to save control as a generic type but how to output in the form of generic controls,,
Thank god to guide my new one

CodePudding user response:

reference 5 floor cw_dream reply:
Quote: refer to 4th floor wxstar8 response:

Quote: refer to the third floor cw_dream response:

I can in other functions such as DrawGame () used in labNum_ [I] Text="xx"; Modify its properties?


As long as it is in the same class can, create label is private, reference is less than in the other classes!

O the results don't let me reference?

All the code I am writing to you, but you still use your own code

CodePudding user response:

((Label) enclosing Controls [" lblNum_3] "). The Text="1";

CodePudding user response:


CodePudding user response:

I also to learn, is really more a great god,

CodePudding user response:

The Label Label=container control. Controls [] "(name)" as the Label;
Can do everything

CodePudding user response:

. Try to use the label tag to store a serial number, read the serial number of the label to change content,
 public void AddLabel () 
{
for (int i=0; I & lt; 16. I++)
{
The Label l=new Label ();
L.N ame="labNum_" + i.T oString ();
L.F ont=new Font (" black ", 15);
L.T ext="2";
L.t ag=I;
L.L ocation=new Point (215 + (I % 4) * 50, 215 + (I/4) * 50);
L.S considering=new Size (20, 20).
This. Controls. The Add (l);
}
}
Private bool changeLabText (string num, string TXT)
{
Foreach (c in this control. Controls)
{
If (c.t ag==num)
{
((Label), c). The text=TXT;
return true;
}
}
return false;
}

  •  Tags:  
  • C#
  • Related