question

ahmedAlie-8738 avatar image
0 Votes"
ahmedAlie-8738 asked JackJJun-MSFT answered

Transfer data from the main form to the UserControl

hi

I have a main form and a UserControl.
I do my own thing, I have a tool flowLayoutPanel1 in main form .

And move the User Control to the tool one by one in the form of rows on the main interface.
Everything is good so far.
The main problem is that I cannot control the tools within the user interface all together, but I can control one only through the user interface.
I want to do the following from the main interface loop on all the elements flowLayoutPanel
And every user control inside the tool, I want to change the background of the deletion button to red if the tot = 0 for each element

2-Second, delete all user control in flowLayoutPanel if the tot = 0

167321-untitled1.png




my try

 //UserControl
 public void changebutoncolor()
         {
             
                 foreach (Control crt in this.Controls)
                 {
    
                 if (crt.lb_tot.Text=="0")
                 {
    
                    crt.bt_del.BackColor = Color.Red;
                       
                 }
             }
                 
         }
    
 //call in main
 UserControl2 usr = new UserControl2();
            usr.changebutoncolor();


dotnet-csharp
untitled1.png (26.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@ahmedAlie-8738,Based on my test, you could try the following code to control your usercontrol.

Code:

UserControl:

  public partial class TestUCL : UserControl
     {
         public TestUCL()
         {
             InitializeComponent();
         }
    
    
         public void changebutoncolor()
         {
    
             foreach (Control crt in this.Controls)
             {
    
                 if (crt.Text == "0"&&crt.Name=="lbltot")
                 {
    
                     btndelete.BackColor = Color.Red;    
    
                 }
             }
    
         }
            
     }

Form1:


   public Form1()
         {
             InitializeComponent();
             foreach (Control item in testUCL1.Controls)
             {
                 if(item.Name=="lbltot")
                 {
                     item.Text="0";
                 }
             }
             foreach (Control item in testUCL2.Controls)
             {
                 if (item.Name == "lbltot")
                 {
                     item.Text = "0";
                 }
             }
             foreach (Control item in testUCL3.Controls)
             {
                 if (item.Name == "lbltot")
                 {
                     item.Text = "60";
                 }
             }
         }
    
         private void btnChange_Click(object sender, EventArgs e)
         {
             testUCL1.changebutoncolor();
             testUCL2.changebutoncolor();
             testUCL3.changebutoncolor();
    
         }
    
         private void btnClear_Click(object sender, EventArgs e)
         {
             foreach (Control item in testUCL1.Controls)
             {
                 if (item.Name == "lbltot" && item.Text == "0")
                 {
                     flowLayoutPanel1.Controls.Remove(testUCL1);
                 }
             }
             foreach (Control item in testUCL2.Controls)
             {
                 if (item.Name == "lbltot" && item.Text == "0")
                 {
                     flowLayoutPanel1.Controls.Remove(testUCL2);
                 }
             }
             foreach (Control item in testUCL3.Controls)
             {
                 if (item.Name == "lbltot" && item.Text == "0")
                 {
                     flowLayoutPanel1.Controls.Remove(testUCL3);
                 }
             }
         }

Result:

167688-1.gif




If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1.gif (33.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.