Hi All,
I would like my 'quit' button to appear in the bottom right hand corner of my menu screen, but it seems that every computer in the office has a different sized screen, different size ration (some are widescreen), and different resolution. When I deploy my program, the quit button may appear off-screen (the user has to scroll down to find it) or it may appear half way across the screen on a widescreen laptop for example. If I set my form to 'maximised', is there a way to position a button relative to the bottom right hand corner of the form?

Also, on one maximised form I have two datagridview's side by side. Is there are way to automatically set each one to take up half of the available form width, so they fill the form nicely?

Many thanks,
toomutch

Dani AI

Generated

this is a WinForms layout question. Instead of recalculating positions on each resize (as demonstrated), use the built-in layout system so the UI adapts cleanly to any resolution and aspect ratio.

For the Quit button: anchor it to the bottom and right so its distance from those edges stays constant as the form grows or shrinks. You can do this in the designer, or set it once in code. For the two DataGridViews: a TableLayoutPanel with two 50% columns keeps them side-by-side and always sharing the width evenly, with no manual math.

' WinForms: pin Quit button and split client area 50/50
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    QuitButton.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
    QuitButton.Location = New Point(Me.ClientSize.Width - QuitButton.Width - 16, _
                                    Me.ClientSize.Height - QuitButton.Height - 16)

    Dim tlp As New TableLayoutPanel With {.Dock = DockStyle.Fill, .ColumnCount = 2, .RowCount = 1}
    tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 50.0F))
    tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 50.0F))
    tlp.RowStyles.Add(New RowStyle(SizeType.Percent, 100.0F))

    DataGridView1.Dock = DockStyle.Fill
    DataGridView2.Dock = DockStyle.Fill

    tlp.Controls.Add(DataGridView1, 0, 0)
    tlp.Controls.Add(DataGridView2, 1, 0)
    Me.Controls.Add(tlp)
End Sub

Tips: use ClientSize (not Width/Height) when computing positions; give the form a sensible MinimumSize; for mixed-DPI offices, set AutoScaleMode = Dpi and test at 125%/150%. Avoid mixing Dock and Anchor on the same control. This approach is simpler, flicker-free, and easier to maintain than handling Resize manually.

Recommended Answers

All 2 Replies

try something like this

set the y axis of the datagridview as u want....

u can also write the below code in load as well as form resize event so that if the form is resized even the controls will be resized...

          DataGridView1.Size = New System.Drawing.Size(Me.Width / 2 - 100, 300)
          DataGridView1.Left = Me.Left + 50

          DataGridView2.Size = DataGridView1.Size
          DataGridView2.Left = Me.Width / 2 + 50

          Button1.Top = Me.Height - Button1.Height - 20
          Button1.Left = Me.Width - Button1.Width - 20

perfect - just what I was looking for.
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.