Help why the windows is closed but i see meesag

Hi all ,

i created this code ``“IP Address” {
$wifiButton = New-Object System.Windows.Forms.Button
$wifiButton.Text = “Wi-Fi”
$wifiButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$wifiButton.Top = 30
$wifiButton.Left = 20
$wifiButton.Padding = New-Object System.Windows.Forms.Padding(10, 0, 0, 0)

$lanButton = New-Object System.Windows.Forms.Button
$lanButton.Text = "LAN" 
$lanButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$lanButton.Top = 30
$lanButton.Left = 120
$lanButton.Padding = New-Object System.Windows.Forms.Padding(10, 0, 0, 0)

$form = New-Object System.Windows.Forms.Form
$form.Text = "Select IP Address"
$form.Controls.Add($wifiButton)
$form.Controls.Add($lanButton)
$form.ClientSize = New-Object System.Drawing.Size(250, 100)
$form.StartPosition = "CenterScreen"
$form.Topmost = $true

$result = $form.ShowDialog()

$form.Add_Closing({
    $this.DialogResult = $result
})

if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
    $ip = (Get-NetAdapter | Where-Object { $_.Name -like "Wi-Fi*" } | Get-NetIPAddress -AddressFamily IPv4).IPAddress
    if ($ip) {
        [System.Windows.Forms.MessageBox]::Show("Your Wi-Fi IP address is: $ip")
    }
} elseif ($result -eq [System.Windows.Forms.DialogResult]::Cancel) {
    $ip = (Get-NetAdapter | Where-Object { $_.Name -like "Ethernet*" } | Get-NetIPAddress -AddressFamily IPv4).IPAddress
    if ($ip) {
        [System.Windows.Forms.MessageBox]::Show("Your LAN IP address is: $ip")
    }
}

}``
but once i close the second windows i can see still the ip address message
how can i fix it?

thanks?

Your code came through kind of wonky.
Is it supposed to start at $wifibutton or “IP Address”?
Because IP address and the initial curly bracket and end curly bracket look out of place.

sorry this the code its starting with ip address

"IP Address" {
    $wifiButton = New-Object System.Windows.Forms.Button
    $wifiButton.Text = "Wi-Fi" 
    $wifiButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $wifiButton.Top = 30
    $wifiButton.Left = 20
    $wifiButton.Padding = New-Object System.Windows.Forms.Padding(10, 0, 0, 0)

    $lanButton = New-Object System.Windows.Forms.Button
    $lanButton.Text = "LAN" 
    $lanButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $lanButton.Top = 30
    $lanButton.Left = 120
    $lanButton.Padding = New-Object System.Windows.Forms.Padding(10, 0, 0, 0)

    $form = New-Object System.Windows.Forms.Form
    $form.Text = "Select IP Address"
    $form.Controls.Add($wifiButton)
    $form.Controls.Add($lanButton)
    $form.ClientSize = New-Object System.Drawing.Size(250, 100)
    $form.StartPosition = "CenterScreen"
    $form.Topmost = $true

    $result = $form.ShowDialog()

    if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
        $ip = (Get-NetAdapter | Where-Object { $_.Name -like "Wi-Fi*" } | Get-NetIPAddress -AddressFamily IPv4).IPAddress
        [System.Windows.Forms.MessageBox]::Show("Your Wi-Fi IP address is: $ip")
    } elseif ($result -eq [System.Windows.Forms.DialogResult]::Cancel) {
        $ip = (Get-NetAdapter | Where-Object { $_.Name -like "Ethernet*" } | Get-NetIPAddress -AddressFamily IPv4).IPAddress
        [System.Windows.Forms.MessageBox]::Show("Your LAN IP address is: $ip")
    }
}

I’m not sure what the “IP Address” is at the front.
If I change that to function IPAddress, leave the rest as is, and just run IPAddress after committing it to session, it works as expected for me.

Alternatively, just leaving the “IP Address” open curly and end curly out, it works as well.

this is the full script

Add-Type -AssemblyName System.Windows.Forms

# Create a form
$form = New-Object System.Windows.Forms.Form
$form.Text = "System Information"
$form.Size = New-Object System.Drawing.Size(500, 120)
$form.StartPosition = "CenterScreen"

# Create labels and textboxes for user input
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10, 10)
$label.Size = New-Object System.Drawing.Size(200, 20)
$label.Text = "Select an option:"
$form.Controls.Add($label)

$dropdown = New-Object System.Windows.Forms.ComboBox
$dropdown.Location = New-Object System.Drawing.Point(220, 10)
$dropdown.Size = New-Object System.Drawing.Size(150, 10)
$dropdown.Items.AddRange(("Hostname", "IP Address", "Serial Number", "MAC Address", "System Uptime", "Disk Space", "Installed Software"))
$form.Controls.Add($dropdown)

# Add buttons to the form
$button1 = New-Object System.Windows.Forms.Button
$button1.Location = New-Object System.Drawing.Point(10, 50)
$button1.Size = New-Object System.Drawing.Size(100, 30)
$button1.Text = "Get Info"
$form.Controls.Add($button1)

$button2 = New-Object System.Windows.Forms.Button
$button2.Location = New-Object System.Drawing.Point(120, 50)
$button2.Size = New-Object System.Drawing.Size(100, 30)
$button2.Text = "Exit"
$form.Controls.Add($button2)

# Add event handlers for the buttons
$button1.Add_Click({
    $option = $dropdown.SelectedItem.ToString()
    switch ($option) {
        "Hostname" {
            $hostname = (Get-WmiObject Win32_ComputerSystem).Name
            [System.Windows.Forms.MessageBox]::Show("Your hostname is: $hostname")
        }
"IP Address" {
    $wifiButton = New-Object System.Windows.Forms.Button
    $wifiButton.Text = "Wi-Fi" 
    $wifiButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $wifiButton.Top = 30
    $wifiButton.Left = 20
    $wifiButton.Padding = New-Object System.Windows.Forms.Padding(10, 0, 0, 0)

    $lanButton = New-Object System.Windows.Forms.Button
    $lanButton.Text = "LAN" 
    $lanButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $lanButton.Top = 30
    $lanButton.Left = 120
    $lanButton.Padding = New-Object System.Windows.Forms.Padding(10, 0, 0, 0)

    $form = New-Object System.Windows.Forms.Form
    $form.Text = "Select IP Address"
    $form.Controls.Add($wifiButton)
    $form.Controls.Add($lanButton)
    $form.ClientSize = New-Object System.Drawing.Size(250, 100)
    $form.StartPosition = "CenterScreen"
    $form.Topmost = $true

    $result = $form.ShowDialog()

    if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
        $ip = (Get-NetAdapter | Where-Object { $_.Name -like "Wi-Fi*" } | Get-NetIPAddress -AddressFamily IPv4).IPAddress
        [System.Windows.Forms.MessageBox]::Show("Your Wi-Fi IP address is: $ip")
    } elseif ($result -eq [System.Windows.Forms.DialogResult]::Cancel) {
        $ip = (Get-NetAdapter | Where-Object { $_.Name -like "Ethernet*" } | Get-NetIPAddress -AddressFamily IPv4).IPAddress
        [System.Windows.Forms.MessageBox]::Show("Your LAN IP address is: $ip")
    }
}

"Serial Number" {
$serial = (Get-WmiObject -Class Win32_ComputerSystemProduct).IdentifyingNumber
[System.Windows.Forms.MessageBox]::Show("Your serial number is: $serial")
}
"MAC Address" {
$wifiButton = New-Object System.Windows.Forms.Button
$wifiButton.Text = "Wi-Fi"
$wifiButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$wifiButton.Top = 30
$wifiButton.Left = 50

$lanButton = New-Object System.Windows.Forms.Button
$lanButton.Text = "LAN"
$lanButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$lanButton.Top = 30
$lanButton.Left = 150

$form = New-Object System.Windows.Forms.Form
$form.Text = "Select MAC Address"
$form.Controls.Add($wifiButton)
$form.Controls.Add($lanButton)
$form.ClientSize = New-Object System.Drawing.Size(300, 100)
$form.StartPosition = "CenterScreen"
$form.Topmost = $true

$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
    $mac = (Get-NetAdapter | Where-Object { $_.Name -like "Wi-Fi*" }).MacAddress
    [System.Windows.Forms.MessageBox]::Show("Your Wi-Fi MAC address is: $mac")
} elseif ($result -eq [System.Windows.Forms.DialogResult]::Cancel) {
    $mac = (Get-NetAdapter | Where-Object { $_.Name -like "Ethernet*" }).MacAddress
    [System.Windows.Forms.MessageBox]::Show("Your LAN MAC address is: $mac")
}
}
"System Uptime" {
    $bootTime = (Get-WmiObject Win32_OperatingSystem).LastBootUpTime
    $bootTime = [System.Management.ManagementDateTimeConverter]::ToDateTime($bootTime)

    $now = Get-Date
    $uptime = $now - $bootTime

    $uptime = $uptime.Days.ToString() + " days, " + $uptime.Hours.ToString() + " hours, " + $uptime.Minutes.ToString() + " minutes, " + $uptime.Seconds.ToString() + " seconds"
    [System.Windows.Forms.MessageBox]::Show("Your system uptime is: $uptime")
}

"Disk Space" {
$diskspace = Get-WmiObject Win32_LogicalDisk | Select-Object DeviceID, @{Name="Size (GB)";Expression={$_.Size/1GB -as [int]}}, @{Name="FreeSpace (GB)";Expression={$_.FreeSpace/1GB -as [int]}}
$message = "Your available disk space is:`r`n"
foreach ($disk in $diskspace) {
    $message += "Drive $($disk.DeviceID): $($disk.'FreeSpace (GB)') GB free out of $($disk.'Size (GB)') GB`r`n"
}
[System.Windows.Forms.MessageBox]::Show($message)
}

"Installed Software" {
$installedSoftware = Get-WmiObject -Class Win32_Product | Select-Object Name, Version
$softwareList = "The installed software on your system is:`r`n`r`n"
foreach ($software in $installedSoftware) {
    $name = $software.Name
    $version = $software.Version
    $softwareList += "$name $version`r`n"
}
[System.Windows.Forms.MessageBox]::Show($softwareList)
}
}
})

$button2.Add_Click({ $form.Close() })
$form.Add_Shown({ $dropdown.Select() })
[void] $form.ShowDialog()

when you select ip adress it asking you if you want to see ip adreess of wifi or lan if you not select any option and just click on X its still showing you ip address and i dont understand why?

Maybe a cached issue or an unclosed form from another session?
It looks like everything works for me.

when you close the second form do you see any messaage?

Just goes back to main form.

i see message that your ip is i dont understnad why

I re-read your issue, and now I understand and can replicate.
I took it as after you showed the IP, closed the box, it still showed up. But the issue comes when you don’t select anything, it still shows up.

There’s nothing to tell the form what to do when nothing is selected. And your “Result” for LAN is “Cancel”.

You could do this a couple different ways, but if you just want to fix it quick to see the issue, change below.

    $lanButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

to

    $lanButton.DialogResult = [System.Windows.Forms.DialogResult]::Yes

Then change the other spots below, and add in a else catch at the end.
current

    if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
        $ip = (Get-NetAdapter | Where-Object { $_.Name -like "Wi-Fi*" } | Get-NetIPAddress -AddressFamily IPv4).IPAddress
        [System.Windows.Forms.MessageBox]::Show("Your Wi-Fi IP address is: $ip")
    } elseif ($result -eq [System.Windows.Forms.DialogResult]::Cancel) {
        $ip = (Get-NetAdapter | Where-Object { $_.Name -like "Ethernet*" } | Get-NetIPAddress -AddressFamily IPv4).IPAddress
        [System.Windows.Forms.MessageBox]::Show("Your LAN IP address is: $ip")
    } 

change to

    if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
        $ip = (Get-NetAdapter | Where-Object { $_.Name -like "Wi-Fi*" } | Get-NetIPAddress -AddressFamily IPv4).IPAddress
        [System.Windows.Forms.MessageBox]::Show("Your Wi-Fi IP address is: $ip")
    } elseif ($result -eq [System.Windows.Forms.DialogResult]::Yes) {
        $ip = (Get-NetAdapter | Where-Object { $_.Name -like "Ethernet*" } | Get-NetIPAddress -AddressFamily IPv4).IPAddress
        [System.Windows.Forms.MessageBox]::Show("Your LAN IP address is: $ip")
    } else {
    $form.Close()

Whole IP Address section would look like this;

"IP Address" {
    $wifiButton = New-Object System.Windows.Forms.Button
    $wifiButton.Text = "Wi-Fi"
    $wifiButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $wifiButton.Top = 30
    $wifiButton.Left = 20
    $wifiButton.Padding = New-Object System.Windows.Forms.Padding(10, 0, 0, 0)

    $lanButton = New-Object System.Windows.Forms.Button
    $lanButton.Text = "LAN"
    $lanButton.DialogResult = [System.Windows.Forms.DialogResult]::Yes
    $lanButton.Top = 30
    $lanButton.Left = 120
    $lanButton.Padding = New-Object System.Windows.Forms.Padding(10, 0, 0, 0)

    $form = New-Object System.Windows.Forms.Form
    $form.Text = "Select IP Address"
    $form.Controls.Add($wifiButton)
    $form.Controls.Add($lanButton)
    $form.ClientSize = New-Object System.Drawing.Size(250, 100)
    $form.StartPosition = "CenterScreen"
    $form.Topmost = $true

    $result = $form.ShowDialog()

    if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
        $ip = (Get-NetAdapter | Where-Object { $_.Name -like "Wi-Fi*" } | Get-NetIPAddress -AddressFamily IPv4).IPAddress
        [System.Windows.Forms.MessageBox]::Show("Your Wi-Fi IP address is: $ip")
    } elseif ($result -eq [System.Windows.Forms.DialogResult]::Yes) {
        $ip = (Get-NetAdapter | Where-Object { $_.Name -like "Ethernet*" } | Get-NetIPAddress -AddressFamily IPv4).IPAddress
        [System.Windows.Forms.MessageBox]::Show("Your LAN IP address is: $ip")
    } else {
    $form.Close()
    }
}

The other option is changing/adding button clicks to each one. As you have it now, you are limited to the options available in “DialogResult”, and you chose one that matches Closing/Cancelling the form. So when you hit X without selecting, that’s seen as “Cancel”, which you told it to show Lan Settings.

Let me know if that works and makes sense.

thank you its works now

1 Like