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:
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)