TabControl and TabPages

by tim91700 at 2012-12-10 05:24:52

Hello,

I have a database with 2 machines

I want to create tabs like this :
-Index Tab
-Tab for machine 1
-Tab for machine 2

I have :
-Index Tab
-Tab for machine 2
-Tab for machine 2

My code :
$tabScanner.text = "Scanner"
$tabScanner.AutoSize = $true
$tabScanner.Controls.Add($boutonLancerScan)
$tabScanner.Controls.Add($boutonStopScan)
$tabScanner.Controls.Add($dataGridView1)

#It's not complete
$tabModele.autosize = $true


$tabs.Width = 550
$tabs.Height = 280
$tabs.Top = 25
$tabs.left = 5
$tabs.TabPages.Add($tabScanner)

$GLOBAL:taba = @(1,2)
Foreach ($GLOBAL:ta in $GLOBAL:taba)
{
$tabModele.Text = $ta
$tabModele.AutoSize = $true
$tabs.TabPages.Add($tabModele)
}

$form1.Controls.Add($tabs)


Thanks you for your help
by nohandle at 2012-12-10 11:43:29
$GLOBAL:ta why do you use the global scoping here? Try it without it. Edit: This should not affect how the script works, it just seems illogical in this case.

the varible names are really confusing, at least for me.
by tim91700 at 2012-12-10 23:03:50
Global is normal, it’s to have the variable listed on the column.

The name isn’t important, I want to understand why I have two ‘2’ for tabs name and not ‘1’ and ‘2’.
by nohandle at 2012-12-11 02:55:16
[quote="tim91700"]Global is normal, it’s to have the variable listed on the column.[/quote]
What seem strange is if you do foreach ($item in $collectionOfItems)
{
the item variable should be used here only because it is created for the context of this command
hence it seems strange to mark the variable as global - possibly making the script more complicated.
}
[quote="tim91700"]
The name isn’t important, I want to understand why I have two ‘2’ for tabs name and not ‘1’ and ‘2’.[/quote]
Ok, but to get there I have to be able to understand the code first, which is quite hard for me at the moment.

I suppose if you set tabmodele text property to 1 and add it to the collection only reference to the original object is passed. You iterate and set tabmodele text property to 2, the change is done on the original object because it is referenced only. You add it to the collection, and again only a reference to the object is passed to the collection.

You now have what seems like two tabs but in fact the two tabs are only shortcuts to one tab object. To make it work you have to:
create the object in the foreach again and again,
or create a base object and clone it (.clone() method) when you add it
or create a function (something like constructor) that creates the object and passes it as output - so you don’t have to care about freeing the base object after you are done.
by tim91700 at 2012-12-11 04:12:06
Hello,

Thanks for your solution :
$tabs.Width = 550
$tabs.Height = 390
$tabs.Top = 25
$tabs.left = 5
#$tabScanner is the Index tab, It's will never change
$tabs.TabPages.Add($tabScanner)

$GLOBAL:tabMachine = @("toto","tata")
for ($i=0; $i -lt $GLOBAL:tabMachine.Length; $i++)
{
$tabModele = New-Object System.Windows.Forms.TabPage
$tabModele.Text = $tabMachine[$i]
$tabModele.AutoSize = $true
$tabModele.autosize = $true
$tabModele.Visible = $true
# I have 13 contros to add, but I want to understand first
$tabModele.Controls.Add($labelComboDateScan)
$tabs.TabPages.Add($tabModele)
}
$form1.Controls.Add($tabs)


Now, I have one problem :

On my script, the last item on the tab $tabMachine have the control, not the rest.

Any idea ?
by nohandle at 2012-12-11 06:07:44
[quote="tim91700"]On my script, the last item on the tab $tabMachine have the control, not the rest. [/quote]
Sorry I don’t understand what you are trying to say. Can you rephrase please?
by tim91700 at 2012-12-11 06:14:08
If on $tabmachine there is this : $tabMachine=("test","TEST",Admin)

On the GUI, I’ll have these tabs : Scanner(1), test(2), TEST(3), Admin(4)

On the (1), all is ok.
On the (2), all is empty
On the (3), all is empty
On the (4), I have the label
by nohandle at 2012-12-11 06:35:49
Franky, I am lost in your code.
Do you refer to this:
$tabModele.Controls.Add($labelComboDateScan)
Is the variable $labelComboDateScan supposed to contain the controls?
Isn’t this the same case (with the reference) as in the previous case?
by tim91700 at 2012-12-11 06:48:05
You have always right, it’s works !

Thanks you for your time