Totally confused

Archived support forum for customers who once purchased a PrimalForms product license. This forum is locked.
This topic is 12 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
eggnetadmin
Posts: 12
Last visit: Thu Feb 24, 2022 8:57 am

Totally confused

Post by eggnetadmin »

Hello,I still consider myself as a PowerShell newbie. I've a few basic scripts in production, that work well, but this is the first project I've attempted using forms. I'm currently using PowerForms2011 2.0.16 on Windows 7 x64. I've tried this form in both 32 and 64 bit mode.The problems I'm seeing are two-fold. First, when I run it, the openfile dialog works, and seems to return the proper file information. But, when I click the "Go" button, it returns the information for the last file chosen, for all 3 variables. The second problem is that, when I try to debug the form, the Variable pane displays NO variables. I don't get the red X which I've seen posted earlier.In my searching (day 2+ now!), I stumbled across scoping, so I've attempted to make the variables global, but this hasn't helped (the attempts are commented in my code.I have the TFM for PrimalScript, and several other books on PowerShell, but haven't found anything that seems to apply. I'm sure this is my inexperience, but, any help would be appreciated. I've copied the current code below.Thank you!Tim ---------------------------------------------------------------------------------------------$FormEvent_Load={ #TODO: Initialize Form Controls here }$buttonCustomerList_Click={ #TODO: Place custom script here $openfiledialog1.ShowDialog() #$global:Custfile = $selfile $Custfile = $selfile $selfile="" #$textbox1.text = $global:Custfile.filename $textbox1.text = $Custfile.filename }$buttonMessageBody_Click={ #TODO: Place custom script here $openfiledialog1.ShowDialog() #$global:BodyFile = $selfile $BodyFile = $selfile $selfile="" #$textbox2.text = $global:Bodyfile.filename $textbox2.text = $Bodyfile.filename}$buttonAttachmentText_Click={ #TODO: Place custom script here $openfiledialog1.ShowDialog() #$global:attachFile = $selfile $attachFile = $selfile $selfile="" #$textbox3.text = $global:attachFile.filename $textbox3.text = $attachFile.filename }$openfiledialog1_FileOk=[System.ComponentModel.CancelEventHandler]{#Event Argument: $_ = [System.ComponentModel.CancelEventArgs] #TODO: Place custom script here $selfile = $openfiledialog1}$buttonGO_Click={ #TODO: Place custom script here $email="lbemal"$fax="Fax number"$csvfile="c:userstleighDocumentsPowershell attachmentcustomer download.csv" #[System.Windows.Forms.MessageBox]::Show($global:custfile.filename) #[System.Windows.Forms.MessageBox]::Show($global:attachfile.filename) #[System.Windows.Forms.MessageBox]::Show($global:bodyfile.filename) [System.Windows.Forms.MessageBox]::Show($custfile.filename) [System.Windows.Forms.MessageBox]::Show($attachfile.filename) [System.Windows.Forms.MessageBox]::Show($bodyfile.filename)}#region Control Helper Functionsfunction Load-DataGridView{ <# .SYNOPSIS This functions helps you load items into a DataGridView. .DESCRIPTION Use this function to dynamically load items into the DataGridView control. .PARAMETER DataGridView The ComboBox control you want to add items to. .PARAMETER Item The object or objects you wish to load into the ComboBox's items collection. .PARAMETER DataMember Sets the name of the list or table in the data source for which the DataGridView is displaying data. #> Param ( [Parameter(Mandatory=$true)] [System.Windows.Forms.DataGridView]$DataGridView, [Parameter(Mandatory=$true)] $Item, [Parameter(Mandatory=$false)] [string]$DataMember ) $DataGridView.SuspendLayout() $DataGridView.DataMember = $DataMember if ($Item -is [System.ComponentModel.IListSource]` -or $Item -is [System.ComponentModel.IBindingList] -or $Item -is [System.ComponentModel.IBindingListView] ) { $DataGridView.DataSource = $Item } else { $array = New-Object System.Collections.ArrayList if ($Item -is [System.Collections.IList]) { $array.AddRange($Item) } else { $array.Add($Item) } $DataGridView.DataSource = $array } $DataGridView.ResumeLayout()}#endregion$datagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs] #TODO: Place custom script here }------------------------------------------------------------------------------------------
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Totally confused

Post by davidc »

don't see where you are saving the path from the OpenFileDialog. You need to do something like this: if($openfiledialog1.ShowDialog() -eq 'OK'){ #Get the File
Path: $textBoxFilePath.Text = $openfiledialog1.FileName}Note: The first line makes sure the user didn't cancel. As for debugging and the Variables Panel, the information is only populated when you hit a break point in the code. If this is not the case please let us know. David
David
SAPIEN Technologies, Inc.
User avatar
eggnetadmin
Posts: 12
Last visit: Thu Feb 24, 2022 8:57 am

Totally confused

Post by eggnetadmin »

David,This code block: #$global:attachFile = $selfile $attachFile = $selfile $selfile="" $textbox3.text = $attachFile.filenameHas been working. My input form (at least the textbox text) does change based on what file the user has selected. (the Openfile dialog returns the $selfile variable).As far as the debugging issue, I'm certain it's because, simply, I don' t know what I'm doing in it. I did look for a tutorial, or howto, but didn't find one.Tim
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Totally confused

Post by davidc »

Ok I see what you are doing. You are setting:$selfile = $openfiledialog1This line is setting the object and not the file path. This line is not needed nor do you need all the scoped variables. It just leads to confusion and complications :). You can simplify this by using my example instead and use the TextBox's Text property in the 'Go' button's Click event. I also recommend giving the controls meaningful names. For example, $textBoxAttachedFile instead of $textBox3. We currently working on the guides. David
David
SAPIEN Technologies, Inc.
User avatar
eggnetadmin
Posts: 12
Last visit: Thu Feb 24, 2022 8:57 am

Totally confused

Post by eggnetadmin »

David,Ok.. The display lines in the GO event are just for testing. The final scope for this project is to take the three files listed, Modify an attachment, and create a PDF of it, then modify the message body, and then email the results to people listed in CustFile. I've got the meat of the program in a .ps1 file, but am working on putting a GUI front end on it, so IT doesn't need to get involved when a user gets confused.As the code is now, the textbox?.text properties are getting properly modified, which is why I'm confused that, when I get to GO, the variables aren't what they should be. I can tell, that the variables ($attachFile, $BodyFile, $CustFile) are, at some point, getting set properly, but, when I go into the "GO" section, they're all reset to the last file selected in the Open file dialog.Tim
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Totally confused

Post by davidc »

My question to you is: Are you calling the ps1 script from the Form script or is the ps1 script calling the GUI? In either case I would recommend creating a Project that includes the form and the ps1 script. What way you can call the script from the form file (or vice versa) using the Call-<FileName> methods. Note: In projects, PrimalForms will automatically generate the return variables for the form controls when you use the Call functions. The Document explorer has some sample projects in addition to those posted on our blog. David
David
SAPIEN Technologies, Inc.
User avatar
eggnetadmin
Posts: 12
Last visit: Thu Feb 24, 2022 8:57 am

Totally confused

Post by eggnetadmin »

David,I had actually been planning on putting the meat of the .ps1 into the Go button event.I suppose I'm thinking too narrowly, but I've looked through some of the sample projects, and didn't see any that looked like I could model from. I'm sure it's because I'm attacking this from a limited mindset.I think I figured out why my variables are changing, but not how to fix it. I did figure out how to put some breakpoints into the form, and, you were right, THEN the variable panel populated. When I inspected the $CustFile variable, it's not the file object I expected, but the OpenDialog object. Which (Duh!) explains why it changed to the last file chosen. I'm concerned, though, that if I set $CustFile (for example) to the $selfile.filename property, it's not an object anymore, so won't respond the way I'd expect if I had, for example, used a Get-Children object for it. (I am doing quite a bit of file watcher work for other tasks, that use the file object later in the script).I apologize for not speaking too intelligently on this - I'm still quite new to PS, though I've done tons of BAT coding, in a past life!Tim
This topic is 12 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.