C# how to pass variables from form to userControl and viceversa - c#

being a beginner of C # (WinForm) I am making graphical interfaces using a form inside which there is a container panel, in it at runtime I am going to open various user controls, which for me represent the various graphical pages.
First of all I would like to know if this is a good method to manage graphical interfaces, or if there are better ones.
Then I would like to understand what is the best way to pass the data between the main form and the usercontrol pages. I usually instantiate the objects as static, so that I can also see them from the userControls ...
Thanks

Create properties in your user control so as to access the underlying data. I would advise against exposing any nested control in your user control.

Related

WinForms UI design: displaying controls for only currently viewed hierarchy level

I am attempting to create a WinForms application which allows the user to display and edit data stored in a MSSQL database. The data being altered is hierarchical, and within a single level of the hierarchy the properties which may be altered are identical; in other words the controls for a single level of the hierarchy are the same, but they may differ from other levels.
I am trying to create the application in such a way that there is only a single form with controls that update based on the hierarchical level of the item being viewed by the user. I realize this is possible by putting all the controls for all levels on a single form and updating their 'Visible' property, but that method makes design of the form difficult due to clutter... Have any of you found a more elegant/less ugly solution?
If you want to do this in WinForms, you can take advantage of the fact that visibility and enabled-ness are both "heritable" traits in the Windows model.
In other words, if you group all of your controls within a parent container (such a Panel or UserControl), then disable that container control and make it invisible, all of its child controls will also become likewise disabled and invisible.
I recommend creating UserControls for each level of the hierarchy. The line of thinking is pretty much the same as if you used separate Forms, except that they're not actually separate Forms. Multiple UserControl objects can be displayed on a single form, so you can have as many as you need. This keeps all related controls together, which makes management much easier. You can also interact individually with these UserControls in the WinForms designer, just like they were separate Forms, solving the "clutter" problem.
To toggle between "active" hierarchies, loop through all of your UserControl objects. Make the currently "active" one enabled and visible (all of its children will automatically become likewise). Make the rest of them disabled and hidden (and all of their children will automatically become likewise).
I won't argue with HighCore here, though. If you don't already know WinForms, you could just as easily spend your time learning WPF. If you decide to do so and want to know how to accomplish this same task in that UI framework, please be sure to ask a new question.

Winforms GUI controls distribution

I have a winforms tab control which has several tab pages. Within each tab page controls (textboxes, radio buttons, etc...) are group into groupboxes. These group boxes are arranged from top to bottom but in some occassions some groupboxes needs to be visible and other ones to be hidden. Also control within each group box sometimes (depending on the scenario) needs to be visible and sometimes hidden. So I would like to know if someone knows a good approach to do this, maybe some kind of pattern if any. Also it would be good to implement a generic solution to do this. Could any expert in GUI guide me in the right direction to do this?
Using C# and dot NET Framework 4.0, WinForms. This is a desktop application, not Web-based.
You can define the scenarios in a class then add another class that will manage the layout by reading the scenario and laying the elements based on the scenario. I have not provided details because it varies on how specific you want to be and what behavior you want to achieve. It is better to put the Widgets inside a User Control and let that user control communicate with the layout manager.You can use the Mediator pattern or variation of it to coordinate between the widgets. Hope this helps.
I usually try to group related controls into UserControls (even if this means doubling up on some controls) and adding them to or removing them from the form as needed. An example of this could be payment methods - when the user selects a specific payment method (Credit Card, Cash, Cheque, etc) a UserControl with the correct elements is displayed within a panel on the form.
A good pattern to use when managing this sort of set up is Model-View-Presenter, in the example, all the UserControls would probably implement an IPaymentMethod view interface and provide a way to update the corresponding models.

Split a large winform class

I'm using WinForms to design an application. It has many controls (buttons, menus, etc) that have underlying event handlers. I find that my class is getting rather large and hard for one person to navigate. This is due to the fact that each control requires one or more methods to handle events within the context of the window.
What are some techniques for organizing these methods or splitting up such a large class?
If it is just about navigation you can split your class in several files, just add the partial before the class declaration in every file.
Generally if you have some very complex controls or event handlers bound to these controls it can be reasonable to separate those in your own custom user controls and just add them to your form. Just add a new user control element to your project and customize it the way you need.
All your controls are immediately available as any other .Net controls in the same project, but you might also want to put them in a separate DLL file (if you want even more separation of your code).
Here is a nice tutorial how to design your own controls in WinForms.

C# UserControl factory

Let's say you have two classes that extend UserControl. Each of the controls provides a custom event (this could be done by using an interface).
You want to display one of the controls in the odd days and the other in the even days.
You also want to be able to drag&drop (Visual Studio) the UserControl on your form without knowing what the Control type will finally be.
How do you do that ? Is the factory pattern useful here ?
I would make a container control that is added on the form (and that is present in the designer toolbox), that internally uses some factory to create an instance of the actual control to use and then adds it to the container with Dock set to Fill.
You could make a third usercontrol that creates & hosts the usercontrol depending on the day.
But, this has a bad feeling to it, could you explain more in detail what you actually are trying to do?

What are the purpose of User Controls in Visual C#?

User Controls -- Do they serve a special purpose?
As far as I can tell they are no different to forms - they have the same toolbox and features.
Are there certain times when they are appropriate to use over forms?
It would be interesting to understand what they are good for.
You use them to group a set of controls and behaviors together in a re-usable way. You can't show a control on the screen unless it's added to a form somewhere.
One good example is a textbox. It's very common to have a label next to your textboxes. You can build a user control to make this easier. Just drop a label and a textbox on the control, expose whatever your properties you want, setup the new control in your toolbox, and now you can just drop this control on your form instead of needing to arrange a label and a toolbox on the form separately.
You could kind of think of them as a panel which "remembers" what controls you put on it. And there's one more important piece. You can put code in these controls as well, and use that to also build special behaviors into your custom controls.
I have to disagree (slightly) with the selected answer. Reusability is only part of what a UserControl is for.
All Controls are reusable. Almost all controls are reusable on the same Form/Window/Panel/etc. For example, a TextBox is a control.
There are two ways to create your own reusable control:
Custom Control
Completely custom, and reusable.
Created entirely in code.
You get a bit more granular control over what your control is doing this way.
Lighter weight (usually), because there isn't anything added in for designability within Visual Studio.
In ASP.Net only: No "HTML" type file to use or edit.
User Control
Completely custom, and reusable.
Created partially in a designer in Visual Studio, and partially in code. (via code behind)
Much easier to deal with from a visual aspect.
A little heavier, as there is pre-existing code added in by the framework to support designing inside Visual Studio.
In ASP.Net only: You can change the appearance a bit simply by editing the .ascx file (basically HTML).
User Controls serve the purpose of reusing controls.
Imagine you need a search box in several pages of your application. You can create a search user control and drop it in every page where you want it visible.
So, it's nothing more than a container that aggregates reusable blocks for your pages.
Forms have a lot of extra furniture that you don't need if you simply want a collection of controls together - the minimize and maximize buttons for example. If you just simply grouped your controls in a Panel, you'd have all the event handlers on the same form as the panel - with a user control, the event handling code is in the user control class, not the form class.
You can reuse the same control on many forms. In fact all items you are using while creating windows forms are the controls. User controls are just extra controls extending controls library provided by .NET.
In ASP.NET, user controls enable you to split your page into reusable components. For example, you may want to have a search box which can be used in different places on your website, so you'd use a user control. They can also be used for partial page caching. You can cache portions of your page to improve performance.

Categories