By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,894 Members | 1,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,894 IT Pros & Developers. It's quick & easy.

Setting a control

P: n/a
I have a function that I can't seem to get to work properly. This function
is suppose to loop through a collection of controls and locate an existing
ojbect of that type. if it locates the control it uses that object if it
does not a new object is added.

public void SetMainControl(System.Windows.Forms.Control Testcontrol)

{

System.Windows.Forms.Control aTmpControl = TestControl;

bool found = false;

foreach(System.Windows.Forms.Control aControl in
StudyViewer_Fill_Panel.Controls)

{

if (aControl is aTmpControl)

{

found = true;

TestControl = aControl;

break;

}

}

if (!found)

{
StudyViewer_Fill_Panel.Controls.Add(Testcontrol);

}

Testcontrol.Dock = DockStyle.Fill;
}
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
L#
On Thu, 5 Feb 2004 14:45:55 -0800, "MFRASER"
<mf*****@henwoodenergy.com> wrote:
I have a function that I can't seem to get to work properly. This function
is suppose to loop through a collection of controls and locate an existing
ojbect of that type. if it locates the control it uses that object if it
does not a new object is added.


An example:

public class MyControl : Control
{
// implementation of your control
}

private void Form1_Load(object sender, System.EventArgs e)
{
// add your control to the controls collection
this.Controls.Add(new MyControl());

// loop through all controls on the form and check if it's a
// MyControl
foreach (Control c in this.Controls)
{
// method 1
if (c.GetType() == typeof(MyControl))
{
// it's a MyControl
}

// method 2
if (c is MyControl)
{
// it's a MyControl
}

}

}
--
Ludwig
mailto:ludwig_(nospamplease)stuyck@pandora(nospamp lease).be
Nov 15 '05 #2

P: n/a
That is what I thought but when I create a function to set the control
and pass in that control the code will not compile.

So If I changed your code to

An example:

public class MyControl : Control
{
// implementation of your control
}

private void Form1_Load(object sender, System.EventArgs e)
{
// add your control to the controls collection
//this.Controls.Add(new MyControl());
this.SetMainControl( new MyControl());
}
private void SetMainControl(System.Windows.Forms MyControl)
{
// loop through all controls on the form and check if it's a
// MyControl
foreach (Control c in this.Controls)
{
// method 1
if (c.GetType() == typeof(MyControl))
{
// it's a MyControl
}

// method 2
if (c is MyControl)
{
// it's a MyControl
}

}

}
}


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3

P: n/a
L#
On Thu, 05 Feb 2004 16:19:39 -0800, Fraser Michael
<mf*****@henwoodenergy.com> wrote:
That is what I thought but when I create a function to set the control
and pass in that control the code will not compile.


That's beause the parameter has to be a Control, like this:

private void SetMainControl(Control MyControl)
...

--
Ludwig
mailto:ludwig_(nospamplease)stuyck@pandora(nospamp lease).be
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.