design panel without parent form in Visual Studio - c#

Anyone come up with a way that I can design a panel without a form?
On the surface usercontrol doesn't seem the way to go.
Background:
I come from a text editor world and VS is new to me. We did everything with panels instead of forms. So open for learning. Specifically have a base class panel (ExtendedPanel) that defines some basic controls: Cancel, Save, Save and Close. This ExtendedPanel then will be used for ClientExtendedPanel that is tied to a bindingsouce clientBindingSource. This is all tied to my entity framework model. So I will add, edit and delete sql datarows for my Client table. If no changes have happened by Save button will not be enabled. If I make a change but hit cancel it will warn me. I've done all this before but since I left that company I don't have access to the code base and they didn't use VS (text editor only)so it wasn't really transportable anyway.
All that background so I can ask: Is usercontrol the way to go, or is there something that will allow me to visually add controls to a panel like it is a form?

Yes, a UserControl provides a form-like canvas in the designer for you to add other controls (buttons, etc).
You can do this too by inheriting a panel and writing the code to add the buttons and wire their events, etc, but you won't get the designer support.

Related

Can't see a button in designer

I'm on Windows 10, maintaining a C# Desktop application using Visual Studio 2019, putting controls onto a Form (System.Windows.Forms.Form). I'm attempting to change an application; reworking where the controls go etc., so there is code and control design that I want to keep and put into different locations. I am using the visual designer and connecting to code using the control events (as opposed to creating the forms dynamically).
I have Document Outline visible on the left; I cut several groups of controls I intend to paste back later, so all that one particular TableLayoutPanel has in it now is two buttons. But I cannot find those buttons in the designer. I can see them in the Document Outline; they're in a TableLayoutPanel (in a group box in another TableLayoutPanel on a TabPage, etc.). When I click on a control in the Document Outline, the corresponding control is usually highlighted in the View; however, when I click on either of these buttons, nothing is highlighted in the view.
I tried resizing the Form (which determines the sizes of all these panels/tableLayoutPanels set to 'fill'); it's currently at 1367,850; one of the button locations shows as 664,752, but I don't know in what coordinate system that applies. I tried manually setting the location to 50,50 in the properties window, but it won't let me change that there.
These buttons appear at the bottom of the form; they've never appeared there, but I've noticed sometimes the designer has shown dotted-line outlines of them outside the form entirely. They appear in place when the application is run.
I've tried changing between the 100% scaling and the 125% scaling; that didn't help. For two buttons, I suppose I could delete them and recreate them, but there are many, many controls on this UI, and if there's some trick to making things appear I'd like to know about it.
Is there a trick to this that I'm missing?

C# settings form under settings menu

Is there a way to display the projects settings in a form inside the application?
I have added a Menu Item called setting, Now when that is clicked I want a small form to popuo with the applications setings.
Is there a quick way to do this that just displays the settings as you would see them in Visual Studio? Or do I have to manually fetch the settings and display them?
If I need to manually do it. Which type of form must I use so that it will appear inside my main form?
I think you'll manually need to collect them and display them. To display on an area inside your main form you'll want to use a UserControl and place your created UserControl on your main form.
Take a look at PropertyGrid control. It's the one what VS uses itself to edit properties of things (aka settings). Displaying things is pretty easy: add PropertyGrid to a form and assign object to it propertyGrid.SelectedObject. You will be able to see and edit properties of that object (in case of configuration settings this will be a configuration class instance I assume) almost right away! Tuning, adding localization, etc are not trivial tasks, but there are plenty of tutorials.
Otherwise, you could possibly use TableLayoutPanel with 2 columns: labels and respective texboxes (or other edit controls, to example, NumbericUpDown for int values), rows are autosized. But then you will have to add label and textbox for each setting yourself, as well as write some code to implement getting/setting of each configuration setting.

Reusing screens created in compact framework?

I've created a screen in compact framework using the form editor, is there a way I can grab this screen or at least some components of it and use them in multiple places in the compact framework app?
Yes, tcarvin is right, just create a user control and then reuse this in your compact framework application as often as you need.
To start right click in VS on the project name in solution explorer:
In the popup menu click Add and then UserControl. Accept or change the file name for the user control and then you are looking at the empty user control:
You can now resize the user control canvas and then place other controls as buttons, labels, textboxes etc. on it:
You can then also enter code for button events etc. In the example one can add code to use openfiledialog to select a file and the filename will then displayed in the textbox.
When you are ready, you have to build your solution to get an updated control list on the left in visual studio. Back to a window form design view, you can then place your usercontrol:
Is that simple?
You can also build a library with user controls and then reuse your controls in every compact framework project where you reference the library.
~josef
It sounds like you need to look at UserControls. They let you create add one or more controls to a surface (the UserControl), and then you can add that UserControl to as many forms in your application as you want.

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.

Using C# WinForms Designer on Panel instead of Form?

Warning: I'm new to GUI building in C# (come from a long history in Java with Swing and SWT.)
Using VS2008, When I create a new class, and extend System.Windows.Forms.Form, the WinForms Designer works really nicely. However; when I create a new class and extend System.Windows.Forms.Panel, the designer does not work nearly as elegantly - it simply shows some kind of abstract, list-type view of the components contained in the Panel (as opposed to showing the actual layout of the Controls in the Panel.)
Is there a way to get the Designer to work more nicely with a Panel?
Or is there some workaround so that I can build a Form with the designer, and then use it as if it were only a Panel?
What I'm really looking for is some UI 'element' I can build with the designer, and then dynamically (read: programmatically) add/remove that 'element' to a larger UI.
I think what you're looking for is a UserControl. You can inherit directly from that, and you should be able to use the designer to drag and drop stuff on it.
To make it even easier, you can just right click on your project and click Add -> User Control. That will create the .cs file for you as well as a partial .cs file for the designer generated code.

Categories