Re-Useable function

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
mcrank
Posts: 4
Last visit: Tue Jun 23, 2015 1:49 am

Re-Useable function

Post by mcrank »

Hello all! I have a question for the programmer types. I would like to create a function that is re-usable in such a way that I am modifying controls on seperate tabpages. In specific I am querying AD for sAMAccount name and if I do not find a match I am changing the background color of the text box to a reddish color (Tomato). What I have come across is I built the function with specific control names and I now have a need for that same function on another tabpage with obviously different control names. Is there a website out there or can anyone provide some information on how to pass in my function parameters to update the control from which the information came? I could always duplicate the code and change the control names but I would like to get into the habit of re-use when I can. I hope that made sense... Thank you! Marco
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re-Useable function

Post by davidc »

I not quite sure what you mean, but you could always pass the control type as a parameter. For example:

function Set-BackgroundColor

{

param([System.Windows.Forms.Control]$control,
[System.Drawing.Color]$color)



$control.BackColor = $color

}

Set-BackgroundColor $textbox1
'Blue'

Note: Most of the form controls derive from type Control. If you need more specific access to the TextBox control, you can always specify System.Windows.Forms.TextBox as the parameter type. David
davidc2011-11-29 15:29:15
David
SAPIEN Technologies, Inc.
User avatar
mcrank
Posts: 4
Last visit: Tue Jun 23, 2015 1:49 am

Re-Useable function

Post by mcrank »

Yep I think that will work. Didn't even think about passing the control into the function as a parameter. I will play around with it and see what I get to work. To hopefully clear up my initial question I have a Tab Control with several tab pages. I built some functionality on tabPage one where I query AD for the sAMAccount names in a couple of text boxes. In writing that function I specified the textbox names from tabPage1. I am now adding additional functionality on tabPage2 in which I would like to leverage that same AD query function but because I have hardcoded the Tabpage1 controls I would need to duplicate that function for the TabPage2. I think what you provided is what I was looking for as a solution or at least it will work. I just need to go back in and tweak the code and test. Thanks David and BTW love the Spotlight on control blog posts. Keep them coming!!!! Marco
mcrank2011-11-29 16:11:13
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.