Show dynamically created controls in Windows Forms designer - c#

Is there a way to see controls which are created via code in designer instantly but not only during execution?

The Windows Forms designer only applies properties contained in the automatically generated file "Form1.Designer.cs" (example filename for "Form1"). If you change properties (e.g. text, color, whatever) or create new controls in your own code, i.e. in "Form1.cs", the designer does not show it.
It is practically impossible because the designer would have to either 1) parse your code or 2) execute it to apply all changes to the controls.
Option 1 does not work because expression evaluation only works when running the code... Which leads us to option 2: Letting the designer running your code to find out dynamically added properties? First of all, automatically running untrusted code is not what you want. Second, there must be a reason that you do these changes dynamically instead of statically in the designer, so showing dynamic changes as WYSIWYG does not even make sense.

The designer can only display controls that exist at design time or show example controls for databound controls. If you think about code that would dynamically at runtime create a textbox or label based on a variable, how would the designer know which one to display in design mode?
If you have specific logic for how you want your dynamically created controls to display in design mode, you would have to create a custom control and implement the design time drawing code. This is mentioned under the Custom Design Experience heading here: http://msdn.microsoft.com/en-us/library/ms171725.aspx

Related

What is the bottom space on the Windows Forms design view?

I'm trying to learn about Windows Forms Application. Now, as I was creating a DataSet, Binding source, Table adapters. I noticed they show up on a bottom section on the design view. My question is: What is that section, and what goes in there?
From MSDN:
Non-visual components added to the Windows Forms Designer are placed on the Component Tray, located below the design surface, so that they are easily accessible without cluttering the visual design space.
This section generally corresponds to nonvisual elements in a Form, but which are stil relevant to the Form, such as a Timer.
Since they're nonvisual, it doesn't make sense to put them in the designer with the visual elements, but they still need to be in the designer for the designer to be useful in setting their values.
The bottom part of the winforms designer is used for non-UI elements. Things that are created / generated just as fields in the class behind, and not added to the actual view.

Pre-defined XAML code for new control that is added in VS designer

Is there a way to automatically generate XAML code when you drop your control from toolbox to designer? I.e. it would create certain objects (nested properties) automatically and user would not have to type the same code every time to set certain (nested) properties. Kind of a template XAML code that is pre-defined by control's developer.
I'm pretty new to WPF so I'm wondering why there aren't any nested properties in the controls that are in Visual Studio's toolbox (button, label etc...)? Are attached properties a cure for this or have they just cut corners when designing WPF. :)
The problem is that my control (WinForms style property hierarchy) has nested properties and if I "internally" in my code create objects for those properties then XAML does not reflect the situation because it does not have any elements that match the current situation (i.e. the objects created in my control's constructor).
Is the only solution to leave all the properties null and let the user create them all? That way it seems to work correctly but user has to write many lines of XAML to reflect the situation in WinForms.
The functionality that you seek is not available via any of the controls in WPF. However, Visual Studio used to have Macros that would enable us to add pre-written sections of code into our pages, but unfortunately, they decided to remove that great functionality. Fortunately, they have introduced Code Snippets as a partial alternative.
Unfortunately again, these Code Snippets don't work in XAML pages, but once again fortunately, there are a few Visual Studio add ins that will enable you to enter pre-written XAML into your pages at the click of a button on the Code Plex website:
XAML Code Snippets addin for Visual Studio 2010
XAML Snippets for Visual Studio
Please try using one of the above add ins to see if they meet your needs.

Visual Studio: Typing code vs. drag and drop from the control menu

I tried creating a gridview by typing code using another block of code on a different page as an example. I found that, when I ran the program and clicked the Edit button on a row, the fields did not change into textboxes for editing. I did have code to handle the Row Editing event.
In an earlier project I found that I had to double-click the Row Editing event in the Properties window for a gridview to create the event handling code. Typing in that code in the separate aspx.cs file did not work.
I am working with ASP.Net / C#.
What is going on with Visual Studio when I do drag and drop from the control menu as opposed to just typing in code? I am assuming it is adding something that I am missing when I am just typing.
Does anyone else have other examples that didn't work when they typed code that I should watch out for?
Even if you did a mighty good job copying the HTML, you likely still missed a few properties. That's because the GridView component comes with a bunch of so-called Design-Time properties that can (only) be configured from the Properties panel in Visual Studio.
I think you'll find that dragging a control from the toolbox onto your web form will set the control's design-time properties to a specific set of default values. This does not happen when you type the HTML by hand.

Which one is better generating controls(labels, textboxes, listview, buttons etc) on runtime or creating layout in c#?

I am working on my final year project, in my project i am using winform c#. My project is some sort of security system including hardware.
In my project I have a lots of controls like panels, textboxes, labels etc. Till now I am using layout and it is getting slower and slower (and flickering more and more) as I am adding more controls in it.
My question is that isn't it better to generate controls when required and destroy them when I don't need them? Will it save memory?
Assuming that you use the VS Windows Forms Designer, it will create the code for you which will be loaded at runtime. There will be no difference if you load the controls in Form.InitializeComponent (as the Designer does) or in any other place where you wrote it yourself. The only thing the designer does is it creates the Form.Designer.cs file for you while you design the form, and you can use those controls the same way you would use the controls you create at runtime yourself.
Loading a lot of controls on a single form is not a good practice. And it will get slower as you add more contols. You should probably consider redesigning the GUI.

How can I add my own code along with the code generated by the form designer?

When I am working in Visual Studio, and use the designer, VS manages the code for creating and positioning the controls.
But what if I want to add my own code there?
Say I want to use a string variable as the name for my form, or for the default text in a box? I know I can set this in the form_load function, but what if I want to do it in the designer code page?
When the designer generates this code, it is arranged in such a way that the designer can add to it. How can I add my own code, or manage parts of the code, without interfering with the functionality of the designer?
Would it work if I moved parts of the code to a different file? How can I do that?
Basically, I want to Have My Designer and Code it Too!
Visual Studio should create the designer as a "partial" class. Just create another file that is also another part of the same "partial" class. It will be separate file, but same class. Also, the autogenerator won't overwrite your code this way.
Right click on the form and click on 'View Code'. Adding your code there will not interfere with the designer don't worry!
To change the name or any property of a textbox (or any control) simply click on the textbox in the Form/Designer and change any of its property in the properties box, usually at the bottom right of the screen.

Categories

Resources