locked
Powershell GUI - Adding and Removing Controls RRS feed

  • Question

  • I have created some powershell tools with a GUI. I am trying to combine all the Tools into one GUI so that you can use a menu to switch between the different tools.

    Just trying to test this by adding and removing Labels depending on the Menu item selected. This is what I have so far but can't seem to get the Remove to work:

    ###################Load Assembly for creating form & button######
    
    [void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
    [void][System.Reflection.Assembly]::LoadWithPartialName( “System.drawing.systemcolours”)
    [void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
    Add-Type -AssemblyName PresentationCore,PresentationFramework
    #####Define the form size & placement
    
    $form = New-Object “System.Windows.Forms.Form”;
    $form.Width = 550;
    $form.Height = 350;
    $form.Text = "Test";
    $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
    
    #####Define Menu and items
    
    $mainToolStrip = New-Object "System.Windows.Forms.ToolStrip";
    $form.Controls.Add($mainToolStrip);
    
    
    $Test1 = New-Object "System.Windows.Forms.ToolStripbutton";
    $mainToolStrip.Items.Add($Test1);
    $Test1.Text = "Test 1";
    
    
    $Test2 = New-Object "System.Windows.Forms.ToolStripMenuItem";
    $mainToolStrip.Items.Add($Test2);
    $Test2.Text = "Test 2";
    
    
    $Clear = New-Object "System.Windows.Forms.ToolStripMenuItem";
    $mainToolStrip.Items.Add($clear);
    $clear.Text = "Clear";
    
    
    
    
    $buttoneventHandlerTest1 = [System.EventHandler]{
    ##############Define text label1
    $textLabel1 = New-Object “System.Windows.Forms.Label”;
    $textLabel1.Left = 25;
    $textLabel1.Top = 135;
    $textLabel1.AutoSize = $True;
    $textLabel1.Text = "This is a Test";
    $form.Controls.Add($textLabel1);
    $form.Controls.Remove($textLabel2);
    $form.Refresh();
    }
    
    
    $buttoneventHandlerTest2 = [System.EventHandler]{
    ##############Define text label2
    $textLabel2 = New-Object “System.Windows.Forms.Label”;
    $textLabel2.Left = 25;
    $textLabel2.Top = 100;
    $textLabel2.AutoSize = $True;
    $textLabel2.Text = "This is a Test 2";
    $form.Controls.Add($textLabel2);
    $form.Controls.Remove($textLabel1);
    $form.Refresh();
    }
    
    $buttoneventHandlerclear = [System.EventHandler]{
    $form.Controls.Remove($textLabel2);
    $form.Controls.Remove($textLabel3);
    $form.Refresh();
    Write-host "Clearing"
    }
    
    
    ##### Action Buttons
    
    $Test1.Add_Click($buttoneventHandlerTest1) ;
    $Test2.Add_Click($buttoneventHandlerTest2) ;
    $Clear.Add_Click($buttoneventHandlerclear) ;
    $form.ShowDialog()

       or is there a better way to do this?
    Tuesday, January 30, 2018 6:15 AM

Answers

  • Is this what you are trying to do?

    Add-Type -AssemblyName System.Windows.Forms
    
    #region Form
    $form = New-Object System.Windows.Forms.Form
    $form.Size = '550,350'
    $form.Text = "Test"
    $form.StartPosition = 'CenterScreen'
    #endregion Form
    
    #region ToolStrip
    $mainToolStrip = New-Object System.Windows.Forms.ToolStrip
    $form.Controls.Add($mainToolStrip);
    
    $Test1 = New-Object System.Windows.Forms.ToolStripbutton
    $mainToolStrip.Items.Add($Test1);
    $Test1.Text = 'Test 1'
    
    $Test2 = New-Object System.Windows.Forms.ToolStripbutton
    $mainToolStrip.Items.Add($Test2)
    $Test2.Text = 'Test 2'
    
    $Clear = New-Object System.Windows.Forms.ToolStripbutton
    $mainToolStrip.Items.Add($clear);
    $clear.Text = 'Clear'
    #endregion ToolStrip
    
    #region Labels
    $textLabel1 = New-Object System.Windows.Forms.Label
    $textLabel1.Location = '25,100'
    $textLabel1.AutoSize = $true
    $textLabel1.Text = 'This is a Test'
    
    $textLabel2 = New-Object System.Windows.Forms.Label
    $textLabel2.Location = '25,100'
    $textLabel2.AutoSize = $true
    $textLabel2.Text = 'This is a Test 2'
    #endregion Labels
    
    #region Event Code
    $Test1.Add_Click({
    	$form.Controls.Add($textLabel1);
    	$form.Controls.Remove($textLabel2);
    })
    
    $Test2.Add_Click({
    	$form.Controls.Add($textLabel2)
    	$form.Controls.Remove($textLabel1)
    })
    
    $Clear.Add_Click({
    	$form.Controls.Remove($textLabel1);
    	$form.Controls.Remove($textLabel2);
    })
    #endregion Event Code
    
    $form.ShowDialog()

    Simplify.  Don't use lines of code that are not needed and don't overcode ot try to copy c# methods.

    Clean looking code always works better.  Don't comment the obvious.


    \_(ツ)_/





    • Edited by jrv Tuesday, January 30, 2018 6:49 AM
    • Marked as answer by JayDee295 Tuesday, January 30, 2018 7:00 AM
    Tuesday, January 30, 2018 6:42 AM

All replies

  • the problem with your code is, $textLabel1 and $textLabel2 are defined in respective script blocks level (event handlers here) and that's why they are not accessible outside their event handlers. Quick solution is to move the declaration of these two objects outside of event handlers script blocks.

    I would personally define all these objects outside and show/hide them in menu events rather than re-creating them on every menu click.


    cheers..uc
    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Tuesday, January 30, 2018 6:38 AM
  • Is this what you are trying to do?

    Add-Type -AssemblyName System.Windows.Forms
    
    #region Form
    $form = New-Object System.Windows.Forms.Form
    $form.Size = '550,350'
    $form.Text = "Test"
    $form.StartPosition = 'CenterScreen'
    #endregion Form
    
    #region ToolStrip
    $mainToolStrip = New-Object System.Windows.Forms.ToolStrip
    $form.Controls.Add($mainToolStrip);
    
    $Test1 = New-Object System.Windows.Forms.ToolStripbutton
    $mainToolStrip.Items.Add($Test1);
    $Test1.Text = 'Test 1'
    
    $Test2 = New-Object System.Windows.Forms.ToolStripbutton
    $mainToolStrip.Items.Add($Test2)
    $Test2.Text = 'Test 2'
    
    $Clear = New-Object System.Windows.Forms.ToolStripbutton
    $mainToolStrip.Items.Add($clear);
    $clear.Text = 'Clear'
    #endregion ToolStrip
    
    #region Labels
    $textLabel1 = New-Object System.Windows.Forms.Label
    $textLabel1.Location = '25,100'
    $textLabel1.AutoSize = $true
    $textLabel1.Text = 'This is a Test'
    
    $textLabel2 = New-Object System.Windows.Forms.Label
    $textLabel2.Location = '25,100'
    $textLabel2.AutoSize = $true
    $textLabel2.Text = 'This is a Test 2'
    #endregion Labels
    
    #region Event Code
    $Test1.Add_Click({
    	$form.Controls.Add($textLabel1);
    	$form.Controls.Remove($textLabel2);
    })
    
    $Test2.Add_Click({
    	$form.Controls.Add($textLabel2)
    	$form.Controls.Remove($textLabel1)
    })
    
    $Clear.Add_Click({
    	$form.Controls.Remove($textLabel1);
    	$form.Controls.Remove($textLabel2);
    })
    #endregion Event Code
    
    $form.ShowDialog()

    Simplify.  Don't use lines of code that are not needed and don't overcode ot try to copy c# methods.

    Clean looking code always works better.  Don't comment the obvious.


    \_(ツ)_/





    • Edited by jrv Tuesday, January 30, 2018 6:49 AM
    • Marked as answer by JayDee295 Tuesday, January 30, 2018 7:00 AM
    Tuesday, January 30, 2018 6:42 AM
  • Perfect! I am just starting out with learning how to script. Thanks for the advise!!
    Tuesday, January 30, 2018 7:02 AM
  • Ah this makes sense. I thought I was doing something wrong. Still very fresh to powershell and Forms. Thanks heaps for the tips.
    Tuesday, January 30, 2018 7:05 AM
  • You should comment everything in your script! This way if you have to go back and modify the code or update, You wont have work through the code again.  
    Tuesday, April 24, 2018 7:58 PM
  • You should comment everything in your script! This way if you have to go back and modify the code or update, You wont have work through the code again.  

    In this case the code is self commenting.  Style guidelines restrict over-commenting as this causes visual confusion.  Comments should be used only where the code is unclear or there are special conditions.

    The following kind of comment is frowned on:

    # add 1 to $i
    $i + 1

    Learning how to properly comment takes experience and good coding skills.

    See: PowerShell Style Guidelines


    \_(ツ)_/

    Tuesday, April 24, 2018 8:13 PM
  • Notice how is says 'Guidelines' and not 'Rules'.  You are not the say all in Powershell coding sorry. Comment are to help understand the script correct?  if someone is starting out with POowershell creating a script. THen it would be helpful to comment eveything. IT helped me. You act like you are the Powershell Guru of this site.  ridiculous. 

    "Comments should be used only where the code is unclear or there are special conditions."   - IF someone is new to powershell, then it safe to say the alot of lines will be unclear.  You post crap now just post and get the last word.   Just stop.

    Thursday, April 26, 2018 5:53 PM
  • Notice how is says 'Guidelines' and not 'Rules'.  You are not the say all in Powershell coding sorry. Comment are to help understand the script correct?  if someone is starting out with POowershell creating a script. THen it would be helpful to comment eveything. IT helped me. You act like you are the Powershell Guru of this site.  ridiculous. 

    "Comments should be used only where the code is unclear or there are special conditions."   - IF someone is new to powershell, then it safe to say the alot of lines will be unclear.  You post crap now just post and get the last word.   Just stop.

    I think you fail to understand the role of comments in programming.  Perhaps a little research would help you to better understand my advice.

    Yes you can comment anything that doesn't move however, even though it helps you, it will likely make code harder for others to understand.  Learning how to code and how to use the "self-documenting" capability of all modern languages is far more useful than just writing a lot of comments.


    \_(ツ)_/

    Thursday, April 26, 2018 6:29 PM
  • You should comment everything in your script! This way if you have to go back and modify the code or update, You wont have work through the code again.  

    In this case the code is self commenting.  Style guidelines restrict over-commenting as this causes visual confusion.  Comments should be used only where the code is unclear or there are special conditions.

    The following kind of comment is frowned on:

    # add 1 to $i
    $i + 1

    Learning how to properly comment takes experience and good coding skills.

    See: PowerShell Style Guidelines


    \_(ツ)_/

    This is both true and false.

    Your comment, in this case, is frowned on.  However, consider a similar comment:

    # Increment the loop variable to show the number of times this process has run. 
    $i + 1

    The point of comments in your code is more to remind (or explain) what you are doing and why, then to mimic the commands being done.

    Your comment suggestion of "# Add 1 to $I" is self commenting, but details of why you would be doing it in code, or even what $i is supposed to represent are not.  

    Therefore, this really could be considered code that is in extreme need of comments, and not the other way around.


    Tuesday, August 7, 2018 2:02 PM
  • Again. to the moderator:

    Before you preach about the role of comments, take into account that there are time in code that you may not remember WHY you added 1 to $i (as was your example)

    No one argues that a comment:

    # Add 1 to value $i

    is not needed as such a comment adds little value.

    # Add 1 to value $i so that later in execution, we can confirm this step took place.

    However is very much a valid comment, and one that EVERYONE should be encourage to add to their code.

    As a somewhat seasoned developer, I have had to edit code others have written and find that I would rather have comments that need to be ignored, then have to wonder what $i stands for, and to have to figure out why the original programmer decided it was time to add 1 to it.

    I think discouraging ANYONE from adding comments to any code, especially a new-bie, is wrong and shows a disregard for the skills of others.

    Encourage comments, but help others make them of value.  Code never self documents WHY something is being done, or why the choice of doing something a specific way was made.

    Tuesday, August 7, 2018 2:10 PM
  • Encourage comments, but help others make them of value.  Code never self documents WHY something is being done, or why the choice of doing something a specific way was made.

    Before you go off looking to prove a negative start by reading the following:

    PowerShell Style Guidelines


    \_(ツ)_/

    Tuesday, August 7, 2018 2:32 PM