0

I need to create and add some TextBoxes which has same attribute as some other TextBoxes. Is there a way to copy the attributes to another ?

I'm looking for a one like solution. I know I can set variable one by one.

TextBox Old = new TextBox() {
                             Size = new System.Drawing.Size(25,25),
                             Location = new Point(a.row*25, a.col*25),
                             Multiline = true
                             };


TextBox New = new TextBox(); //which has same location,size as old one ?

EDIT The TextBox might be any other .NET controls !

Mohsen Sarkar
  • 5,910
  • 7
  • 47
  • 86

3 Answers3

1

You can use this Solution. You can write a extention and that get via Reflection all propertys

Please use the search function in future.

Community
  • 1
  • 1
Venson
  • 1,772
  • 17
  • 37
  • @Mahdi that is how AutoMapper works. I would not go down the road of rolling your own, I'd leverage an existing solution. – CubanX Feb 16 '13 at 12:49
0

Probably the most straightforward way is this:

TextBox New = new TextBox {
  Size = Old.Size,
  Location = Old.Location,
  Multiline = Old.Multiline
};

If this is something you need to do a lot, you could write an extension method that does the same thing:

public static class TextBoxExtensions {
  public static TextBox Copy(this TextBox textBoxToCopy) {
    var copiedTextBox = new TextBox {
      copiedTextBox = textBoxToCopy.Size,
      copiedTextBox = textBoxToCopy.Location,
      copiedTextBox = textBoxToCopy.Multiline
    };
  }
}

Usage:

var copyOfOld = Old.Copy();

If you are going to add a lot more properties to copy, I'd think about using AutoMapper and defining a map between TextBox and TextBox. If you're interested in that path, let me know and I'll post a sample.

It would turn this into a one liner, but you'd need a dependency on AutoMapper, but it's available on NuGet: http://nuget.org/packages/AutoMapper/2.2.0

First, take a dependency on AutoMapper.

Define the mapping somewhere in your project:

Mapper.CreateMap<TextBox, TextBox>();

Usage:

var newTextBox = Mapper.Map<TextBox, TextBox>(Old);

or, if you already have an instance you want to stuff it into:

Mapper.Map(Old, newTextBox);

AFAIK, there is no built in, one line solution, so it's either the extension method, or take a dependency on AutoMapper. The extension method does not have to do it that way, you can use reflection or other choices there.

I use AutoMapper in just about all of my projects and it's invaluable.

You can define many mappings in your map definition, then all your copies become one liners. Well, besides the definition :)

CubanX
  • 5,176
  • 2
  • 29
  • 44
0

Create an initializer method:

private void InitializeTextBox(TextBox textBox)
{
    textBox.Size = new System.Drawing.Size(25, 25);
    textBox.Location = new Point(a.row * 25, a.col * 25);
    textBox.Multiline = true;
}

And use like this:

TextBox t1 = new TextBox(), t2 = new TextBox();
InitializeTextBox(t1);
InitializeTextBox(t2);

Or a copier method:

private void CopyTextBoxProps(TextBox source, TextBox dest)
{
    dest.Size = source.Size;
    dest.Location = source.Location;
    dest.Multiline = source.Multiline;
    //...
}

and use it accordingly.

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72