I have a SplitContainer that contains a textBox, which is used to inform about errors or speficic situations. But most of the handled cases don't produce errors and therefore the box is not needed. In thoses cases I would like to make it disappear. Setting Visibile=false is not what I intend, because it still limits the other Windows.Form-elements. Instead those elements should "grow" in the left space from the box. Is there some thing like a floating disapear behaviour?
If I understand correctly, the text box in question is placed inside the let say right panel of the SplitContainer while the other controls - inside the left panel (I might be wrong, but otherwise I see no reason why you are mentioning the SplitContainer in the question). Then, depending on which panel of the SplitContainer contains the text box, you can set Panel1Collaped or Panel2Collapsed property to false to make it disappear and let the other panel fully occupy the split container space.
If you want the control to be removed, you can do:
textBox1.Dispose();
This also causes your other elements to "grow" in the left space like you wanted.
Related
Here is the problem. If you dynamically place controls in a panel, it works fine, but only until the vertical scrollbar appears. Once there is enough content for this to happen, it starts positioning controls nonsensically.
In my window, you can click a button to add another row of controls inside the panel, which represent options for an item in a list. If you scroll the vertical scrollbar on the panel all the way down and click the button again, the new row of controls will be positioned below the bottom edge of the panel out of view. If you scroll down, there is a huge gap between the new row and the previous row of controls. This should not happen. The positioning code is working flawlessly, as proven by debug output. As far as I can tell, the problem is the stupid anchoring system, however disabling anchoring on these controls does not fix the problem as one might expect. Instead, it just makes it position them wrong in a different manner. This makes no sense at all, and is super annoying!
I tried disabling Autoscroll in code before controls are added to the panel. No change. So I modified that code to disable both the vertical scroll bar and Autoscroll and set the scrollbar to not visible before controls are added. No change again, except that the now disabled vertical scrollbar still manages to appear usable when there is enough content in the panel in spite of it being disabled and set not visible!? That's not supposed to happen when I disabled and made it invisible! With anchoring disabled on the controls being added to the panel and once the vertical scrollbar has appeared, clicking the button to add a few more rows of controls now causes them to be indented a bit for no reason and positioned overlapping each other a bit vertically! It's as if the coordinate system in the panel has somehow arbitrarily changed, because of the presence of a vertical scrollbar and anchoring being disabled on the controls? The debug code shows that the controls are all being placed at correct coordinates, yet they appear positioned very wrongly. So my code is working perfectly, and therefore something else is the problem here.
Everything behaves exactly as expected up until the vertical scrollbar appears. This is so bizarre. Does anyone have any idea what on earth is going on with this stuff? Apparently it is far easier to make it do stupid stuff than to get it working properly.
Thanks again! I got it working. I went with TaW's solution first since it seemed like the simplest solution. Incidentally, I already tried TaW's approach days ago when I was fighting with it, but I had naturally subtracted the AutoScrollPosition value rather than add it, because I didn't expect it to be a negative value!
It seems very odd that control positioning is relative to the current AutoScrollPosition, as absolute coordinates seems like a much more natural, intuitive approach than having negative numbers. I guess that would make it slightly harder to place a control in the currently visible area, but I suppose that's not a big deal as most scrollable interfaces are probably initialized ahead of time and don't need to do that anyway.
Suppose that I have the following form in Designer:
I want to give users the ability to stretch this form as they want and all controls should be located like in the picture, no matter how user changed the size of this form, so they should take the same amount of space and stick to the same controls and borders.
How can I do it in WinForms? I know that there are such things like Docks etc, but I didn't find the correct way to use them in this situation.
You want the Anchor property in this case, not Dock. Anchoring means that a control will always keep the same distance to certain sides (top, left, right, and/or bottom) even if it means that the size must be changed; docking OTOH does not care about margins, it just fills up all available space on one or all sides.
Here's what you might want to do:
Anchor the two image buttons to the top and right.
Anchor the OK button to the right and bottom (I guess).
Anchor the large ListBox to all sides.
Just To Add some notes on good answer of stakx
For Controls Like ListBox that have a limit to their height, setting anchor is not enough and you should set IntegralHeight of them to false.
I reccomend to set MinimumSize of Form to prevent a user from sizing a window to an undesirable size.In your case Set it to a minimum acceptable size to prevent ugly small form with an unusable ListBox.
I have a multiline textbox in a WinForms application. What I'd like to do is always have the top line visible, even if it scrolls. Is there some trick someone knows to do this?
Fake it. Use two TextBox objects, draw your own borders. You will need to deal with wrapping to the next line yourself.
You could also copy the first X characters to a label so when the TextBox scrolls they can see the first line in the label.
Unless it is an essential feature I would try to cut it.
The simple answer; depending on the appearance you are going for is to use existing windows controls to get the effect you want.
You can use a label control above a textbox and allow the textbox to scroll.
You can use two textbox - the top with it's .multiline property set to false, while the bottom allows scrolling.
You could encapsulate this all into a user control for reuseability.
Beyond that, I think you'd be looking at a fairly large project to implement your control (or at least overriding the onPaint() event of the textbox) with the desired behavior.
In my winform, I have three group boxes, based on selection of the
items in a combobox the second group box(at the center)
is hidden using groupbox.visible property.
The problem is when the second goupbox is hidden, there
seems to be an empty space in the area of the hidden group box,
I want to move the third group box to the place where second
group box is present. Can I use any other control instead of group box?
You could manually set Location and Size properties of your third groupbox or (I think better) set Dock property of both groupboxes to Top, so when your second gb becomes not visible, the third should scroll up to occupy free space.
You have a small number of options, assuming you are using just the default VS controls.
If you place these group boxes in a flow layout panel, when you make it invisible the layout panel will move other controls into it's previously occupied space.
If you dock the group boxes, making one invisible causes the others to occupy the space if the docking dictates.
If you cannot use layout mechanisms or controls, your only option is to modify the location or size of neighbouring controls to fill the space manually.
I very much advise trying to use the layout containers, or find a container online that handles the positioning for you. Position code buried in the form using potentially lots of "magic" numbers is definitely not maintainable.
you should solve it by chaning control that you're groupboxes are in. Their position can't be set to constants. The other solution is to change position of third groupbox when you change visibility of the second one.
A FlowLayoutPanel should work http://msdn.microsoft.com/en-us/library/system.windows.forms.flowlayoutpanel.aspx
I'm working on a "tricky" UI. Part of what I need to do is easily show and hide various UserControls. Generally one control will occupy the entire main window when needed, the other's will hide.
In WinForms I used to simply use SendToBack and BringToFront and easily showed the control I wanted to show. Now I have no clue. Played around with zorder but that didn't seem to work.
I'm thinking maybe put all the controls I want on the main window, then pro-grammatically resize them and remove the unused ones... or something.
Any ideas?
You should set the Visibility property to Collapsed, Hidden or Visbible depending on whether you want the controls removed, hidden or shown.
As #AresAvatar points out Collapsed removes the control completely so it takes up no space, this means that other controls may move around the container. If the position of elements is important then using Hidden will be the better option.
UIElement.Visibility Property on MSDN
Visibility Enumeration on MSDN