How to Access Form Controls From User Control | C# Winforms

Поделиться
HTML-код
  • Опубликовано: 11 дек 2024

Комментарии • 10

  • @kentofuu
    @kentofuu Год назад +1

    What if we want to add a form inside a panel from user control by clicking a button inside user control?
    ((Panel)posForm.Controls.Find("controlPanelCust", true)[0]).Controls.Add(new FormPOS());
    Throws an error "Top Level control cannot be added to a control", but i already set Top Level = false on the FormPOS. So, how to add a form inside a panel on other form based on user button click?

    • @AaricAaiden
      @AaricAaiden  Год назад

      This is how it is done:
      // access panel on a form and add a new form
      var panel = form1.Controls.Find("panel1", true).FirstOrDefault() as Panel;
      Form2 f2= new Form2();
      f2.TopLevel = false;
      f2.Dock = DockStyle.Fill;
      panel.Controls.Add(f2);
      f2.Show();

    • @kentofuu
      @kentofuu Год назад

      I have
      1. User control that named ucCustomer
      2. Form that named FormPOS
      3. Form that named FormPOS_Cars
      Sorry... Why it doesn't work? The panel won't changing.. I have this btnSelect_Click event on my user control, if the button in user control get clicked, I want the form inside the panel (which the panel on the formPOS, not in user control) changing to FormPOS_Cars
      private void btnSelect_Click(object sender, EventArgs e)
      {
      var controlPanel = this.Parent as Panel;
      var posForm = controlPanel.TopLevelControl as Form;
      ((Label)posForm.Controls.Find("lblCust", true)[0]).Text = lblName.Text;
      FormPOS formPOS = new FormPOS();
      var panel = formPOS.Controls.Find("controlPanelCust", true).FirstOrDefault() as Panel;
      FormPOS_Cars carPOS = new FormPOS_Cars();
      carPOS.TopLevel = false;
      carPOS.Dock = DockStyle.Fill;
      panel.Controls.Add(carPOS);
      carPOS.Show();
      }

  • @rubenaguillonsegura3521
    @rubenaguillonsegura3521 2 года назад +1

    Worked great, thanks!

  • @ahad123able
    @ahad123able Год назад +1

    How can we pass label or any texts from form to user controls? Thanks a lot

    • @AaricAaiden
      @AaricAaiden  Год назад

      Constructor Method: Pass data through the constructor of the target form when creating an instance of it.
      // Form1.cs
      private void button1_Click(object sender, EventArgs e)
      {
      string data = textBox1.Text;
      Form2 form2 = new Form2(data); form2.Show();
      }
      // Form2.cs
      public Form2(string data)
      {
      InitializeComponent();
      label1.Text = data;
      }

    • @ahad123able
      @ahad123able Год назад +1

      @@AaricAaiden thank you sir! But i was asking you how to pass to user control not a form , if u can help me

    • @AaricAaiden
      @AaricAaiden  Год назад

      @@ahad123able
      1. In the UserControl, create a public property to store the text:
      public partial class MyUserControl : UserControl
      {
      public string Text { get; set; }
      public MyUserControl()
      {
      InitializeComponent();
      }
      }
      2. In the Form, create an instance of the UserControl and set its "Text" property:
      public partial class MyForm : Form
      {
      public MyForm()
      {
      InitializeComponent();
      MyUserControl myUserControl = new MyUserControl();
      myUserControl.Text = "This is some text";
      this.Controls.Add(myUserControl);
      }
      }
      3. In the UserControl, access the Text property to display the text:
      public partial class MyUserControl : UserControl
      {
      public string Text { get; set; }
      public MyUserControl()
      {
      InitializeComponent();
      label1.Text = Text;
      }
      }

    • @fetrush6383
      @fetrush6383 11 месяцев назад

      hi the code you provided above is not working for me i replace everything as it be but still not working :(@@AaricAaiden