0

I'm building a little form in VB.NET when a button is pressed to request some information. I'd like it to be vertically stacked with Label, Control, Space, Label Control, Space, etc. The textbox controls should be the full width of the form, however they're shorter and I don't understand why.

Private Sub Btn_BLApi_Test_Click(sender As Object, e As EventArgs) Handles Btn_BLApi_Test.Click
    Dim frm As New Form With {.Size = New Size(300, 500)}
    Dim fp As New FlowLayoutPanel With {
        .Dock = DockStyle.Fill,
        .BackColor = Color.PapayaWhip,
        .FlowDirection = FlowDirection.TopDown
    }
    frm.Controls.Add(fp)
    Log($"frm width={frm.Width}, fp width={fp.Width}")
    fp.Controls.Add(New Label With {.Text = "HTTP Method:"})
    Dim BLApi_ChooseMethod As New ComboBox With {.Name = "Method"}
    BLApi_ChooseMethod.Items.AddRange({"GET", "POST", "PUT"})
    fp.Controls.Add(BLApi_ChooseMethod)
    fp.Controls.Add(New Panel With {.Height = 20})
    fp.Controls.Add(New Label With {.Text = "URI (starts with '/'):", .Width = 200})
    fp.Controls.Add(New TextBox With {.Name = "URI", .Width = fp.Width, .Anchor = AnchorStyles.Left + AnchorStyles.Right})
    fp.Controls.Add(New Panel With {.Height = 20})
    fp.Controls.Add(New Label With {.Text = "Parameters (separate with '&')"})
    fp.Controls.Add(New TextBox With {.Name = "Parameters", .Width = fp.Width, .Anchor = AnchorStyles.Left + AnchorStyles.Right})
    fp.Controls.Add(New Panel With {.Height = 20})
    fp.Controls.Add(New Label With {.Text = "Request Body (In JSON format)"})
    fp.Controls.Add(New TextBox With {.Name = "Request", .Multiline = True, .Width = fp.Width, .Anchor = AnchorStyles.Left + AnchorStyles.Right})
    fp.Controls.Add(New Panel With {.Height = 20})
    Dim btn_Go = New Button With {.Text = "Send Request"}
    AddHandler btn_Go.Click, AddressOf Btn_BLApitTest_Click
    fp.Controls.Add(btn_Go)
    Log($"Parameters width={fp.Controls.Find("Parameters", True)(0).Width}")
    frm.ShowDialog()
End Sub

In this code, the form is 300px wide and the flow layout panel is set to fill it and should also be 300px wide. But in the first Log I get the output:

frm width=300, fp width=284

Then the TextBoxes should be set to the width of the flowlayoutpanel (fp), but the second Log reveals that it is neither 300px nor 284px wide, it is 200px wide

Parameters width=200

This is what the form looks like:

Temporary Form Example

Why are the widths not all 300px? Also, when I expand the form, the flowlayoutpanel expands with it, as expected (PapayaWhip background color), but the anchored TextBoxes do not)

Scott
  • 3,663
  • 8
  • 33
  • 56
  • 3
    Form.Width includes the borders. Use Form.ClientSize.Width. FlowLayoutPanel overrides the behavior of the Dock and Anchor properties since its job is to position controls. – Hans Passant Apr 24 '23 at 15:29
  • Does this answer your question? [Cannnot resize Child Controls in a FlowLayoutPanel Control](https://stackoverflow.com/questions/1844145/cannnot-resize-child-controls-in-a-flowlayoutpanel-control) – Étienne Laneville Apr 24 '23 at 15:49
  • @ÉtienneLaneville no, not directly. That explains the anchor question, but not why when I explicitly set the Textbox width =fp.Width, the TextBox ended up with a width of 200. – Scott Apr 24 '23 at 17:51
  • @Scott "_the FlowLayoutPanel control calculates the width of an implied column from the widest child control in the column._" Everything will be the width of your `"URI (starts with '/'):"` Label for which you have specified a Width of 200. – Étienne Laneville Apr 24 '23 at 18:29
  • so fp.Width is the width of the column, not the width of the full control? That seems odd but would make what's happening make sense – Scott Apr 24 '23 at 20:32
  • Even setting TextBox.Width=frm.Width makes it no larger than 200 – Scott Apr 24 '23 at 20:35
  • 1
    I can't see any reason to be building this form dynamically. There's nothing variable there so why would you nor just build it in the designer and then create an instance at run time? You seem to be making things difficult for no reason. – jmcilhinney Apr 25 '23 at 02:18
  • It's 20 lines of code - didn't seem like it should have been difficult when I first did it. Also, it's a bit of a prototype for making dynamic questionnaires for users – Scott Apr 26 '23 at 12:51

0 Answers0