How to pass and input control into a function

P

pbearne

Hi guys

I am trying to this function below

I have a web form with a load of inputs text boxs etc and i need to
disable them

So I have tried to write a function I can call and get the function to
work the type of the input so can disaable as needed

But this won't compile because the "inputObject" is not the right type!

Any ideas

Paul

private void DisableInput(Control inputObject,string tooltipString)
{

switch (inputObject.GetType().ToString())
{
case "TextBox":
(TextBox)inputObject.ReadOnly = true; // tried this
break;
case "DropDownList":
case "ListBox":
case "Button":
case "GridView":
inputObject.Enabled = false;
break;
case "HtmlInputFile":
case "HtmlInputHidden":
inputObject.Disabled = true;
break;
default:
Response.Write(inputObject.GetType().ToString());
}
inputObject.ToolTip += tooltipString;
uploadedThumbnail.Disabled = true;
}
 
D

DeveloperX

See comments in the code.

private void DisableInput(Control inputObject,string tooltipString)
{


switch (inputObject.GetType().ToString())
{
case "TextBox":
// Need to bracket the whole thing so we know ReadOnly applies to
the textbox not the control
((TextBox)inputObject).ReadOnly = true; // tried this
break;
case "DropDownList":
case "ListBox":
case "Button":
case "GridView":
inputObject.Enabled = false;
break;
case "HtmlInputFile":
case "HtmlInputHidden":
//no Disabled method, Enabled = false = disabled
inputObject.Enabled = false;
break;
default:
//Response.Write(inputObject.GetType().ToString());
//Missing break.
break;
}
//Control doesn't have a tooltip member and do you want to append
the string each time this is run?
//You could end up with a tip of "DisabledDisabledDisabledDisabled"
inputObject.ToolTip += tooltipString;
//Not sure where this variable is declared so ignoring
uploadedThumbnail.Enabled = false;
}
 
P

pbearne

got it to work

private void DisableInput(object inputObject,string tooltipString)
{

switch (inputObject.GetType().ToString())
{
case "TextBox":
TextBox textBox = (TextBox)inputObject;
textBox.ReadOnly = true;
textBox.ToolTip += tooltipString;
break;
case "DropDownList":
DropDownList DropDownList = (DropDownList)inputObject;
DropDownList.Enabled = false;
DropDownList.ToolTip += tooltipString;
case "ListBox":
ListBox ListBox = (ListBox)inputObject;
ListBox.Enabled = false;
ListBox.ToolTip += tooltipString;
case "Button":
Button Button = (Button)inputObject;
Button.Enabled = false;
Button.ToolTip += tooltipString;
case "GridView":
GridView GridView = (GridView)inputObject;
GridView.Enabled = false;
GridView.ToolTip += tooltipString;
break;
case "HtmlInputFile":
HtmlInputFile HtmlInputFile = (HtmlInputFile)inputObject;
HtmlInputFile.Disabled = true;
case "HtmlInputHidden":
HtmlInputHidden HtmlInputHidden = (HtmlInputHidden)inputObject;
HtmlInputHidden.Disabled = true;
break;
default:
Response.Write(inputObject.GetType().ToString());
}

}

But is there a better way?
 
D

DeveloperX

Disregard the comments where I say Disabled doesn't exist on
HtmlInputFile. I just had to google it and it looks like an ASP
convention, where as windows forms all use Enabled. Anyone know if
that's right? Irritating if you ask me.
 
P

pbearne

here is the code i used in the end

I hope this is of use

private void EnableInput(Object inputObject, string tooltipString)
{
switch (inputObject.GetType().ToString())
{
case "TextBox":
((TextBox)inputObject).ReadOnly = false;
((TextBox)inputObject).ToolTip =
((TextBox)inputObject).ToolTip.Replace(tooltipString,"");
break;
case "DropDownList":
((DropDownList)inputObject).Enabled = true;
((DropDownList)inputObject).ToolTip =
((DropDownList)inputObject).ToolTip.Replace(tooltipString,"");
break;
case "ListBox":
((ListBox)inputObject).Enabled = true;
((ListBox)inputObject).ToolTip =
((ListBox)inputObject).ToolTip.Replace(tooltipString,"");
break;
case "Button":
((Button)inputObject).Enabled = true;
((Button)inputObject).ToolTip =
((Button)inputObject).ToolTip.Replace(tooltipString,"");
break;
case "GridView":
((GridView)inputObject).Enabled = true;
((GridView)inputObject).ToolTip =
((GridView)inputObject).ToolTip.Replace(tooltipString,"");
break;
case "HtmlInputFile":
((HtmlInputFile)inputObject).Disabled = false;
break;
case "HtmlInputHidden":
((HtmlInputHidden)inputObject).Disabled = false;
break;

}

}

private void DisableInput(object inputObject,string tooltipString)
{

switch (inputObject.GetType().ToString())
{
case "TextBox":
((TextBox)inputObject).ReadOnly = true;
((TextBox)inputObject).ToolTip += tooltipString;
break;
case "DropDownList":
((DropDownList)inputObject).Enabled = false;
((DropDownList)inputObject).ToolTip += tooltipString;
break;
case "ListBox":
((ListBox)inputObject).Enabled = false;
((ListBox)inputObject).ToolTip += tooltipString;
break;
case "Button":
((Button)inputObject).Enabled = false;
((Button)inputObject).ToolTip += tooltipString;
break;
case "GridView":
((GridView)inputObject).Enabled = false;
((GridView)inputObject).ToolTip += tooltipString;
break;
case "HtmlInputFile":
((HtmlInputFile)inputObject).Disabled = true;
break;
case "HtmlInputHidden":
((HtmlInputHidden)inputObject).Disabled = true;
break;
}
}
 
P

pbearne

change the switch to

switch
(inputObject.GetType().ToString().Substring(inputObject.GetType().ToString().LastIndexOf(".")
+ 1))

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top