Drawing Custom Control above all other controls (previously added) - c#

I have a UserControl that adds other UserControls, but I want the "latest" control added to be topmost so it's above the others. Because the controls should be overlapping eachother. Like a card game. So I add 5 controls, the first one should have the least priority the latest the most priority - most visible.
Any ideas?
Or do I have to override the Paint method for the "container" control? And Control.CreateGraphics() and draw it?

Consider BringToFront and SendToBack methods of the Control class.
Check out answers to these questions too
How to set Z-order of a Control using WinForms
Bring Winforms control to front

Just use userControl1.BringToFront() when you add the new control.
Note however, that won't prevent the user from "tabbing" into the controls that are underneath it. For that, you need to disable or make invisible the other controls.

In Windows Forms, the order in which controls are added to their parents' Controls collection determines the order of their rendering.
This means that either you handle the addition of child controls in code and use the appropriate insert positions, or you move the controls around in the designer (which, unfortunately, often means dangerous hand edits to the *.Designer.cs file).
I recommend that you go for the first approach, which is the only feasible method for larger WinForms projects anyway, and make the control insertion logic explicit in your code. The good news, by the way, is that there is no need to tinker with paint handlers, so your worry about hacks like using CreateGraphics() is unjustified and dispelled :)

Related

How to initially place WPF controls on dynamic fullscreen application

Alright, so I'm trying to figure out the best way to accomplish this for my rather unique case. I have a fullscreen WPF application where controls are added to a Grid dynamically and the user is given the option to freely move/resize them. The controls can be individually moved or resized anywhere on the parent Grid but I have prevented them from overlapping via their MouseMove events. They also cannot be moved outside of their container. Since the app will be running on machines with different monitor sizes, I need to take that into consideration when making the original layout. I have an initial layout that I would like to use, but it would seem that I'm out of options:
I first tried using rows and columns just to (initially) place the controls that are added. This method places them correctly but becomes a problem when I need to move or resize the elements because the control is already assigned to a particular row and column. I could try to use this method and then remove any rows/columns after placing the controls, but I don't think that would work well.
I've also considered a Viewbox, but that's not practical in my case for fairly obvious reasons (as it merely resizes the controls to fit the screen). I'd prefer not to use this because I would only need it to standardize my initial layout. That's it. I also don't want to mess up any text that will be displayed on the window.
So yeah, this is more of a "best practice" question because any solution I can think of would not look very professional or elegant. Feel free to ask any questions if you need clarification.
Edit: As an additional note, I'd prefer to stick with a Grid as opposed to a Canvas as my container.
Edit 2: Just to be clear, I would not need the specific (inital) layout after the first launch. When the program exits, the layout (Margins, Width & Heights, etc. for each element) is saved to a file to use for the next launch.
I've developed a number of kiosk/interactive applications using WPF. If you are trying to show the element transitions (while moving), then it might be best to use a parent Canvas and bind the Canvas.Left and Canvas.Top properties. You can mimic the grid alignment, using a Canvas, if you put in place mechanisms (e.g. Manipulation/Mouse events, converters) to make sure that the Canvas attached properties adjust to the row/column offsets.
It is not uncommon at all to use the Viewbox to mitigate display differences (and your use case is not "rather unique"). You set the Viewbox to a target resolution (e.g. 1920x1080) and allow the control to fill the available space. The other alternative would be to dynamically apply a content template based on the application window size/ratio.
After looking at multiple options, I've decided to just use a calculation to (sort of) simulate the behavior of rows/columns. Because my application is fullscreen, I can take my SystemParameters.PrimaryScreenWidth and SystemParameters.PrimaryScreenHeight to orient my layout. Using a combination of universal padding (static values) and ratio-based calculations (dynamic values), I can smoothly set my initial layout.
For instance, I'm dividing the width of my monitor by 6 (rounded up to avoid decimals) and using that (minus half the control's width) as the control's Margin.Left property, centering it on a 'column' of sorts.
Honestly, my initial layout is fairly simple right now, so we'll see if this will suffice going forward. Thanks to everyone who contributed to the question, and sorry if I was unclear on what I was asking.

How Can I Intelligently Implement Auto-Resizing in a Visual C# Winforms App?

My application (which I am using Visual C# 2008 WinForms for) involves a lot of generated controls. Specifically: grids of buttons, arrays of labels, lists, headings, etc... all populated so that they fit their containers appreciably.
I want users to be able to resize the main form, which obviously would require me to either destroy my generated content, and remake it at the proper size OR I could index through every control, figure out what it is by name and type, and re-size each item individually. I would have to do this while/after the form resizes.
Are there any more intelligent ways of doing this? Dock and Anchor don't quite apply here because I am dealing with items that don't make up 100% of a dimension (for example, grids of buttons).
Hard do give a reasonnable answer without seing just how complex the layout in question is.
But in principle, you should use a layout container such as FlowLayoutPanel or TableLayoutPanel to do the job they were designed to do. If one does not do the job, just nest them.
Docking/anchoring is probably the answer here. You need to anchor your grid to top/bottom/left/right or dock it (same effect, but the grid will fill the parent control).
If this is done right your control(s) will re-size with the rest of the form just as if you created everything in the designer.
I believe something like this would work:
Control.Anchor = AnchorStyles.TopLeft | AnchorStyles.BottomRight;

Show and Hide UserControls (BringToFront/SendToBack)

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

handling orientation changes effectively for all controls with small code change

I have a Windows Mobile project built in C#.
I have a lot of ready made forms having various controls on it, from Listviews to Editfields.
When user changes orientation some elements are not refreshing correctly. For example the Listview's columns are same and doesn't accommodate the new screen width change (scrollbars appear or half of the screen is filled).
How do you handle these changes?
Do I need to call for each form these fixes by hand, or I can create some kind of global way to fix this? I would like to go with the simplest method if possible.
I would like to avoid the classic way, to add code to all of my forms. So I am looking for better ways, and I would like to see more ideas.
I'm assuming that most of your controls are using a DockStyle, and that will get you 90% of the way in terms of updating the GUI on orientation changes. For the ListView, you'll have to add in some code.
You can add an event handler on Form.Resize, and there put in code to resize the ListView columns. You can tell portrait vs landscape by comparing width vs height. There's also a way to add an event handler on an actual orientation change, but it's interop and I don't remember the code offhand. Form.Resize should be sufficient for most cases.
You can check out an example here

Dynamic Form Controls

Using C# 2.0 what is the best way to implement dynamic form controls?
I need to provide a set of controls per data object, so should i just do it manually and lay them out while increment the top value or is there a better way?
You can use panels with automatic layout such as FlowLayoutPanel and TableLayoutPanel.
Unfortunately there are only 2 panels with automatic layout out of box but you can create custom layout panel.
I would recommend you to read following articles:
How to: Create a Resizable Windows Form for Data Entry
Walkthrough: Creating a Resizable Windows Form for Data Entry
Another option would be using of WPF (Windows Presentation Presentation).
WPF is a perfect match for your task.
WPF controls can be hosted in WinForms apps so you don't have to switch to it completely.
#Sam I know this question was about Windows Forms, but you should definitely start looking at WPF. This sort of scenario is really easy in WPF with DataTemplates and TemplateSelectors.
What do you mean by “dynamic”? A new, fixed set of controls for each data row in the data set? Then use a UserControl that contains your controls.
Or do you mean that, depending on your data layout, you want to provide the user with a customized set of controls, say, one TextBox for each column?
Yeah, I've found manually layout out controls (incrementing their Top property by the height of the control plus a margin as I go) to be reasonably effective.
Another approach is to place your controls in Panels with Dock set to Top, so that each successive panel docks up against the one above. Then you can toggle the visibility of individual panels and the controls underneath will snap up to fill the available space. Be aware that this can be a bit unpredictable: showing a hidden panel that's docked can sometimes change its position relative to other docked controls.
Well that's the way we are doing it right now on a project. but that's only useful for simple cases. I suggest you use some sort of template for more complex cases.
For instance I used Reflection to map a certain type of control to a certain property on my domain objects on an older project.
You could try generating the code from templates using t4 see T4 Templates in Visual Studio for Code Generation Screencast for a simple example. You can apply this to WinForms.
Also DevExperience has a nice ( expensive ) framework, see DevExpress eXpressApp Framework™ .

Categories