Center form during runtime - c#

I've been developing a game. As such, I need to be able to display information that are group accordingly. I've been using a menu system that allows me to utilize 1 form to display different bits of grouped information using the panel control. Using a timer on the form, it automatically changes in size depending on just how much information is displayed, using the currently visible panel as a guide for dimensions.
I have the form's screen position behavior set to Center Screen. This works great.
However, there is a problem. The form is only truly centered when it is the size of the biggest panel - the log in screen - is active, being that it is the first menu a user sees.
My question is, using code, could I get the form to be re-centered during runtime? If so, how would I go about doing so?

You can call the method Form.CenterToScreen
or follow this answer!
StartPosition is set to CenterPosition but my form is not centered

Related

How to set fixed size form to different resolutions

I have a problem and I couldn't find the answer anywhere on the internet. I am working on a project which its forms should be fixedsingle borderstyle. I don't want anyone to maximize or minimize the forms. So the real question is that I have two monitors and I'm working on the bigger one. When I start the program and slide the form to the smaller monitor, it looks exactly the same heigth and width. But I want the form to look smaller as the screen gets small or to look bigger if the screen gets bigger. Can you please help me?
You need to specify the Form.MinimumSize and Form.MaximumSize properties according to your needs. If you want to adjust the form dynamically at run time, you can hook the Form.Move event and set those properties relative to the screen's resolution minus the space used by the taskbar using Screen.WorkingArea. For a better user experience if the form is smaller than the screen size, I recommend disabling the maximize form button by setting Form.MaximizeBox to false.

Not allowing controls to overlap

I am have been trying all day to figure out how to not allow controls that I draw on event to check and see if there is another control there and if so to move it over so they do not overlap. I need them to be able to be overlapping if the user drags them there but on the creation of the control to check and move them if necessary. I am trying to create a card game and dont want controls to be hidden behind controls (cards) that the user has not yet moved manually. I am using picture boxes as my cards. Any help would greatly be appreciated. Here is a screen shot of my form. The darker panels are whats in the players hand. The panels that are a light grey are the playing field. Ideally, the user would play a card and it would try and place it in the top left of the panel. if there is a card that intersects then move it over until it doesn't touch a card. Basically find a open space to put the card.
Have you tried using the flow layout panel Class it's under the toolbox=> container with this control you can set how controls are added to it.

WinForm Controls are transparent, and not displaying properly

When a form loads, I'd like it to show a loading image (within a Picture Box) and a standard Windows label with some text. However, all I see are white boxes, or sometimes I see another form underneath. How do I get the image and label to display properly.
I've tried setting AllowTransparency to false when the form loads, and also setting the Transparency Key of the form to some other colour, but nothing has worked.
The project is C# .Net v3.5 (also tried v4 and v4.5).
Any ideas?
First, you can't display an image, busy-wait, and then change the image - this will never redraw anything, leading to the symptoms you describe. To "wait" you will need to return control to your main application loop so it can continue to process messages (e.g. to handle redraw requests for your window). One way to do what you want is to display your initial state (splash screen) and then use a timer to call you back later to change the display to your second state.
The next problem you face is using forms controls with transparency. Most controls treat "transparent" as "fill your background with your parent controls color", which is not what you want. An easy way around this is to implement a Paint handler and draw the image and text for yourself - this gives you much more control of how your display looks, and will also allow you to get a cleaner redraw (no flicker or other problems caused by the display being built up but by bit in several controls)
Lastly, consider implementing your splash screen display as a separate control/form that you show above your main form during loading, as that makes it easy to "overlay" on your main form without having to change its design at all.
Just write formObjectName.Refresh() after formObjectName.Show()

Dock, Anchor and Fluid layouts in Windows Forms Applications

So, I've been asked to redesign an old application I wrote a few years ago.
Basically, nothing much needs to be changed, except that the Customer wants it to be more fluid, and that it must be fullscreen (no visible "window") I.e. no Titlebar, just a Borderless fullscreen Window.
What is the best way to make sure everything stays fluid, I mean how can we make sure everything appears where it should, 'cause you know, different resolutions, monitor sizes etc?
This is easy in web pages/css, but this is not something I've done before. Most of the Controls will be created programatically at runtime, based on what action was performed, etc. How would I accomplish such a layout? Basically I want to be able to lay it all out full screen, without knowing how large their monitor is, or what resolution they're using.
Your certainly correct in trying to design your form using a fluid layout that responds to the size of the available space and size of the form font. To do that you want to use the following controls and control properties.
1, TableLayoutPanel will split an area into a set of rows and columns and allow you to position your child controls within individual cells of that table layout. This responds to a change in the form width and height.
2, FlowLayoutPanel will position your child controls from left to right and automatically move to a new row when you run out of space. This is great for a fluid design as it will adjust the layout depending on the available space.
3, Control.Anchor property allows a child control to alter position and size based on the size of the form client area. So you make your control always be a fixed offset from the right or bottom edges.
4, Control.Dock property will position a child control against an edge and the opposite size will automatically be defined by the containing form.
You could put Your controls into tableLayout, and set the Dock property to fill.

Graphical hiccups in C# User Control - Resize obscures components

I'm experiencing difficulty with a custom-made User Control, and my searching on Stack Overflow, MSDN, and Google didn't pop up any troubles quite like the one I'm experiencing.
I have a very simple User Control: It's a label, a text box, and a button, with a SaveFileDialog and a FolderSelectDialog available. The text box and button are anchored Left,Right and Right respectively, with the intent that if the control is resized larger, the text box will enlarge to fill the gap, and the button will stay on the right edge of the control.
The problem I am encountering is that when the control is enlarged, the area to the right of the default width of the control becomes blank space when the project is built and run. The pictures here will illustrate what I mean:
In editor:
Running:
The control is smallish in its design window, but when I add it to a form and widen it, it behaves as intended. However, when I run the form the control was added to, half the control isn't visible.
I suspect that I'm overlooking something fairly straightforward, but I wasn't able to find anything addressing this point in my search. Help would be much appreciated.
My guess is that there is a panel or something that is added to your control and will be brought to front somehow runtime.
from property window's top there's a combo from which you can select all the controls in your User Control.
check if all the controls are what you want.
if you find that panel or anything delete it :)
EDIT:
alright this was not your problem.
now I can only assume that you have set some manual sizes to your user control, i.e. in its constructor. in that case designer will show the correct size of you user control,
now some other place in your code, you have set the user controls size manually again. if the layout is suspended and size changes, I think that the anchored controls' size will not change automatically.
if this is your problem, it is probably hard to find.

Categories