What is the best practice when you want to modify a form

I am modifying an application and I need to add and remove field depending on the configuration the user set.

Right now, I use two panel that contain the corresponding field and I'm hiding and showing them when I need to. I re-size the form and relocate elements in the form accordingly. But then, it become hard to modify either of the panel and the form when you are in editing mode.

I also think that since it re-use a lot of the element, it would be unwise to create another separate form but maybe it's just me.

I'm not sure how to rearrange all of this and would really like some tips.

Upvotes: 0

Views: 105

Answers (2)

Alex
Alex

Reputation: 4948

It seems like you are trying to adjust the form size depending on different controls. I suggest you learn about TableLayoutPanels. For each TableLayoutPanel Row/Column, you can set that Row/Column to AutoSize.

This means if there's nothing to display (aka it is hidden), your TableLayoutPanel will AutoSize accordingly.


Example

  1. Your TableLayoutPanel is docked to the MainForm.
  2. You have three rows in your TableLayoutPanel.
  3. The first row will decide if we should hide or show the 2nd and 3rd rows.

Form1.vb [Design]

Form Design

Using the following code, the TableLayoutPanel will adjust it's controls appropriately:

Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
    Select Case True
        Case RadioButton1.Checked 'Show Row #2
            Label1.Show()
            Label2.Hide()
        Case RadioButton2.Checked 'Show Row #3
            Label1.Hide()
            Label2.Show()
    End Select
End Sub

Here's the result:

Show Row #2 Show Row #3


How Does it Work?

Almost everything was done by the designer.

  • Label1 was placed in your TableLayoutPanel's 2nd row (Index=1)
  • Label2 was placed in your TableLayoutPanel's 3rd row (Index=2)
  • Both labels were docked to "Fill" the row
  • The labels were then set to TextAlign=Center
  • The TableLayoutPanel's Row2 was set to "AutoSize"
  • The TableLayoutPanel's Row3 was set to "AutoSize"

TableLayoutPanel Resizer

Upvotes: 1

mschmidt9026
mschmidt9026

Reputation: 61

Create two separate sets of controls in different forms. Then import them both as composite controls. You can then hide and show as needed using the .visible control property.

Upvotes: 0

Related Questions