?
Solved

cloning a control ?

Posted on 2006-06-20
9
Medium Priority
?
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1600 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 86

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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 86

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 400 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 86

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: 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.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes
Suggested Courses

743 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