Reputation: 391
i m trying to draw windows logo on form through powershell. following code will put only one dot on form. whats wrong with it?
$labels = @(0)*5
for ($i=0;$i -lt 4;$i++)
{ $labels[$i] = new-object system.window.forms.label
$labels[$i].location = new-object system.drawing.point($i+10,5)
$labels[$i].text = $i.tostring()
$main_form.controls.add($labels[$i])
}
}
$main_form.showdialog()
the ouput is just one dot on form. changing text value to say "a" prints only one a.
Upvotes: 0
Views: 1175
Reputation: 10075
You've got your x and y coordinates the wrong way round - Controls.Add
takes x
then y
, but you're putting all your controls on the same y
coordinate, and your x offset for each control is 1 pixel (you might have meant $i * 10
instead of $i + 10
) so they're all overlapping each other.
There's also a bunch of typos - e.g. system.window.forms
- window
instead of windows
, and new-object system.drawing.point($i+10,5)
doesn't even work (it gives a Method invocation failed because [System.Object[]] does not contain a method named 'op_Addition'.
error). It's worth spending a bit of time testing the code you post before submitting it, even to the point of cutting and pasting it from your question to make sure it actually runs because you're more likely to get a response from someone!
In any case, the following works for me:
Add-Type -AssemblyName "System.Windows.Forms";
Add-Type -AssemblyName "System.Drawing";
$main_form = new-object System.Windows.Forms.Form;
$labels = @();
for( $i=0; $i -lt 5; $i++ )
{
$label = new-object System.Windows.Forms.Label;
$label.BackColor = "Orange";
$label.Location = new-object System.Drawing.Point(10, ($i * 25));
$label.Text = $i.ToString();
$labels += $label;
$main_form.Controls.Add($label);
}
$main_form.ShowDialog();
which shows a form like this:
Feel free to adapt this to your needs.
Upvotes: 2