Click here to Skip to main content
15,668,517 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi I like to bind a object proberty with a user control proberty.
I'v try the folowing code, but it works only when i'm in the actual method, not outside the method.

The User Control shall act like a GUI for the Object (diffrent object with same GUI). So if i start the program and load the objectprobertys from an XML file the User control shall show the settings. If I change the value in the user control the Object property shall also be changed. Binding in both directions.

What Im doing wrong?

BR Niklas

What I have tried:

 partial class Form1
        public SignalGenerator SigGen = new SignalGenerator();

        public void InitializeIO()
            ucSigGen.DataBindings.Add("IOType", SigGen, "IOType", true, 
            ucSigGen.DataBindings.Add("Type", SigGen, "Type", true, 
            ucSigGen.DataBindings.Add("Adress", SigGen, "Adress", true, 

            ucSigGen.Header = "SignalGenerator";
//If i enter this row in this method it works
            SigGen.Adress = "10";

//but when I enter it here there binding dont work

        private void btMainStart_Click(object sender, EventArgs e)

            SigGen.Adress= "10";

Updated 22-Nov-21 23:57pm
Maciej Los 23-Nov-21 5:05am    
Does the "btMainStart" is a part (control) of UserControl?
Niklas Arabäck 23-Nov-21 5:07am    
No it's in main form
Maciej Los 23-Nov-21 5:14am    
OK. Why do you want to bind SignGen in main form instead of in UserControl? As to me, you have to be able to set an instance of SignGen to ucSignGen and ucSignGen has to return an instance of SignGen. In other words, when you set SignGen to ucSignGen apropprate controls have to be filled in with proper data.

1 solution

Well, i'd suggest to move data biding into ucSignGen. Imagine, you can easily set/get an object related with UserControl. All you need to do is to create custom property:

 partial class ucSignGen
    private SignalGenerator SigGen = new SignalGenerator();

    public SingGenerator SignGenerator
        get => SignGen;
        set => SignGen = value;


Then in main form:
ucSignGen1.SignalGenerator = AnInstanceOfSignGen;
//user has changed something and clicked accept button, then you need to call an event which will refer changes to main form

That's why you need to define an event - for example: OnSignGen_Changed in ucSignGen control.

//[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public partial class ucSignGen : UserControl
    public delegate void OnSignGen_Changed(object sender, EventArgs e);
    private OnSignGen_Changed SignGenChanged;


    //SignGenChanged will be visible on [Events] of ucSignGen user control
    public event OnSignGen_Changed SignGenChanged
        add { SignGenChanged+= value; }
        remove { SignGenChanged-= value; }


Please, read these articles to find out how to use events to communicate between forms:
Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]

This should be helpful too:
Define an event in controls - Windows Forms .NET Framework | Microsoft Docs[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900