Validate multiple Textboxes conente and pass value if not empty

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 8 years and 1 month 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.
Locked
User avatar
PsCustomObject
Posts: 137
Last visit: Thu Mar 28, 2024 3:34 am

Validate multiple Textboxes conente and pass value if not empty

Post by PsCustomObject »

Hello,

sorry to ask for help once agian but find myself stuck with this.

I have a form with mulitple textboxes apart from two of them remaining are optional with a checkbox next to them that needs to be checked to enable the texbox.

On the same form I do have a button that launches a PowerShell command accepting multiple optional parameters, the idea would be to pass the text in the textxoes as parameter but only if the textbox has been filled by the user(hope it is clear).
if you know Exchange and ever used the old (Exchange 2007/2010) message tracking log tool that's exactly the type of result I'm trying to achieve.

My appraoch was using multiple if staemente more or less like this
  1. if ($txt_A.txt -ne '') {
  2.  
  3. $command += " -arg1" + ($txt_A).txt
  4. }
It seems to work but I was wondering if there is (and I'm sure there is) a better/more elegant way to achieve this.

As usual thanks in advance for your invaluable help and if the above is not clear I'll try to further elaborate.

Thanks L.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Validate multiple Textboxes conente and pass value if not empty

Post by jvierra »

You could use validation events to build the command. As each textbox is validated successful then generate a command.
User avatar
PsCustomObject
Posts: 137
Last visit: Thu Mar 28, 2024 3:34 am

Re: Validate multiple Textboxes conente and pass value if not empty

Post by PsCustomObject »

Thanks,

I tought of that after reading the blog post, which I cannot find right now, but as far as I could see that does not apply to my case as

- all textboxes could have a value
- none of them could have a value

That's why I find myself kinda lost, well I will keep looking and testing worst case I will keep all the if statements.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Validate multiple Textboxes conente and pass value if not empty

Post by jvierra »

That can mal be discovered in the validated event. The plus is the validated event will get executed every time there is a change and account for the change. No need to look back.
User avatar
PsCustomObject
Posts: 137
Last visit: Thu Mar 28, 2024 3:34 am

Re: Validate multiple Textboxes conente and pass value if not empty

Post by PsCustomObject »

Thanks,

I'll go back to the post I mentioned and do my homework, maybe I can indeed come up with something more elegant that a neverending list of "IF"

L.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Validate multiple Textboxes conente and pass value if not empty

Post by jvierra »

Consider this:

Each control is either a contributor or not. Each control determines if another control is required or not. Each control implies a set of rules. The validating and validated events are designed to dynamically check these rules.

A CmdLet has a set of validation attributes on each parameter. This is a linear method of implementing a state machine. Each parameter attempts to validate its set of rules. Look at the overarching rule - a parameter belongs to a parameter set. Other "validating" states are [ValidateNotNull()], [ValidateRange()], [ValidateLength()]...etc. This is what validation in Forms does but is more flexible. The newer [ValidateScript()] is modeled after the Windows Forms "Validating" event. The plus in forms is we get two bytes at the apple. We get validating and validated. We get to think about things when the control or form is "Validating" then, assuming validating is satisfied, we get called to tell us the things were "Validated" successfully.

Look at all events in Windows. "OnBefore<thing>", "On<thing>",OnAfter<thing>". It is a pattern. It is an ancient pattern in the world, the universe and in computing. Once you realize that Windows "anticipates", "acts" and "looks back" then you will begin to understand what Windows is about. We see this pattern start in 1945 and continue at Bell Labs in 1962 and at Xerox PARC in 1973. See https://en.wikipedia.org/wiki/History_o ... _interface. The elements were adopted early because it is the only way to manage an interface based on visual and physical interactions and not on a text based command interface.

[P.S. - I missed PARC due to having left California to early but made it to Bell Labs in Holmdel NJ before Jobs or Gates.

What you are using has a rich and crazy history. It was designed and developed by many very smart scientists and engineers starting when computers were still made out of vacuum tubes and small power plants. My first computer took up most of a floor at Bell Labs and only allowed us to send it boxes of punched cards with data and program. My first "Hello World" program was bigger than a 1Tb USB drive and weighed much more.

AS complex as it seems the events and methods of forms objects are all well thought out. They allow us to do almost anything we can imagine with a GUI.

I know this all sounds too complicated but, once comprehended, it makes building forms easy and allows us to use very small amounts of code. We only need to learn how to think like a form.

See: https://en.wikipedia.org/wiki/Event-driven_programming
See: https://en.wikipedia.org/wiki/Finite-state_machine
This topic is 8 years and 1 month 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.
Locked