Using C# WinForms Designer on Panel instead of Form? - c#

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.

Related

Create WPF page fragment in Visual Studio 2015 (C#)

I am trying to develop multi-document interface for C# application (that is suprisingly hard, in Delphi it was and still is the simple task for more than two decades!!!) and it seems to me that I should use AvalonDoc framework for this https://avalondock.codeplex.com/
The essence is - all the dynamic documents will be the parts (fragments) of one (main) C# Window and there will be one XAML file - that is require by Avalon.
My intention is to create separate XAML and code-behind file for each document, is it possible to create fragments in Visual Studio 2015? E.g. such code pieces that does not inherit from Window and that can be dynamically inserted into window (e.g. as AvalonDock LayoutDocument's).
I know that VS has notion of controls and components but I am not sure - is it right practice to create entire documents (e.g. invoices, stores) as single components/controls?
Yes you can create UserControls -
They belong to the list of standard template files in Visual Studio.
A UserControl is :
1 A Xaml file for describing content.
You can design it graphically or with code editing.
2 C# or Vb.net file for codebehind = event handlers, data members, extra methods , ...
Both files make a single class during compilation thanks to the partial keyword.
Once the Usercontrol is compiled you can drag and drop it onto the surface of a Window like a standard control (e.g. button).
Usercontrols can also be instanciated through C#/Vb.Net code.
Let me know if I answer correctly - if I am complettely wrong - I Delete, or I complete if needed

Creating a Custom DockPanel Template in designer

I have an application where I'd like to create multiple DockPanels at run-time, but I'd like them to all follow the same template.
I've had some success attempting this dynamically (creating the class in pure code) but the lack of designer features is seriously impeding my ability to make it aesthetically pleasing (it also feels like I'm working against the API, which usually means I'm doing something wrong).
p.s. I'm using DevExpress v13.1 and WinForms on the .NET 4.5 Framework
There are simple steps how to create reusable UI portion in Win Forms and place it into multiple Dock Panels:
Create UserControl (VS menu Project->Add UserControl...) that contains all needed UI stuff via the designer.
Rebuild the solution -> UserControl will appear in the Toolbox.
Drop this UserControl onto specific Dock Panel.
Repeat Step 3 for each Dock Panel. Profit!!!
At runtime, just create this UserControl instances and place it onto runtime-created Dock Panels.

Change Panel Type in Design Mode

I have designed a Form class using a TableLayoutPanel. It is looking how I want it too, but I realized while running the application that it flickers when re-sizing and swapping out panels. So I created a new class, DoubleBufferedTableLayoutPanel using TableLayoutPanel as a base class. I'm wondering if there is an easy way in the Visual Studio 2012 designer to swap out the two panels without having to completely redesign the form again.
You can go into the designer.cs file and change the types manually. Since the new type derives from the old type, it should work flawlessly.

design panel without parent form in Visual Studio

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.

Transforming a c# class into a visual control

I new to c# and windows form programming. Here is the problem i am trying to solve.
I have to write an application that uses an multiple instance of an ActiveX control. Therefore, I dragged as many control as necessary to my Form. Now my problem is that i'd like to add some personalised methods to this activeX. The logical solution I thought was to create my own class derived from that aciveX and add some more members and methods to that class so it would work as desired. Now my problem is that the newly generated class doesn't exists as a control that can be inserted into the form.
So How can a class become a control and then inserted into a form ?
If you look at the Designer.CS file that corresponds to your form, you should see, in the #region Windows Form Designer generated code, the code that was generated when you dragged the ActiveX control onto the form.
This code is similar to what your code needs to look like.
My guess would be that you should wrap this activex into your own control, and add funcionality that lacks into that wrapper.
Pros: you'l have .net control and will be able to extend it and use it anyway you like.
Cons: if you want to access original methods, you'll have to generate pass-through method wrappers.
How:
- create a control class
- drop activex you have onto it
- set activex dock to 'fill'
- either set control to public, create get wrapper for it, or create method wrappers you desire
- compile that and use it on the form.

Categories