Solved

cloning a control ?

Posted on 2006-06-20
9
248 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
...or?

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

        private void Foo(TextBox tb)
        {
            tb.Text = "nothing";
        }
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:YASAR_SAID
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thank you all for the great help
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now