mariu5 mariu5 - 2 months ago 5
Vb.net Question

Change location of an object on a panel

I have a form which has two groupboxes. Each groupbox has one main panel with some more controls attached to it. However, when I try to change the location of an object on one of these panels, the object does not move.

How can I change the location of an object which is attached to a panel?

Complete form:

Function Show-GUIUserJoin
{
#---Form

$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(436,350)
$Form.Font = New-Object System.Drawing.Font("Segoe UI",9,0,3,1)
#$Form.Icon = New-Object System.Drawing.Icon($ScriptPP + "Script\Icon.ico")
$Form.FormBorderStyle = "FixedSingle"
$Form.MaximizeBox = $False
$Form.Text = $ScriptTitle
$Form.StartPosition = "CenterScreen"

#---GroupBox Join

$GBUserJoin = New-Object System.Windows.Forms.GroupBox
$GBUserJoin.Location = New-Object System.Drawing.Size(12,5)
$GBUserJoin.Size = New-Object System.Drawing.Size(198,200)
$GBUserJoin.Font = New-Object System.Drawing.Font("Consolas",8,0,3,1)
$GBUserJoin.Text = " -"

#---Panel

$PUserJoin = New-Object System.Windows.Forms.FlowLayoutPanel
$PUserJoin.Location = New-Object System.Drawing.Size(8,16)
$PUserJoin.Size = New-Object System.Drawing.Size(179,250)
$PUserJoin.Font = New-Object System.Drawing.Font("Segoe UI",9,0,3,1)
#$PUserJoin.Enabled = $False

#---Select

#---Radiobutton

$RBSelectJoin = New-Object System.Windows.Forms.RadioButton
$RBSelectJoin.Location = New-Object System.Drawing.Size(12,0)
$RBSelectJoin.Size = New-Object System.Drawing.Size(12,12)

#---Label

$LSelectJoin = New-Object System.Windows.Forms.Label
$LSelectJoin.Location = New-Object System.Drawing.Size(26,-3)
$LSelectJoin.Font = New-Object System.Drawing.Font("Segoe UI",9,0,3,1)
$LSelectJoin.AutoSize = $True
$LSelectJoin.Text = "Eintritt"

#---Template

#---Label

$LTemplate = New-Object System.Windows.Forms.Label
$LTemplate.Location = New-Object System.Drawing.Size(8,20)
$LTemplate.AutoSize = $True
$LTemplate.Text = "Vorlage"

#---Combobox

$CBTemplate = New-Object System.Windows.Forms.ComboBox
$CBTemplate.Location = New-Object System.Drawing.Size(10,41)
$CBTemplate.Size = New-Object System.Drawing.Size(176)
$CBTemplate.FlatStyle = "PopUp"
$CBTemplate.DropDownStyle = 2

#---Work Location

#---Label

$LWorkLocation = New-Object System.Windows.Forms.Label
$LWorkLocation.Location = New-Object System.Drawing.Size(8,250)
$LWorkLocation.AutoSize = $True
$LWorkLocation.Text = "Arbeitsort"

#---Combobox

$CBWorkLocation = New-Object System.Windows.Forms.ComboBox
$CBWorkLocation.Location = New-Object System.Drawing.Size(10,150)
$CBWorkLocation.Size = New-Object System.Drawing.Size(176)
$CBWorkLocation.FlatStyle = "PopUp"
$CBWorkLocation.DropDownStyle = 2

#---GroupBox Resigination

$GBUserResignation = New-Object System.Windows.Forms.GroupBox
$GBUserResignation.Location = New-Object System.Drawing.Size(222,5)
$GBUserResignation.Size = New-Object System.Drawing.Size(197,125)
$GBUserResignation.Font = New-Object System.Drawing.Font("Consolas",8,0,3,1)
$GBUserResignation.Text = " -"

#---Panel

$PUserResignation = New-Object System.Windows.Forms.FlowLayoutPanel
$PUserResignation.Location = New-Object System.Drawing.Size(8,16)
$PUserResignation.Size = New-Object System.Drawing.Size(181,95)
$PUserResignation.Font = New-Object System.Drawing.Font("Segoe UI",9,0,3,1)
#$PUserResignation.Enabled = $False

#---Select

#---Radiobutton

$RBSelectResignation = New-Object System.Windows.Forms.RadioButton
$RBSelectResignation.Location = New-Object System.Drawing.Size(12,0)
$RBSelectResignation.Size = New-Object System.Drawing.Size(12,12)

#---Label

$LSelectResignation = New-Object System.Windows.Forms.Label
$LSelectResignation.Location = New-Object System.Drawing.Size(26,-3)
$LSelectResignation.Font = New-Object System.Drawing.Font("Segoe UI",9,0,3,1)
$LSelectResignation.AutoSize = $True
$LSelectResignation.Text = "Austritt"

#---Coworker

#---Label

$LCW = New-Object System.Windows.Forms.Label
$LCW.Location = New-Object System.Drawing.Size(8,20)
$LCW.AutoSize = $True
$LCW.Text = "Mitarbeiter"

#---Combobox

$CBCW = New-Object System.Windows.Forms.ComboBox
$CBCW.Location = New-Object System.Drawing.Size(10,41)
$CBCW.Size = New-Object System.Drawing.Size(176)
$CBCW.FlatStyle = "PopUp"
$CBCW.DropDownStyle = 2

#---Date of resignation

#---Label

$LDOR = New-Object System.Windows.Forms.Label
$LDOR.Location = New-Object System.Drawing.Size(8,70)
$LDOR.AutoSize = $True
$LDOR.Text = "Austrittsdatum"

#---Date picker

#---Textbox (for displaying choosen date)

$TBDOR = New-Object System.Windows.Forms.TextBox
$TBDOR.Location = New-Object System.Drawing.Size(10,91)
$TBDOR.Size = New-Object System.Drawing.Size(146)
$TBDOR.BorderStyle = "FixedSingle"
$TBDOR.Enabled = $False

#---Picker

$DPDOR = New-Object System.Windows.Forms.Form
$DPDOR.Size = New-Object System.Drawing.Size(176,220)
$DPDOR.Font = New-Object System.Drawing.Font("Segoe UI",9,0,3,1)
$DPDOR.FormBorderStyle = "FixedToolWindow"
$DPDOR.StartPosition = "Manual"
$DPDOR.Topmost = $True
$DPDOR.ControlBox = $False


#---Calendar

$DPDORCal = New-Object System.Windows.Forms.MonthCalendar
$DPDORCal.Location = New-Object System.Drawing.Size(5,5)
$DPDORCal.ShowTodayCircle = $False
$DPDORCal.MaxSelectionCount = 1

#---Buttons

#---Open Date Picker

$BOpenDP = New-Object System.Windows.Forms.Button
$BOpenDP.Location = New-Object System.Drawing.Size(163,91)
$BOpenDP.Size = New-Object System.Drawing.Size(23,23)
$BOpenDP.FlatAppearance.BorderSize = 0
$BOpenDP.FlatStyle = "Flat"
#$BOpenDP.Image = [System.Drawing.Image]::FromFile($ScriptPP + "Images\DatePicker.png")

#---Buttons

#---Form

#---Confirm

$BFormConfirm = New-Object System.Windows.Forms.Button
$BFormConfirm.Location = New-Object System.Drawing.Size(273,250)
$BFormConfirm.Size = New-Object System.Drawing.Size(69,23)
$BFormConfirm.FlatStyle = "PopUp"
$BFormConfirm.Text = "Confirm"
$BFormConfirm.Enabled = $False

#---Cancel

$BFormCancel = New-Object System.Windows.Forms.Button
$BFormCancel.Location = New-Object System.Drawing.Size(350,250)
$BFormCancel.Size = New-Object System.Drawing.Size(69,23)
$BFormCancel.FlatStyle = "PopUp"
$BFormCancel.Text = "Cancel"

#---Date Picker

#---Confirm

$BDPConfirm = New-Object System.Windows.Forms.Button
$BDPConfirm.Location = New-Object System.Drawing.Point(5,190)
$BDPConfirm.Size = New-Object System.Drawing.Size(77,23)
$BDPConfirm.FlatStyle = "PopUp"
$BDPConfirm.Text = "Confirm"

#---Cancel

$BDPCancel = New-Object System.Windows.Forms.Button
$BDPCancel.Location = New-Object System.Drawing.Point(92,190)
$BDPCancel.Size = New-Object System.Drawing.Size(77,23)
$BDPCancel.FlatStyle = "PopUp"
$BDPCancel.Text = "Cancel"

#---EventHandling

#---GroupBox Join

#---Panel

#---Select

#---Radiobutton

$RBSelectJoin_CheckedChanged =
{
if($RBSelectJoin.Checked -eq $True)
{
$RBSelectResignation.Checked = $False

$PUserResignation.Enabled = $False

$PUserJoin.Enabled = $True
}

else
{
$PUserJoin.Enabled = $False
}
}

#---Label

$LSelectJoin_OnClick =
{
$RBSelectJoin.Checked = $True
}

#---GroupBox Resigination

#---Panel

#---Select

#---Radiobutton

$RBSelectResignation_CheckedChanged =
{
if($RBSelectResignation.Checked -eq $True)
{
$RBSelectJoin.Checked = $False

$PUserJoin.Enabled = $False

$PUserResignation.Enabled = $True
}

else
{
$PUserResignation.Enabled = $False
}
}

#---Label

$LSelectResignation_OnClick =
{
$RBSelectResignation.Checked = $True
}

#---Date of resignation

#---Date picker

#---Calendar

$DPDORCal_DateChanged =
{
$TBDOR.Text = $DPDORCal.SelectionStart.ToShortDateString()
}

#---Buttons On-Click Actions

#---GroupBox Resigination

#---Panel

#---Date of resignation

#---Date picker

#---Buttons

#---Open Date Picker

$BOpenDP_OnClick=
{
$TBDOR.Text = $DPDORCal.SelectionStart.ToShortDateString()

$DPDOR.Location = New-Object System.Drawing.Size((3 + $Form.Location.X + $GBUserResignation.Location.X + $TBDOR.Location.X),(21 + $Form.Location.Y + $GBUserResignation.Location.Y + $TBDOR.Location.Y + $TBDOR.Height + 10))

$DPDOR.ShowDialog()
}

#---Form

#---Confirm

$BFormConfirm_OnClick=
{

}

#---Cancel

$BFormCancel_OnClick=
{
$Form.Close()
}

#---Date Picker

#---Confirm

$BDPConfirm_OnClick=
{
$TBDOR.Text = $DPDORCal.SelectionStart.ToShortDateString()

$DPDOR.Close()
}

#---Cancel

$BDPCancel_OnClick=
{
$DPDOR.Close()
}

#---Adding Elements to the form

#---GroupBox Join

$Form.Controls.Add($GBUserJoin)

#---Panel

$GBUserJoin.Controls.Add($PUserJoin)

#---Select

#---Radiobutton

$GBUserJoin.Controls.Add($RBSelectJoin)

$RBSelectJoin.add_CheckedChanged($RBSelectJoin_CheckedChanged)

#---Label

$GBUserJoin.Controls.Add($LSelectJoin)

$LSelectJoin.add_Click($LSelectJoin_OnClick)

#---Template

#---Label

$PUserJoin.Controls.Add($LTemplate)

#---Combobox

$PUserJoin.Controls.Add($CBTemplate)

#---Work Location

#---Label

$PUserJoin.Controls.Add($LWorkLocation)

#---Combobox

$PUserJoin.Controls.Add($CBWorkLocation)

#---GroupBox Resigination

$Form.Controls.Add($GBUserResignation)

#---Panel

$GBUserResignation.Controls.Add($PUserResignation)

#---Select

#---Radiobutton

$GBUserResignation.Controls.Add($RBSelectResignation)

$RBSelectResignation.add_CheckedChanged($RBSelectResignation_CheckedChanged)

#---Label

$GBUserResignation.Controls.Add($LSelectResignation)

$LSelectResignation.add_Click($LSelectResignation_OnClick)

#---Coworker

#---Label

$PUserResignation.Controls.Add($LCW)

#---Combobox

$PUserResignation.Controls.Add($CBCW)

#---Date of resignation

#---Label

$PUserResignation.Controls.Add($LDOR)

#---Date picker

#---Textbox (for displaying choosen date)

$PUserResignation.Controls.Add($TBDOR)

#---Picker

#None

#---Calendar

$DPDORCal.add_DateChanged($DPDORCal_DateChanged)

$DPDOR.Controls.Add($DPDORCal)

#---Buttons

#---Open Date Picker

$PUserResignation.Controls.Add($BOpenDP)

$BOpenDP.add_Click($BOpenDP_OnClick)

#---Buttons

#---Form

#---Confirm

$Form.Controls.Add($BFormConfirm)

$BFormConfirm.add_Click($BFormConfirm_OnClick)

#---Cancel

$Form.Controls.Add($BFormCancel)

$BFormCancel.add_Click($BFormCancel_OnClick)

#---Date Picker

#---Confirm

$DPDOR.Controls.Add($BDPConfirm)

$BDPConfirm.add_Click($BDPConfirm_OnClick)

#---Cancel

$DPDOR.Controls.Add($BDPCancel)

$BDPCancel.add_Click($BDPCancel_OnClick)

#---Show form

$Form.ShowDialog() | Out-Null
}

Show-GUIUserJoin


For example: No matter what value I put in
$CBWorkLocation.Location = New-Object System.Drawing.Size(10,150)
, its location doesn't change.

Am I missing something here?

Answer

You put the UI elements in a FlowLayoutPanel:

$PUserJoin          = New-Object System.Windows.Forms.FlowLayoutPanel
...
$PUserJoin.Controls.Add($CBWorkLocation)

The elements in such panels are aligned automagically:

Represents a panel that dynamically lays out its contents horizontally or vertically.

Don't use flow layouts if you want to place elements freely on a canvas.

Comments