Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8985707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:23:03+00:00 2026-06-15T21:23:03+00:00

Hi i am trying to create a dynamic form in pwoershell , this is

  • 0

Hi i am trying to create a dynamic form in pwoershell , this is a form which has 5 buttons(color names) and each button opens a different text file(such as if red button is clicked,it should open red.txt; here is the full code;

Script Start

$var = "Red","Blue","Yellow","Black","White"


$testForm = New-Object System.Windows.Forms.Form
$testForm.Text = "Color List"
$testForm.AutoSize = $True
$testForm.AutoSizeMode = "GrowAndShrink"     

$Font = New-Object System.Drawing.Font("Times New Roman",24, [System.Drawing.FontStyle]::Bold)   
$testForm.Font = $Font


$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Select the Color"
$Label.AutoSize = $True
$testForm.Controls.Add($Label)


$x=100
$y=50
foreach($color in $var)
{
    $run = New-Object System.Windows.Forms.Button
    $run.Location = New-Object System.Drawing.Size($x,$y)
    $run.Size = New-Object System.Drawing.Size(100,50)
    $run.Text = "$Color"
    $run.Add_Click({ Invoke-Expression "notepad C:\Users\User\$color.txt" })
    $testForm.Controls.Add($run)
    $Font = New-Object System.Drawing.Font("Times New Roman",14,[System.Drawing.FontStyle]::Regular)
    $run.font = $Font
    $run.AutoSize = $True
    $y+=50

}
$testForm.ShowDialog()

END Script

Everything went fine until, when the form opens with buttons, and all the buttons when clicked, open the file “White.txt” since its the last element in the array; is there any way to change the script to make each button open only their respective files and not the last color file?

please do let me know if any further questions or clarifications needed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:23:04+00:00Added an answer on June 15, 2026 at 9:23 pm

    Looks like the problem is the Add_Click line. The script block contains a link to the variable $color, rather than evaluating it immediately to create a new “notepad…” string for Invoke-Expression and linking to that. The string for Invoke-Expression will be created when the button is clicked. By this time $color is White ‘cos the loop has finished, so all the buttons end up creating a string using White.

    You can fix it with a call to GetNewClosure() which will build cause the string for the Invoke-Expression to be created during the loop, not later on when the button is clicked. So change the line to:

    $run.Add_Click({ Invoke-Expression "notepad C:\Users\User\$color.txt" }.GetNewClosure())
    

    And it should work as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a simple dynamic gallery with a radio button under each
I am trying to create a dynamic form so on click of a button
I have form which contains more (dynamic) wrap-segments, I am trying to create object
I'm trying to create a dynamic form builder. Therefore PHP gets a set of
I am trying to create dynamic controls (Label and combobox) in a WPF form,
I am trying to create a dynamic total generator, which adds a value to
I'm trying to create a dynamic survey form based on dynamic data by using
So I am trying to create a dynamic form where the 2nd dropdown box
Trying to learn how to create a dynamic html form. The elements would be
So here is the problem: I am trying to create dynamic buttons that have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.