Question

I am trying to figure out how to make these widgets, in my case two Rich Text box widgets stay anchored in place so that I can resize the form that they are in and not lose its spacing. Here are some screen caps to better show what I am talking about. enter image description here

Above is what my Form looks like in visual studio before I run it. It is also how I want my form to look. The space in between the two rich text boxes is I want to retain.

enter image description here

Above is what happens when I run this program. My question is how can I set say "anchor points" or something like that to keep the widgets in place so that when I resize the form it will retain the spacing. In other words when I run the program it will look the same has it does in the first image.

FULL CODE: (I am not sure if having this will help but here is what I have)

Public Class Form1
    'Decloration 
    Private widthOffset As Integer = 30
    Private heightOffset As Integer = 30

    Private preWidth As Integer
    Private preHeight As Integer
    Private postWidth As Integer
    Private postHeight As Integer


    Private Sub RichTextBox1_Resize(sender As Object, e As EventArgs) Handles RichTextBox1.Resize
        RichTextBox1.Width = Me.Width - widthOffset
        RichTextBox1.Height = Me.Height - heightOffset

        RichTextBox2.Width = Me.Width - widthOffset
        RichTextBox2.Height = Me.Height - heightOffset

    End Sub

    Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        RichTextBox1.Width = Me.Width - widthOffset
        RichTextBox1.Height = Me.Height - heightOffset

        RichTextBox2.Width = Me.Width - widthOffset
        RichTextBox2.Height = Me.Height - heightOffset

    End Sub
End Class
Was it helpful?

Solution

In WinForms you can add the controls to a TableLayoutPanel, setting the ColumnStyles for each control to either an Absolute or Percent size, so that they either stay the same size or resize as the container is resized.

More info: Walkthrough: Arranging Controls on Windows Forms Using a TableLayoutPanel

The FlowLayoutPanel control and the TableLayoutPanel control provide intuitive ways to arrange controls on your form. Both provide an automatic, configurable ability to control the relative positions of child controls contained within them, and both give you dynamic layout features at run time, so they can resize and reposition child controls as the dimensions of the parent form change. Layout panels can be nested within layout panels, to enable the realization of sophisticated user interfaces.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top