Why can't I check my child TreeViewNode checkboxes programmatically? - winforms

Hello I am attempting to check child checkboxes in a TreeView like so:
Register-ObjectEvent -InputObject $treeView -EventName AfterCheck -Action {
$e = [System.Windows.Forms.TreeViewEventArgs]$EventArgs
if($e.Action -ne [System.Windows.Forms.TreeViewAction]::Unknown)
{
foreach($childNode in $e.Node.Nodes)
{
([System.Windows.Forms.TreeNode]$childNode).Checked = $true
}
}
} | Out-Null
Which does in fact properly set the "Checked" property to true, but the tick is not displayed on the form. I also have attempted setting TreeViewNode text and experience the same result. I've checked the hash values on the .NET objects thinking that the event handler was firing from a separate runspace or working with copies of the objects but this does not appear to be the case.
Any thoughts on why the form isn't displaying changes to the objects from inside the handler? My form's load handler is able to properly populate the TreeView, so why wouldn't I be able to modify it from the AfterCheck handler?
Thanks!

Related

How to access Parent Form public variables, control properties and methods? (WinForms C#)

I have Parent Form (FrmMainMenu) that has 3 panels. A panel1 docked on top and a panel2 docked on the left. The 3rd panel will be my container for the child Forms. I have a Title label (lblTitle.Text = "Home") on panel1 and buttons on panel2. I'm trying to emulate a Blazor look and feel (navbar and sidebar). My question is how can I access/manipulate the Title label (lblTitle.Text) on my Parent Form (FrmMainMenu) from a Child Form?
1. On Control Properties:
Example Event: When opening the child form I want the (lblTitle.Text) properties be changed according to child form function. ex. (lblTitle.Text = "List of Rooms"). And when closing the child form I want the (lblTitle.Text = "Home") go back to its original Text properties.
2. Methods: I have a method (public void ResetColors()) on my (FrmMainMenu) that can reset the colors on my buttons. Despite being public method my child form cannot access the method. This is also true to public variables.
Any suggestions is appreciated.
I tried converting private methods and variables into public. I also tried changing the control Modifiers to public. I still can't access Parent Form control properties and public methods.
I got it.
((frmMainMenu)this.ParentForm).lblTitle.Text = "List of Rooms";
On closing child form:
((frmMainMenu)this.ParentForm).Reset();
((frmMainMenu)this.ParentForm).lblTitle.Text = "Home";

How do we add Checkboxes for an ownerdrawn list control in MFC?

How can I make an owner-draw list control with checkboxes. What I need is:
I have data separate, each has a different color code. I need to add this to a list control with the format :
This allows multiple selection
[checkbox] [color code rectangle] Text_Item
I need to use checkboxes to select which I want selected.
I Should be able to select multiple items from listcontrol without having to use CTRL+Click.
I have tried using DrawFrameControl For getting the checkboxes.But using which we are able to select only one item at a time.Following is my code I have used in my drawitem method.
void OwnerdrawListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
bool sel = (lpDrawItemStruct->itemState && ODS_SELECTED);
if
DrawFrameControl(*pDC, rcItem, DFC_BUTTON, DFCS_CHECKED);
else
DrawFrameControl(*pDC, rcItem, DFC_BUTTON, DFCS_INACTIVE);
}
This is how I am adding the checkboxes to my listCtrl.Im handling an NM_Click event in which Im setting the state of the ienter code heretem that is being clicked.

How to change the background color of a specific WPF listbox item via Powershell code?

I have built a WPF form and the form and its controls are all working great.
I can add items via a button-click event, clear the items via a different button-click event, copy the contents of individual listbox items to the clipboard via a double-click event on the listbox.
I just wanted to titivate and change the background color of a specific listbox item via code (not via selected item).
The control variable is $lbxCopy and is working successfully, as in:
$lbxCopy.add_MouseDoubleClick($zlbxCopyMouseDoubleClick)
The code I am trying to use to change the first item in the listbox is:
$lbxCopy.Items[0].Background = "Red"
The failure message is:
The property 'Background' cannot be found on this object.
Verify that the property exists and can be set.
At winCopyList_Event_Master.ps1:108 char:1
+ $lbxCopy.Items[0].Background = "Red"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
Here is the code from the Window_Loaded event:
$zwinCopyListLoaded = {
#- Initial Load of the Listbox data -----
#- Calls a sub-process that successfully load the listbox -----
. $zLoadListBox
$lbxCopy.Background = "Red"
#- The above works but sets the whole listbox background color.
$lbxCopy.Items[0].Background = "Blue"
#- The above fails !
}#-End of Window Loaded event -----
#-------------------------------------------------------------
#- Window Loaded ~ Event-Monitor -----
$winCopyList.add_Loaded($zwinCopyListLoaded)
I want to be able to choose the specific listbox item via a code event, not via a selection event.
I am sure I am doing something stupid, like I have a blindspot, and I am going to smack my head when I get an answer on this titillation that I am floundering over!
Other Notes: Windows 10 desktop; 64 bit architecture; Powershell 4; I don't understand 'C'; I am just a learner with Powershell & WPF; I have searched with Google/MSDN/StackOverflow without success.
I did try and follow this article without success:
https://www.codeproject.com/Articles/8134/Coloring-items-in-a-ListBox
Apologies in advance for my stupidity.
I found that the suggestion in the post below worked successfully, and I am now using it. Many thanks to those who posted.
https://stackoverflow.com/questions/43565540#43565540
$ListBox = $Window.FindName("ListBox")
#- Repeat the code below for each 'Items.Add' -----
$itm = new-object System.Windows.Controls.ListboxItem
$itm.Content = 'test red'
$itm.Background = 'red'
$ListBox.Items.Add($itm)

Do Something on WPF Form Minimize –PowerShell (version 5)

I have PowerShell script that creates a WPF form from inserted XML code. This form will run on a kiosk station and has no window (so no maximize, minimize, or close buttons) and consumes the entire screen.
Even though the form itself has no window buttons, a person can still press the WIN key on a keyboard, get the task bar, right click on the form in the task bar and choose to minimize, maximize, or close for the form.
So, I set an action for close to lock the workstation (which is better option for this kiosk instead of preventing the form from closing at all) by doing this:
$form.Add_Closing({
$shell = New-Object -ComObject "Wscript.Shell"
$shell.Run("%windir%\System32\rundll32.exe user32.dll,LockWorkStation")
})
I would also like to do this for Minimize if possible, but can’t seem to figure out how.
FYI. I’m not intending this to be a security feature per-se, but more so something that is better done then not for this kiosk station.
if $form is your form, then you could try this (wrapping the repetitive part in a function to stay DRY):
function Lock-Workstation {
$shell = New-Object -ComObject "Wscript.Shell"
$shell.Run("%windir%\System32\rundll32.exe user32.dll,LockWorkStation")
}
$form.Add_Closing({ Lock-Workstation })
$form.Add_StateChanged({
if($form.WindowState -eq "Minimized") {
Lock-Workstation
}
})
Since you don't want the window to be resized anyway, you could simply use:
$form.Add_StateChanged({ Lock-Workstation })

Why does a form displayed by PowerShell sometimes not show up?

When I create a form (window) in PowerShell, I can usually display the form using .ShowDialog():
$form = New-Object System.Windows.Forms.Form
$form.ShowDialog()
.Visible is set to False before and after .ShowDialog().
But when I do a .Show() nothing is displayed on the screen:
$form.Show()
And .Visible is now set to True (presumably because .Show() made the form officially visible.)
When I now try to .ShowDialog() the form again, I get the following error message:
"Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog."
But when I follow the instructions to .ShowDialog() again
$form.Visible=0
$form.ShowDialog()
the result is that nothing is displayed on the screen and PowerShell hangs and cannot recover (ctrl-c doesn't seem to work). I assume this is because the form is being displayed modally somewhere where I cannot see it (or tab to it). But why?
The coordinates of the form haven't changed. So how does the form decide when it is physically visible and when it isn't?
Avoid using Show() from PowerShell as it requires a message pump and that isn't something the PowerShell console provides on the thread that creates your form. ShowDialog() works because the OS does the message pumping during this modal call. Creating the form and calling ShowDialog() works reliably for me.
My problem: When using ShowDialog() as part of a powershell logon script, the first form window would not show and powershell would seem to freeze up on logon. Symptoms were simular to the original post.
Solution I found: Instead of using $form.showDialog(), use:
[System.Windows.Forms.Application]::Run($form)
Works great for me now, and only the first form in the series needed the change. All my other forms that come up afterwards in the script still use showDialog.

Resources