Passing data from UserControls to WinForm

Reaper

New member
Joined
Jan 20, 2021
Messages
3
Programming Experience
Beginner
I want to display the generated data from the usercontrol to show inside of the textbox from the Winfrom (Form1).
The data is getting parsed from the usercontrol to the Form1 but i cant get the textbox (tb_config) to receive the parsed data.


(Sry for bad english)



Ok2zffW.png


I used the Console to check if the text is parsed from the usercontrol. (Winform)

vw9mXFw.png



Code inside of the usercontrol.

8h4QXnT.png
 
Solution
As for the issue, the problem is that you are creating a new instance of Form1 and operating on that. That will have no effect on the instance that is already displayed and contains the user control and the new instance is discarded immediately. If you want to do anything useful, you need to operate on the existing form not a new one.

You could get access to that form by calling the user control's FindForm method. That would return a Form reference and you could cast it as the appropriate type and then do whatever you want with it.

That is a dodgy way to do it though. The user control shouldn't know anything specific about its parent form other than it's a form. The proper way to do what you want to achieve is for...
Firstly, please don't post pictures of code. Copy the code from VS and paste it into your post as text, formatted as code. If we want to execute your code, should we really have to write it out ourselves because we can't copy it from your post?

Secondly, there's no need to declare your own delegates these days. Just use the appropriate Action or Func. For a void method with one string parameter, you would use an Action<string>. You're already doing that in the first snippet so why not do it in the second snippet?

Thirdly, why are you creating a delegate at all in that second snippet? You're not passing it anywhere so why can't you just call the method directly?
 
As for the issue, the problem is that you are creating a new instance of Form1 and operating on that. That will have no effect on the instance that is already displayed and contains the user control and the new instance is discarded immediately. If you want to do anything useful, you need to operate on the existing form not a new one.

You could get access to that form by calling the user control's FindForm method. That would return a Form reference and you could cast it as the appropriate type and then do whatever you want with it.

That is a dodgy way to do it though. The user control shouldn't know anything specific about its parent form other than it's a form. The proper way to do what you want to achieve is for the user control to raise an event and for the form to handle it. The data can be passed via a custom EventArgs object or the form can retrieve the data from a property of the user control. It can then call it own method. If this happens in response to the click of a Button then there's no need to worry about which thread it happens on.
 
Solution
Using the standard EventArgs:
User Control:
using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public string TextBox1Text => textBox1.Text;

        public event EventHandler Button1Clicked;

        private void button1_Click(object sender, EventArgs e)
        {
            OnButton1Clicked(EventArgs.Empty);
        }

        protected virtual void OnButton1Clicked(EventArgs e)
        {
            Button1Clicked?.Invoke(this, e);
        }
    }
}
Form:
using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void userControl11_Button1Clicked(object sender, EventArgs e)
        {
            MessageBox.Show(userControl11.TextBox1Text);
        }
    }
}
The data is exposed via a property and the form pulls the data from that when the event is raised.

Using a custom EventArgs:
EventArgs:
using System;

namespace WindowsFormsApp1
{
    public class CustomEventArgs : EventArgs
    {
        public string Text { get; }

        public CustomEventArgs(string text)
        {
            Text = text;
        }
    }
}
User Control:
using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public event EventHandler<CustomEventArgs> Button1Clicked;

        private void button1_Click(object sender, EventArgs e)
        {
            OnButton1Clicked(new CustomEventArgs(textBox1.Text));
        }

        protected virtual void OnButton1Clicked(CustomEventArgs e)
        {
            Button1Clicked?.Invoke(this, e);
        }
    }
}
Form:
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void userControl11_Button1Clicked(object sender, CustomEventArgs e)
        {
            MessageBox.Show(e.Text);
        }
    }
}
The data is exposed via the event and the user control pushes it when it raises the event.
 
Show us your code.
 
I tried your method but it wont even show the messagebox.
Then you did it wrong. I tested both my examples and they both work so if your code doesn't work then you didn't implement what I exemplified. If you don't show us what you did, we can't tell you what you did wrong.

Also, did you actually debug your code, i.e. set breakpoints, step through the code by line and examine the state at each step? If not then you haven't really tried to diagnose the problem. You know what the code is supposed to do at each line so you need to watch it as it executes and see exactly where and how its actual behaviour differs from your expectation. Even if you still can't work out the solution, at least you can tell us more than "it does work". You can be specific and tell us what you expected, what actually happened and what line it happened on. If you don't know how to debug, stop what you're doing, learn how, then come back and debug this code.
 
By the way, did you actually try creating a new project and using my exact code? You should have. If you had done that then you could have debugged that code and watched exactly how it executed to gain a full understanding, then applied the same principles to your own project. Many people want to take shortcuts and implement things in their project that they don't understand, thus they can't understand why they don't work.
 
Back
Top Bottom