Control.OnCreateControl cannot judge DesignMode when used in a UserControl - c#

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

Related

Windows Forms Designer, Rationale behind Design > GenerateMember

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.

How to restrict control from initializing

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.

Telerik Tab Control - Only the currently SelectedPage shows controls visibility correctly

I'm using the 2013 Q3 Telerik Tab control in a C# WinForms project. If I test the .Visible property of a control placed on a Page in the Tab then it will always return false unless I Select the page. Is there another property besides .Visible that can be used to test the Visibility of a control on a Page without having to select it?
Here's my dilatation:
You should not change the native meaning of the control's properties. Period. I asked in a comment what were you trying to achieve, because I suspected that you're basing some UI logic on some control's visibility, which in my opinion isn't too good approach. The visibility should be bound to the background logic, not the oposite. You're hiding controls for some reason, because at some point something happened - so keep track of that "something" instead of inspecting its results. IMO the Visibility property should be set, but never checked.
Alternatively if it's not possible to change the concept for some reason, as a workaround I'd attach a handler to the VisibleChanged event and set Enabled property if the sender is not a tab control - then by checking against Enabled property you'd know whether the control is in use or not. I find it difficult to imagine a situation in which I'd need to check whether a control is visible.

WinForms designer: Add control and make it visible for the designer

I have a WinForms user control Host with a custom UI Editor.
Through that editor, a child control (Child) can be added to Host.
(The UI Editor creates Child and sets Child.Parent = Host)
Child is handled through a Holder<Child> helper class, which is set as Tag property of e.g. a ListViewItem.
The respective code - some of it, at least - gets added to the form: Holder is created, and set as Tag, which is enough to be created at runtime, too.
However, Child is not visible to the designer - it is displayed, but it can't be selected, nor does it occur in the drop down list with controls for the parent form.
I would like to:
see the Child control in the designer, so that I can modify properties
get notified if the control is removed
Is this possible?
[edit] Thanks all for your input. I've decided to skip the designer - I hoped to throw together something quickly, but apparently it requires more planning than I should allow myself to spend on it right now.
Usethis.Controls.Add(/*Instance of the child*/); on the host class. Then for the notification add event handler for the host's ControlRemoved event (this.ControlRemoved += new ControlEventHandler(Host_ControlRemoved);).
I can't say I fully understand exactly what you are trying to do.
If you are dealing with the problem of how a "child" Control of a UserControl placed on a Form at Design-Time can be made to function as a container onto which you can drag-and-drop other controls from the Toolbox : this CodeProject article by Henry Minute may be helpful : Designing Nested Controls. For example : you have a UserControl with a Panel inside it : an instance of the UserControl is placed on a Form : in the Design-time view of the Form : you want to be able to drag-drop controls onto the Panel in the UserControl and have them become child controls of the Panel : Henry's article will show you how to do that.
This from Microsoft : How to make a UserControl object acts as a control container design-time by using Visual C#
Perhaps might also be useful, although it seems like you already have this step accomplished.

public datagridview within user control is "locked" during design time when subclassing

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.

Categories