# Help why the windows is closed but i see meesag

**URL:** https://forums.powershell.org/t/help-why-the-windows-is-closed-but-i-see-meesag/21741
**Category:** PowerShell Help
**Created:** [February 24, 2023, 5:25pm UTC](https://forums.powershell.org/t/help-why-the-windows-is-closed-but-i-see-meesag/21741 "2023-02-24T17:25:56Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![alexxx55555](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@alexxx55555](https://forums.powershell.org/u/alexxx55555)
#### Post date: [February 24, 2023, 5:25pm UTC](https://forums.powershell.org/t/help-why-the-windows-is-closed-but-i-see-meesag/21741/1 "2023-02-24T17:25:56Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![mrfloppy](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mrfloppy](https://forums.powershell.org/u/mrfloppy)
#### Post date: [February 24, 2023, 5:38pm UTC](https://forums.powershell.org/t/help-why-the-windows-is-closed-but-i-see-meesag/21741/2 "2023-02-24T17:38:56Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![alexxx55555](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@alexxx55555](https://forums.powershell.org/u/alexxx55555)
#### Post date: [February 24, 2023, 5:41pm UTC](https://forums.powershell.org/t/help-why-the-windows-is-closed-but-i-see-meesag/21741/3 "2023-02-24T17:41:17Z")

</div>

sorry this the code its starting with ip address

```auto
"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")
    }
}

```

---

<div class="post-metadata">

### Author: ![mrfloppy](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mrfloppy](https://forums.powershell.org/u/mrfloppy)
#### Post date: [February 24, 2023, 5:44pm UTC](https://forums.powershell.org/t/help-why-the-windows-is-closed-but-i-see-meesag/21741/4 "2023-02-24T17:44:58Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![alexxx55555](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@alexxx55555](https://forums.powershell.org/u/alexxx55555)
#### Post date: [February 24, 2023, 5:47pm UTC](https://forums.powershell.org/t/help-why-the-windows-is-closed-but-i-see-meesag/21741/5 "2023-02-24T17:47:32Z")

</div>

this is the full script

```auto
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?

---

<div class="post-metadata">

### Author: ![mrfloppy](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mrfloppy](https://forums.powershell.org/u/mrfloppy)
#### Post date: [February 24, 2023, 6:22pm UTC](https://forums.powershell.org/t/help-why-the-windows-is-closed-but-i-see-meesag/21741/6 "2023-02-24T18:22:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![alexxx55555](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@alexxx55555](https://forums.powershell.org/u/alexxx55555)
#### Post date: [February 24, 2023, 6:24pm UTC](https://forums.powershell.org/t/help-why-the-windows-is-closed-but-i-see-meesag/21741/7 "2023-02-24T18:24:03Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mrfloppy](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mrfloppy](https://forums.powershell.org/u/mrfloppy)
#### Post date: [February 24, 2023, 6:33pm UTC](https://forums.powershell.org/t/help-why-the-windows-is-closed-but-i-see-meesag/21741/8 "2023-02-24T18:33:37Z")

</div>

Just goes back to main form.

---

<div class="post-metadata">

### Author: ![alexxx55555](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@alexxx55555](https://forums.powershell.org/u/alexxx55555)
#### Post date: [February 24, 2023, 6:36pm UTC](https://forums.powershell.org/t/help-why-the-windows-is-closed-but-i-see-meesag/21741/9 "2023-02-24T18:36:38Z")

</div>

i see message that your ip is i dont understnad why

---

<div class="post-metadata">

### Author: ![mrfloppy](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mrfloppy](https://forums.powershell.org/u/mrfloppy)
#### Post date: [February 24, 2023, 7:20pm UTC](https://forums.powershell.org/t/help-why-the-windows-is-closed-but-i-see-meesag/21741/10 "2023-02-24T19:20:09Z")

</div>

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.

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

```

to

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

```

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

```auto
    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

```auto
    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;

```auto
"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.

---

<div class="post-metadata">

### Author: ![alexxx55555](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@alexxx55555](https://forums.powershell.org/u/alexxx55555)
#### Post date: [February 24, 2023, 7:38pm UTC](https://forums.powershell.org/t/help-why-the-windows-is-closed-but-i-see-meesag/21741/11 "2023-02-24T19:38:21Z")

</div>

thank you its works now

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:15pm UTC](https://forums.powershell.org/t/help-why-the-windows-is-closed-but-i-see-meesag/21741/12 "2024-05-16T20:15:08Z")

</div>


