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.