In Visual Studio, while working on Windows Forms Applications a certain option can be found in the Form Design View. I'm talking about the GenerateMember option (under the Design section of the Properties Window).
The description of this option is the following:
Indicates if a member variable will be generated for this component.
So, if I create a text box tb1 using the Form Dessigner into my Form f1 and I set this option to false, then I cannot access the control within the code (neither f1.tb1 nor tb1 exists).
There should be a good reason for doing that, but I don't know what this reason could be, that's why I'm asking:
Whats the rationale behind the Design > GenerateMember option?
What things can be achieved setting this option to false that cannot be achieved with it on true?
In What situations I should preffer to hide the control setting GenerateMember to false instead of letting it to have the default value (true)?
Thank you.
When GenerateMember is set to false, instead of having a lot of private fields scoped to the Form class instance, they are created scoped to the InitializeComponent() method - meaning you will no longer have this.btnOk or this.btnCancel accessible to anything within your form.
I've figured it is, for the most part, just a personal preference. By specifying which controls you want directly visible as private fields to the rest of your form class, you can avoid getting lost in a sea of unused fields when modifying your code.
Other than that, there is no real functional difference. You could still access the controls via: this.Controls collection.
Related
I have a control class inherent from ListView and put in a User Control, and want to do something once after constructor, so I override OnCreateControl(). But I found neither Design Mode or LicenseManager.UsageMode can judge design time when the UserControl is put in a Form and view by designer. Is there anyway to work around?
A user control contains a property called DesignMode, but examining just it's value isn't enough. The DesignMode should be evaluated for the control and for the whole parent hierarchy in order to know the status of the application.
You can refer the below link for resolving the design mode of a user control
http://www.codeproject.com/Tips/447319/Resolve-DesignMode-for-a-user-control
I have multiple controls in my User controls like
text box, drowndown, listview , gridview and etc.
I have set some property in usercontrols which set enable and visible property of each control.
like isdropdownvisible, istextboxvisible and etc.
But I want those control which are set visible=false does not get initialized. so that processing.
Or suggest me another method which can enhance page speed
Since part of the initialization itself is the setting of the visible flag, i.e. the system does not know whether a control is visible or not until after it is initialized, I'm afraid what you ask for is not only impossible, but illogical as well.
If you have a problem that some controls have too heavy initializations, that are not needed immediately, you can load them in some dynamic manner, but I could not be more specific, without some example code.
When I add a control to a form through the designer, not all properties of the control appear in the designer code. For example, when I add a ListBox the UseWaitCursor property does not appear in the designer code unless it is set to True. When I change it to False it disappears from the designer code, which makes me think that the properties somehow have defaults and don't appear in the designer code if left at default.
Can someone please help me understand how the designer works and where all this is tracked. The reason I ask is I am currently writing a class that extends a third party ActiveX control which I plan to initialize dynamically at run time. I was going through the designer code (when the third party control is added through the designer) and a lot of its properties do not appear there.
This is done with the [DefaultValue] attribute. The Control.UseWaitCursor property looks similar to this:
[DefaultValue(false)]
public bool UseWaitCursor
{
// etc..
}
So if you leave the value at False in the Properties window then the designer knows that it should not display the value in Bold and that it is not necessary to put the property assignment in the InitializeComponent() method since the default is already good. An ActiveX control will certainly have a lot of properties set at its default value as well.
A Windows Form is a type of Control, and Controls are UI elements that have a Size property along with size-related methods such as OnResize and OnSizeChanged.
But Forms also have OnResizeBegin (and OnResizeEnd), which are not found in Control. Is this omission an oversight, a prescient design decision, or something else entirely?
OnResizeBegin event is raised when form size is changed by user, not by programmaticaly changing Form properties (like Size). Most of controls can't be sized in such way, so it hasn't OnResizeBegin event.
When you add something to a base class like Control that has such a huge number of descendents, it's going to get inherited by everything whether it makes sense or not (e.g. would many programmers care that a radio button is starting to get resized?). Every method, property and event you add makes an API that much more complicated.
Now there are plenty of other examples of things in Control that don't make sense for every child (e.g. a Leave event on a Label control), but that's part of the contradictory morass that is Forms. The attached property system used in WPF is much more elegant.
I have a user control that has among other things a label AND a textbox control. With this class, I set the textbox to have its modifier as "public", so when I subclass THIS control, I can get directly to the properties and such of the textbox in each instance where needed. No problem.
Now, the problem. I do the exact same thing but with a dataGridView control (and some others) within a user control. Set ITs modifier to public with intent to derive this user control downstream. Now, I try to derive this control to a NEW control and can't directly touch the dataGridView and add columns, sizing, etc directly.
I tried reproducing the described behavior and was able to do so with the GUI designer in VS 2008 using .Net 3.5. I suspect you are running into the same issue as this problem
That means to get the desired behavior you may need to implement a custom designer. There is even an example designer given by another person answering the question. Please have a look and see if that helps.