Re-Useable function
Re-Useable function
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
Re-Useable function
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
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.
SAPIEN Technologies, Inc.
Re-Useable function
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
mcrank2011-11-29 16:11:13