Solved

cloning a control ?

Posted on 2006-06-20
9
263 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

813 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

18 Experts available now in Live!

Get 1:1 Help Now