Passing data from UserControls to WinForm

Christian

Member
Joined
Apr 7, 2021
Messages
8
Programming Experience
1-3
I need your assistance on how to pass values from usercontrol textbox to display on label, on form2 c# window form. Thanks
 
Usercontrol:
using system.Windows.Foerms;

namespace Form
{
   public partial class dep1 : Usercontrol

   public static dep1
   instance;

public dep1()
{
   InitializeComponent();
   instance = this;
}

 private void btn_Click(object sender, EventArgs e)
{
  Form2.instance.name.Text = textbox1.Text;
}

}

// Second window form which I want to //pass my value from usercontrol //textbox and then pass with a button //to form2 to display on label is below

using system.Windows.Forms;

namespace Form
{
   public partial class Form2 : Form

public static successful1 instance;
 public Label name;

public dep1()
{
   InitializeComponent();
   instance = this;

   name = label4
}

}
 
I moved your posts to a new thread instead of hijacking the old thread. Thank you for posting your code in code tags.
 
Did you read the link I recommended in the other thread where you ignored my telling you to start a new thread?

Anyway, here's two links for how to pass data around the right way in WinForms:

 
Did you read the link I recommended in the other thread where you ignored my telling you to start a new thread?

Anyway, here's two links for how to pass data around the right way in WinForms:

This didn't answer my question. Le...form.6405/#post-21438']other thread [/QUOTE]
where you ignored my telling you to start a new thread?

Anyway, here's two links for how to pass data around the right way in WinForms:

This didn't answer my question. Let me be more clear to you.
I have a static usercontrol form on c# window form which includes textbox's and a send button. I tried passing data from this static usercontrol to form3 on c# window form but it fails to appear on my form3 labels. Below is my code
 
The same principles apply whether you are going between forms, between controls, or between controls and forms.
 
Usercontrol:
namespace abc
{
  public partial class usercontrol1 : Usercontrol
 {
   private static usercontrol1 _instance;

  public static usercontrol1
{
  get
    {
      If (_instance == null)
          _instance = new usercontrol1 ();
       return _instance;
    }
  }
   public delegate void SubmitClickedHandler();

  public usercontrol1 ()
   {
     InitializeComponent();
   }
    
  public event SubmitClickedHandler SumbitClicked;

 protected virtual void onSumbitClicked()
{
  If (SubmitClicked != null)
   {
     SubmitClicked();
   }
}

private void btnpress_Click(object sender, EventArgs e)
{
   //In here I also have my MySQL code that should be sent into the database once button is clicked

  onSumbitClicked();
}
Public string name
{
  get { return textbox1.Text; }
  Set { textbox1.Text = value; }

 }

}

}
 
Form3:
namespace abc
{
  public partial class lamp : Form
 {

  public lamp ()
   {
     InitializeComponent();
   }
    

Public string Data
{
  get { return label1.Text; }
  Set { label1.Text = value;  }

 }

}

}
 
This didn't answer my question.
Sure it does. You're just not paying attention. You're hacking your way through this and expecting us to fix your hack. That's not the way to do it. The fact that you have "static" user controls is ample evidence of that. There's no way that you should have a static field referring to a control. You should go back and read my blog posts, understand the principles and then reimplemnent your code from scratch based on those principles. Just do it right in the first place instead of doing it wrong and then wondering why it doesn't work.
 
Back
Top Bottom