Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking type of controls eg Textbox, Button, listbox etc? 1

Status
Not open for further replies.

rahmanjan

Programmer
Apr 13, 2003
180
AU
Hi guys,

I am trying to write down a generic Class where I will be able to pass a form and that class handle the basic settings of the form's Controls. Eg set the Font, color, size, style etc.

2 issues here:

1- How can I get a from in my class constructor to have access to all its controls? It should be generic to get an Windows Form. i tryed even i declared the controls as Public however i couldn't access the controls. In the constructor i was getting it as:

class Setter(System.Windows.Forms frm) ??

If it is not possible I would like to do it in each form seperately.

2- Let is say i want to loop through all the controls in the form and if the control is textbox set some properties, if its button set some others ... something like:

if (Contol.typeof(Button))
//set stuff
elseif (Contol.typeof(textbox))
//set stuff
etc

Has anyone tryed it at all? Is it possible?

any comments, hints will be appreciated.

TA
 
1. Yes you can create a parameterized constructor that accepts a Form object. But your code is passing the Forms namespace, and not a Form. Do it like this:
Code:
public class foo
{
   public foo(System.Windows.Forms.Form myForm)
   {
      // do things with myForm
   }
}

2. It's very possible -- people do it all the time. Most of the time, people do a foreach() against the form's Controls collection.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Hi there,

Thanks for the reply.

I exactly do the same thing, when i post the above code i didn't have access to my code so i wrote it on the top of my head which was not correct. With that code i don't have access to all control of my form even if i declare them public. ... however if i do it like below:

eg public foo(frmCustomer myForm) -> frmCustomer already exist. Then it works. but not with System.Windows.Forms.Form myForm.

that is what my question was.

Also for my second point when i am looping via foreach how can i spefify if the control is Button, or Textbox or Label.

Any code snippet will be very appreciated!!

Any online resources ...

TA

 
Well, as for the second question you might make a method that would loop all the controls in the form. This method would be something like this:

public void ControlsSettings()
{
foreach(Control child in this.Controls)
{
if (child is GroupBox)
{
foreach(Control child1 in child.Controls)
{
if (child1 is Button)
{
// Do stuff
}
}
}
else if (child is Button)
{
// Do stuff
}
else if (child is TextBox)
{
// Do stuff
}
}
}

I don't know if I understood correctly your second question but this can be a solution.
As for the first question I didn't really understand it.
Greetings
 
Thanks,

u perfectly understood the 2nd question and this somehting i was looking for.

as for as the first question. i want to pass a form as argument and i want to have access to it in a class. it shud be generic so i will be able to pass form1, form2 etc. and i shud be able to have access to all its controls.

hope clear now

ta
 
Well, as for the first question is almost the same thing like the method above. The method would look like this:

public void ControlsSettings(System.Windows.Forms.Form frm)
{
foreach(Control child in frm.Controls)
{
if (child is GroupBox)
{
foreach(Control child1 in child.Controls)
{
if (child1 is Button)
{
// Do stuff
}
}
}
else if (child is Button)
{
// Do stuff
}
else if (child is TextBox)
{
// Do stuff
}
}
}

This, of course will have more conditions, if the control is TextBox or some other.

 
public foo(frmCustomer myForm) -> frmCustomer already exist. Then it works. but not with System.Windows.Forms.Form myForm.

That's because frmCustomer is not the same datatype as System.Windows.Forms.Form. You'll need to either give the parameter the correct type (frmCustomer), or convert it to a frmCustomer using CType().

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Whoops - CType is a VB.NET method. In C# you would do a cast:
Code:
public void MyMethod(Form myForm)
{
   frmCustomer frm = (frmCustomer)myForm;
   // use frm from now on
}
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks Chiph,

I know and you are correct frmCustomer is not same as System.Windows.Forms.Form myForm. However, i want to create a generic class / method to work for all my forms. I hope i am clear here?

I don't want:
frmCustomer frm = (frmCustomer)myForm;
frmProvider frm = (frmProvider) myForm;
frmAccount ....
frmItem .....
etc etc ...

I want some way that System.Windows.Forms.Form myForm will be equal to anyform that i am passing as parameter ... without any casting. Possible?

Ta
 
I just tried the following and it works fine.
Code:
{
  ...
  Test.FormChecker xx = new Test.FormChecker();
  xx.Check( new Test.MyForm() );
  xx.Check( new Test.MyOtherForm() );
}
public class MyForm : System.Windows.Forms.Form
{
}
public class MyOtherForm : MyForm
{
}
public class FormChecker
{
  public void Check( System.Windows.Forms.Form theForm )
  {
    theForm.AutoScale = true;
  }
}

You may cast whereever necessary but you shouldn't need to as any form "IS A" Windows.System.Forms.Form. Anyway, how exactly did you call your function?
 
I think the problem was they wanted to call a method that was specific to a frmCustomer. Obviously, a Windows.Form won't know anything about it, and the compiler flags it as an error.

If there is a set of shared methods that rahmanjan needs to call on a set of forms, it would be best to create an interface that those forms implement, and make the parameter to the method of that type.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Hello guys,

I think it works now with some modifications...

Mondi is the winner and defintely deserves a *Star* ;-)

Thanks you all guys for the help.

Still one issue:

After setting the properties the form looks really disorganized ie the controls are moving to different positions. I din't know why it happens.

I do as bellow:

public static void ControlsSettings(System.Windows.Forms.Form myForm)
{
foreach(Control child in myForm.Controls)
{

if (child is Button)
{ //i added these bits
Button thisChild = (Button) child;

thisChild.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
thisChild.ForeColor = System.Drawing.Color.Maroon;
// thisChild.Image = ((System.Drawing.Image)(resources.GetObject("Image")));
thisChild.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
thisChild.Location = new System.Drawing.Point(184, 144);
thisChild.Name = "cmdGenerate";
thisChild.Size = new System.Drawing.Size(104, 24);
thisChild.TabIndex = 7;
thisChild.Text = "Generate";
}
else if (child is TextBox)
{
TextBox thisChild = (TextBox) child;

thisChild.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
thisChild.ForeColor = System.Drawing.Color.Maroon;
thisChild.Location = new System.Drawing.Point(152, 24);
thisChild.Multiline = true;
thisChild.Name = "txtFile";
thisChild.Size = new System.Drawing.Size(136, 24);
thisChild.TabIndex = 0;
thisChild.Text = "";
}
}

 
ah,
just fine.
for testing i had the location set there n it was causing issues:

thisChild.Location = new System.Drawing.Point(152, 24);

i commented that .... all ok now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top