Resolved Accessing windows element from another class

wlf

Member
Joined
Apr 26, 2020
Messages
12
Programming Experience
Beginner
Hi, I made a cook book app.
It has a flow layout panel on the side and from it you can choose to view different recipes or write a new one.
When you choose an option the main area of the window changes by switching user controls.
The issue is when I choose to write recipe I want it to update the side menu (to show a new recipe after adding it).
The problem is that writing a recipe is in user control which is a different class than the flow layout panel so it cannot add anything to it.
Even if I make the function in the main class and call it from the user control - it wont work. It doesn't even give me an error.
I am losing my mind please someone help.
Form1.cs:
        public void AddRecipeButton(string recipeTitle)
        {
            Console.WriteLine("making a button");  //this shows up
            Button btn = new Button();
            var form = new Form1();
            btn.Text = recipeTitle;
            btn.BackColor = Color.FromArgb(232, 170, 84);
            btn.Padding = new Padding(35, 0, 0, 0);
            btn.Margin = new Padding(-1);
            btn.FlatStyle = FlatStyle.Flat;
            btn.Font = new Font("Calibri", 8);
            btn.TextAlign = ContentAlignment.MiddleLeft;
            btn.FlatAppearance.BorderSize = 0;
            btn.Dock = DockStyle.Top;
            btn.Size = new Size(250, 45);
            panelM.Controls.Add(btn);
        }
UserControl1.cs:
            Form1 form = new Form1();
            form.AddRecipeButton(RecipeTitle.Text);
 
Your code above looks more like WinForms rather WPF. If you are using WPF, use WPF the way it was meant to be used instead of trying to make it work the WinForms way. In WPF, all your control needs to do is add an update the collection of recipes, and anything bound to the collection will update automatically.

Anyway, the problem is that in your UserControl1.cs, you are instantiating a new form instead of referencing the existing form. If you know that your user control exists within the form that you want to update, then you could break object oriented encapsulation and accessing your user control's ParentForm. A more objected oriented approach is to make the form an observer of the user control. That means your user control should expose and event when a recipe is added. The form listens to that event, and then reacts to it when it gets notified that a new recipe has been added.
 
Your code above looks more like WinForms rather WPF. If you are using WPF, use WPF the way it was meant to be used instead of trying to make it work the WinForms way. In WPF, all your control needs to do is add an update the collection of recipes, and anything bound to the collection will update automatically.

Anyway, the problem is that in your UserControl1.cs, you are instantiating a new form instead of referencing the existing form. If you know that your user control exists within the form that you want to update, then you could break object oriented encapsulation and accessing your user control's ParentForm. A more objected oriented approach is to make the form an observer of the user control. That means your user control should expose and event when a recipe is added. The form listens to that event, and then reacts to it when it gets notified that a new recipe has been added.
I am actually using WinForms but I couldn't find a proper place to post the question. I had no idea about using Action and listening for events I only used it in Node.js and had no idea it's a thing elsewhere. Thats so hecking cool thanks! So I make an event in user control and listen for it in (the main) form?
 
Moved to Windows Forms forum.
 
Screenshot_121.jpg


They're not. You need to select the language that suit you. See the highlighted area.
 
O, thanks didn't notice that. Also this method seems incredibly more complicated than it should be. I only want the main form to add a button when my database receives a new entry.
Maybe I'm thinking in wrong categories, in JS this would take me a minute to implement, here I have been diving into forums and documentation for the past week.
 
Part of the issue is the choice of using WinForms. WinForms was at end of life, until Microsoft relented and put it into .NET Core 3.0 due to the mass of legacy WinForms still being used in in-house LOB applications. As I mentioned earlier, this would be easier in WPF.

The other part of the issue is that we are trying to teach you the correct object oriented way of doing this. There are other ways to this that defy good coding practices but will get the job done faster. Think of it this way, there are some things in programming which can be done so much easier if you just did a lot of copy and paste, and used goto's, but in the end, you end up with a pile of crap.
 
Back
Top Bottom