0

I have problem with PictureBox.

When I am docking PictureBox to the top right, it's hidding a part of label which is on the center of form. How I can bring to front label over PictureBox? I am thinking that problem is with docking declaration in both (Image and Label declaration), that's why it's hidden by PictureBox.

How I can do this properly?

Form Declaration:

$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Something"
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
$Form.BackColor = "White"
$Form.Width = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width 
$Form.Height = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height - 50
#$Form.AutoSize = $True
$Form.AutoSizeMode = "GrowAndShrink"
$Form.ControlBox = $false
$Form.MinimumSize = New-Object System.Drawing.Size(1280,1024)
$Form.MaximumSize = New-Object System.Drawing.Size(1920,1080)

Image declaration:

$Image = [system.drawing.image]::FromFile("C:\xxx.png")
$pictureBox = new-object Windows.Forms.PictureBox 
$pictureBox.Dock = [System.Windows.Forms.DockStyle]::Right
$pictureBox.BackColor = "Transparent"
#$pictureBox.Anchor = [System.Windows.Forms.AnchorStyles]::Right
$pictureBox.AutoSize = $True
$pictureBox.Image=$Image
$Form.Controls.Add($pictureBox)

Label declaration:

$redLabel1 = New-Object System.Windows.Forms.Label
$redLabel1.Location = New-Object System.Drawing.Size($Form.Width, $Form.Height)
$redLabel1.AutoSize = $False
$redLabel1.TextAlign = "MiddleCenter"
$redLabel1.Dock = "Fill"
$redLabel1.Text = "Something"
$redLabel1.ForeColor = "Red"
$redLabel1.BackColor = "Transparent"
$Font = New-Object System.Drawing.Font("Arial", 55, [System.Drawing.FontStyle]::Bold)
$redLabel1.Font = $Font
$Form.Controls.Add($redLabel1)

EDIT:

BringToFront() method already tested and working in 50%. Text is not centered in forms, and when label meet PictureBox, text is wrapped.. I would like that label will somehow skip PictureBox..

Screenshot:

enter image description here

Manu
  • 1,685
  • 11
  • 27
Frihu
  • 43
  • 9
  • Add to your question what you have tried (`BringToFront()` or others methods) – Manu Nov 08 '17 at 09:10
  • Okay, I used this BringToFront() in wrong place.. After adding label Control it's working, but Label is not centered in form.. It's wrapped when it's meet picture box, and I would like to overwrite on pictureBox, to have it centered.. So it's seems that BringToFront fixed my problem in 50%.. – Frihu Nov 08 '17 at 09:39
  • Could you show what it looks like with a screenshot? – Manu Nov 08 '17 at 09:44
  • Added in question at the end. – Frihu Nov 08 '17 at 09:51
  • Can you try this : `$redLabel1.WordWrap = $false`. It also should depend on the witdh of your label. – Manu Nov 08 '17 at 09:56
  • Does not work. Script don't work with this. – Frihu Nov 08 '17 at 10:00
  • Do you use anchors to expand your label to the right side of the form? With this screenshot we can't see what is the border of the label. – Manu Nov 08 '17 at 10:02
  • I use only this for label what is upper in question. I use fill in dock. – Frihu Nov 08 '17 at 10:03
  • So you have to play with the width of the dock as it is the container of the label – Manu Nov 08 '17 at 10:03
  • You mean to assign form.width for dock? – Frihu Nov 08 '17 at 10:10
  • Yes so the dock could expand to the form width. Be also sure that the label will expand too in the dock – Manu Nov 08 '17 at 10:12
  • I cannot manage this idea with width of form.. Also when I am put `Dock.Top` text is OK centered, picture does not bother but picture default is on the left, and I would like to have it on the right on Top.. – Frihu Nov 08 '17 at 10:59
  • There are 2 differents questions here, the one I answered was the `BringToFront()`method solution (you can mark it as accepted) and now the problem you have is the position of your different controls. You should open a new question for that. – Manu Nov 08 '17 at 12:42
  • @Manu thank you for help here, Below link to another question... https://stackoverflow.com/questions/47180536/label-and-picturebox-position-collision – Frihu Nov 08 '17 at 13:07

2 Answers2

2

I am quiet sure that your label isn't hidden by the picture, but just put aside this depends on the Z-Order of the image - and the label control (which doesn't show from the lose controls in the example):

  • If you fill dock the first control and than right dock the second control, the first control will just fill the available the space that is leftover.
  • If you right dock the first control and than fill dock second control. The second control will take all the space behind the first control

I have quickly modified my Windows-Form wrapper example, to show this:

$Form    = Form-Control Form @{Text = "Dock test"; StartPosition = "CenterScreen"; Padding = 4}
$Table   = $Form  | Form TableLayoutPanel @{RowCount = 2; ColumnCount = 3; ColumnStyles = ("Percent", 50), "AutoSize", "AutoSize"; Dock = "Fill"}
$Panel   = $Table | Form Panel @{Dock = "Fill"; BorderStyle = "FixedSingle"; BackColor = "Teal"} -Set @{RowSpan = 2}
$Dock = ForEach ($i in 1..2) {
    $Button = $Panel | Form Button @{Location = "25, $(75 * $i - 50)";  Size = "50, 50"; BackColor = "Silver"; Enabled = $False; Text = $i}
    $Group  = $Table | Form GroupBox @{Text = "Dock $i"; AutoSize = $True}
    $Flow   = $Group | Form FlowLayoutPanel @{AutoSize = $True; FlowDirection = "TopDown"; Dock = "Fill"; Padding = 4}
    $Radio  = "None", "Top", "Left", "Bottom", "Right", "Fill" | ForEach {
        $Flow | Form RadioButton @{Text = $_; AutoSize = $True; Click = ([ScriptBlock]::Create("`$Dock[$($i - 1)].Button.Dock = `$This.Text"))}
    }
    New-Object PSObject -Property @{Button = $Button; Group = $Group; Flow = $Flow; Radio = $Radio}
}
$Close  = $Table | Form Button @{Text = "Close"; Dock = "Bottom"; Click = {$Form.Close()}} -Set @{ColumnSpan = 2}
$Form.ShowDialog()

Just a few examples:

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

Possible Solutions:

iRon
  • 20,463
  • 10
  • 53
  • 79
  • When I am using Anchor, label is disappearing and I don't know why... I cannot position it, because I would like that content will auto-resize when I am resizing window. Using another docking combination it's not solving my problem, because Label should be centered in the window, and on the bottom I would like to have button with some event. – Frihu Nov 09 '17 at 06:32
  • Thank you for all answers and help. I did it. For `pictureBox` I used Anchoring to "Top, Right" with Location related to Form width and height. For `label` I used docking also with specified location. I removed `BringToFront` and `SendToBack`. – Frihu Nov 09 '17 at 06:53
0

There is a method called Control.BringToFront : https://msdn.microsoft.com/fr-fr/library/system.windows.forms.control.bringtofront(v=vs.110).aspx

So it should be in your code :

$redLabel1.BringToFront()
Manu
  • 1,685
  • 11
  • 27