C# Adding style to a control - c#

I have a Panel and I am adding controls inside this panel. But there is a specific control that I would like to float. How would I go about doing that?
pnlOverheadDetails is the panel name
pnlOverheadDetails.Controls.Add(lnkCalcOverhead);
The control named lnkCalcOverhead is the control I'd like to float.
Thanks in advance
EDIT: By float I meant the css style not anything fancy :)

If you have a CSS class defined for the control, you could do this before calling the Controls.Add method:
lnkCalcOverhead.CssClass = "MyClass";
If you want to use the style attribute directly, try this:
lnkCalcOverhead.Style.Add("float", "left");

IF you are talking about System.Windows.Forms here (and not WPF or ASP.NET):
When you are talking about float, do you mean you want to position it anywhere you want by code? If so, just set the .Location property of the control.
If you are talking about letting a control be moved around inside the panel by the user of your program, you will have to code that. That means capturing mouse events and moving the control accordingly?
Alternatively you can instead of letting the control reside within the Panel, make it as a single control occupying a new form (hence you dont have to code all the mouse event handling). Just make sure that the window is limited to be moved within the boundaries of the "parent panel" (just check on the move event of the form if its within the boundariesm and force it to stay inside).

Related

Filling the empty space left from hidden controls

The problem is somewhat trivial yet I can't seem to find any standard solution to it.
I have a form where according to the user permissions I show or hide a button+textbox. What happens right now is this:
This is when the user has permission to see/use this. And when he doesn't :
It's not visible - the easy part, but from what I found out, only the FlowLayoutPanel offers automatic functionality to hide this empty space and my case is not that.
Set the Dock property to Top for the label and textbox. That way, when the button and textbox disappear, the layout system will move them up automagically.
It depends if this is a simplified version of your question. If it's only what you describe, whenever you check permissions/hide the relevant controls, you can just reposition the other controls using their Top property.
Wrap the control in a div and set floating to float:none. then, hide the div together with its content inside by setting the div display to none display:none. Eg;
<div style="display:none;float:none">
content to hide begin here
</div>
Well, its very simple, just change the location of that control to the location to the hidden control
and relocate it to the original location when not hiding the hidden control.
btn.location=new point(x,y)
where x, y are coordinates of the hidden control shown at winforms.

C# WinForms add controls programmatically

Can someone suggest the best way to achieve my goal?
So, I have a form with three buttons. What I want is, depending on what button is pressed on panel should be shown different controls (user control). I made this in a simple way: all are added from the beginning, I just make change to the visibility. But what would be nice is, if someone can suggest a more appropriate way, because there is no need to have objects created from beginning.
You can always create the appropriate UserControl, and add it to the Panel.Controls at runtime. This will allow you to create the control(s) as needed, instead of on initialization of your Form.
I would indeed create the controls at design time - if there's advantage no to dynamically create them. Why complicate matters?
If there are a number of controls I would put them all in a panel (within the panel you've already mentioned) so you're only changing the visibility of a single control (the panel) rather than each one within it.
When you press the appropriate button show the appropriate panel (and remember to hide the others in case you've previously shown them)

Create controls dynamically or create controls in a side form? C# winforms

I have a form full of controls, and there is no room for other controls. On the bottom of the form I have a panel with some controls on it.
My goal is that when a certain button is clicked, the original panel on the bottom will be replaced with another panel that contains controls which could be created before the program starts, meaning these controls in the panel do not need to be created dynamically. The replace action would be executed by setting each panel's visible field to it's matched value.
I have thought of two ways of doing this - either creating the new panel (and it's controls) dynamically and adding it to the form instead of the original, or creating the new panel in another form and when the relevant button is clicked the panel being taken from that form and added to the required form (by creating an instance of the new form and making it's panel's modifier public). The "side form"'s purpose is only to create that panel, it has no functionality of it's own.
The advantages of creating the new panel dynamically:
There is no need to create a zero-functionality form.
The advantages of creating the new panel in a side form:
It's very clear which controls are added to the new panel and their positions.
It's very easy to set the location and other fields of the controls in the new panel.
Which way is better?
Thanks!
Have you considered TabControl? That seems a good fit for your needs. Other controls I can think of are StackPanel (Can be fairly easily done for Windows Forms) or OutlookBar like control (again a user control).
Simplest and quickest way seems to be TabControl.
Edit:
SideForm is a different windows form I suppose. So if you are thinking to make controls public and then change their visibility etc, please don't. Use delegates to handle SideForm's events in MainForm.
As you mentioned, there is no room for more controls, I would suggest more screens rather than just one. Having said that I do not know much about your current UI design and functionality so it's up to you.
I would say having the controls hidden and just playing with the Visibility is fine. This means that you do not have to worry about positioning of controls, anchoring and docking at runtime. The problem could well be loading of form. Having huge number of controls having a lot of data associated with them may slow things down.
IMO the best way would be to utilise user controls for this purpose. Simply create one user control per panel you wish to show/hide and place your controls inside. This way you will have both: the designer and the "extra form" you wanted.

Hide/show part of the window with a button click

I have a simple form containing a main view and also some text boxes and an "add" button that I use for adding data that is displayed in the main view (and stored in a text file). What I want to do is to add a little button that will toggle hiding/showing of the adding controls. Such button usually is a small square containing two arrowheads pointing up/down depending on the state. How do I do that?
(I'm sorry for the terrible title, but I don't know the proper name for this. Could anyone tell me?)
I don't think there's something built-in in WinForms for that. When I needed to do something similar, I just changed the Height of the form...
this.ClientSize = new System.Drawing.Size(required_width, required_height);
use a bool for hiding/showing
You can use the forms Height property and the controls could be hidden with Control.Visible = false
I think the word you're looking for is "Collapsible panel".
A quick google/codeproject search will provide you with some links:
http://www.codeproject.com/KB/miscctrl/TgXPPanel.aspx
http://www.codeproject.com/KB/miscctrl/XPCollapsGroupBox.aspx
http://www.codeproject.com/KB/miscctrl/CollapsibleGroupBox.aspx
I suggest you use a SplitContainer control and play with the Panel2.Collapsed property by sitting it to true or false
put the control that you want to hide/show inside panel2 and put the button in panel1. Change the Orientation property to Vertical, and there you go

To Show a Control in two Windows Forms

I have inherited a control from Panel-Class.
I have added some events to this control. I gave move - ability to this control
and so on ..
I have two display screens. I have a main program where the inherited
panel is displaying an image on a small area. I want to show this panel
fullscreen on a second.
I created a new form and use the same control... But i can not move both screens
together. What should I do ?
If you want to be able to manipulate both forms at the same time, show the second form with Show() instead of ShowDialog(). You can certainly pass the original panel to the second form and add it to the form's Controls collection. I am not sure if this is the best way to do it (sharing one control across two forms), but I don't know your requirements either.
I wouldn't use a second form, but a second 'mode' (fullscreen vs. not) on your existing form.
You can have 2 panel controls, or just one and resize.
I think this kind of behaviour calls for a model-view pattern. If that's implemented, the rest should fall into place.
The problem is that you only have one instance of your inherited panel. You actually have to make another "copy" of it, a new instance, before you can add it to the other form.
Mypanel mypanel1 = new Mypanel();
Mypanel mypanel1copy = new Mypanel();
You can either edit these instances to contain the same data all the time through the run, or use something like "Deep Copy":
How do you do a deep copy of an object in .NET (C# specifically)?
Keep in mind, that any changes to mypanel1 should be done to mypanel1copy, too.

Categories