Good day. How to add user control to another user control without using the Control.Controls property? For example, draw a Button on the MyControl : Control object, that the events of button be available? I want to create MyControl that contains another controls, but the Control.Controls collection must be empty, as in all standard WinForms controls.
When you add control to MyControl, it will be something like
this.Controls.Add(control);
otherwise control is not displayed. So you can not avoid this.
What is the problem to have controls in Controls ? Standard controls are not composed from other controls (there are exceptions though: DataGridView in edit mode and PropertyGrid, maybe more).
However you can use dirty tricks, like:
add control to a parent, wiring up necessary events to MyControl;
mimic control (to example, Label, by outputting text in OnPaint event).
Wouldn't it be much easier to fight with that what has problem with Controls (if there is actually any problem) ?
Related
In my wpf window I want to change part of it (make different controls visible) when combo box selection is changed. Exactly like TabControl works, just with combobox. I know I could just make some controls visible while hiding the others in the c# code behind but I want to find out if there are other -better solutions.
You can use two Grid or GroupBox (or other container type) controls and put appropriate set of controls in each of them. This way you can just visibility of panels to hide the whole set of controls instead of hiding each control directly.
It may sometimes be appropriate to create a user control for each set of controls. However, this can depend on a specific case.
I have created a Windows form using a Tab Control, but it has a header with it. I want to hide it. I am not able to do it using any properties of the Tab Control. Is there any property defined for hiding the tab header for the Tab Control without going through the code?
Use following code to hide the tabs or set these properties in design.
tabControl.Appearance = TabAppearance.FlatButtons;
tabControl.ItemSize = new Size(0, 1);
tabControl.SizeMode = TabSizeMode.Fixed;
You want the tab panels without the feature allowing a user to switch between them, so I suppose you want to create few separate sets of controls to be shown to the user one at a time. You can achieve this in several ways (you can choose one of them if you find it appropriate in your case):
Use several Panel controls instead of several tabs in the TabControl, however, it would be hard to work in the designer, because all the controls would be visible
Use different Forms instead of tabs to keep the layout parts separated. It can be ok, but you may not want to use multiple Forms, so it depends on a specific case.
and finally, the suggested solution:
Encapsulate each set of controls in a UserControl. This allows you to keep each layout separately, so you can easily design each of them without the other controls getting in the way ;). The the code handling each of the layouts would also be separated. Then just drag those controls in the Form and use set their visibilities appropriately to show the one you want.
If none of those suggestions work for you, let me know, so I can look for other possible solutions.
It's more easy as you think, you just drag the panel's window upper, so will be outside of the form.
Use DrawMode: OwnerDrawFixed will hide TabPage header text DrawMode : OwnerDrawFixed
Another way to achieve the same (or similar) is: You can remove tabs from TabControl.TabPages collection and then add the tab you want to show.
During the Form initialization I remove tabs (so into the designer I can easily manage them) and in some control event (as button click) I show the tab the user has to see.
Something like that:
// During form load:
ctrTab.TabPages.Clear();
// ......
// During button click or some other event:
if(rbSend.Checked)
ctrTab.TabPages.Add(pgSend);
else
ctrTab.TabPages.Add(pgReceive);
In this way the user can still see the header tab but just as title of controls group, he can't change/switch the current active tab.
I have a custom control which contains a panel, of which I want to be able to drop controls on in the VS IDE and have those controls actually be a child of the panel (which is a child of my custom control) and not have them added to the form my custom control is on.
Basically what happens when you drop controls on a panel not contained in a custom control.
I've tried to google it, but can't seem to come up with any good results, any help would be appreciated :)
Assuming that you're talking about WinForms I think you'll get what you want if you change your custom control to inherit from ContainerControl rather than UserControl.
If it's WebForms it might be worth looking at ContainerControlDesigner, but I don't know much about that.
I am attempting to create my own custom Autocomplete style dropdown control in c# .net2.0. For speed of development I have built my control as a UserControl but have hit on an issue of doing it this way.
When the custom drawn dropdown gets drawn I have to resize the UserControl area to be able to display the list of options.
Ideally I want to be able to mimic the drodpown list behaviour in that the list of options is drawn 'floating' and is not constrained by the UserControls height and width (nor even the parent forms boundaries). A tooltip is another example of the unconstrained 'floating' that I desire.
The only way I can think of achieving this is to create on the fly a new form with no border or title bar and display this when the popup is required.
Is there a better (but also quick) way of doing this?
TIA
You would need to use a Form or NativeWindow to allow the control to float correctly. To make a form follow the control is easy enough but it is more difficult to implement and handle all of the focusing/hiding issues especially if you need seamless tabbing/key navigation.
You can try creating a control that is based off the ToolStrip Drop Down Button control. I believe that this control has the functionality that you are looking for. I found this reference for creating controls based off the ToolStrip, you might try starting with this.
http://blogs.msdn.com/jfoscoding/attachment/1335869.ashx
I have the same question, but the answer to use a UserControl will not do. I also need to create a control container that I can add other controls to at design time so I can add it to yet another container (Splitter Panel) which is not avaialable to me at design time (plugin architecture). When I make a User Control, it is missing the design time support and all I get are icons when I drop controls onto this surface.
Do I need to add all the design time support myself, or is there something I am missing that has this for me?
You will have to use either a Form or UserControl to accomplish any forms design within the designer.
I have often created controls that I need to manipulate en masse. Start with a UserControl, then add a panel that fills the UserControl. This is your base panel that you will fill with all of your controls. I then save as a duplicate control and simply remove the UserControl and leave the panel as the public UI control which is then instanced. If I am making changes, I can go back to the original UserControl, make changes, add code, etc - rinse and repeat.