Solved

cloning a control ?

Posted on 2006-06-20
9
287 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 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 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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 …

707 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