Solved

cloning a control ?

Posted on 2006-06-20
9
270 Views
Last Modified: 2010-08-05
Hi

for example if i have a real control in the form in c# lets say Text1 now how i can define a control varaible
in the same class that clone the the real control let me explain it more

The real control name is :

Text1

now in the click event of the Text1 i want to pass the name of the control to another class or procedure
and define a control variable to take the name of the real control and act just like real control for example

when i say in the control varible
ctrclone.text = "nothing";
i want it to affect the real control

in a gloss i need to create a clone to the control

is that possible

and thanks

please if you feel that my question need to be elaborated more tell me

thanks again
0
Comment
Question by:YASAR_SAID
  • 4
  • 3
  • 2
9 Comments
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 400 total points
ID: 16947324
First of all, what version C# are you running?

Second, in your statement:

    "i want to pass the name of the control to another class or procedure"

Do you mean that you want to pass a STRING variable containing the name of the control?  ...or you want to pass a reference to the control?
0
 

Author Comment

by:YASAR_SAID
ID: 16947344
am using visual c# 2005 express edition

and yes i mean to send a reference of the control
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16947359
This seems to simple...

Like this?

        private void textBox1_Click(object sender, EventArgs e)
        {
            Foo((TextBox)sender);
        }

        private void Foo(TextBox tb)
        {
            tb.Text = "nothing";
        }
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16947363
...or?

        private void textBox1_Click(object sender, EventArgs e)
        {
            Foo(this.textBox1);
        }

        private void Foo(TextBox tb)
        {
            tb.Text = "nothing";
        }
0
 

Author Comment

by:YASAR_SAID
ID: 16947388
ok thats great but if i want it to be open choice without specifying the type of the control
in the Foo function

if i want to pass any type of control then with in the foo function i figure out the type of the control
wither using select case or IF condition , it doesnt matter but is that possible

0
 
LVL 5

Accepted Solution

by:
lostcarpark earned 100 total points
ID: 16947479
Simple:

        private void Foo(Control tb)
        {
            Type t = typeof(MyClass);
            if (t.Name == TextBox)
            {
                tb.Text = "nothing";
            }
        }

If you want to test for lots of different types, replace the if statement with a switch.
0
 
LVL 5

Expert Comment

by:lostcarpark
ID: 16947491
Oops, that should have been:

        private void Foo(Control tb)
        {
            Type t = typeof(tb);
            if (t.Name == TextBox)
            {
                tb.Text = "nothing";
            }
        }

I should be more careful when I copy and paste!

Note that when you declare the parameter as "Control" you can pass anything inherited from Control. You could declare it as type "Object" and allow anything to be passed in, but the more you do that, the greater the potential for introducing errors the compiler can't catch.

Hope this helps,

James
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16947540
Here are two more ways to determine the type of the parameter passed in:

        private void Foo(Control ctl)
        {
            if (ctl.GetType() == typeof(TextBox))
            {
                this.textBox1.Text = "TextBox: " + ctl.Name;
            }
            else if (ctl.GetType().Equals(typeof(Button)))
            {
                this.textBox1.Text = "Button: " + ctl.Name;
            }
        }
0
 

Author Comment

by:YASAR_SAID
ID: 16947732
thank you all for the great help
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about problems I had with the new location API and permissions in Marshmallow

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question