Support Forums

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.
User avatar
Helocheck
Posts: 29
Joined: Tue Apr 29, 2014 1:55 am

Validate multiple Textboxes conente and pass value if not empty

Postby Helocheck » Wed Feb 10, 2016 2:35 pm

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

if ($txt_A.txt -ne '') {
 
$command += " -arg1" + ($txt_A).txt
}


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.
User avatar
jvierra
Posts: 10021
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Wed Feb 10, 2016 2:58 pm

You could use validation events to build the command. As each textbox is validated successful then generate a command.
User avatar
Helocheck
Posts: 29
Joined: Tue Apr 29, 2014 1:55 am

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

Postby Helocheck » Wed Feb 10, 2016 3:07 pm

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.
User avatar
jvierra
Posts: 10021
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Wed Feb 10, 2016 3:18 pm

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
Helocheck
Posts: 29
Joined: Tue Apr 29, 2014 1:55 am

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

Postby Helocheck » Thu Feb 11, 2016 12:46 am

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.
User avatar
jvierra
Posts: 10021
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Thu Feb 11, 2016 1:39 am

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

Return to “PowerShell GUIs”

Who is online

Users browsing this forum: No registered users and 3 guests