add the same control several times on different parents - c#

I'm trying to add a panel on two different panels in this way:
_formMain.panel3.Controls.Add(_formMain.panel1);
_formMain.panel4.Controls.Add(_formMain.panel1);
What I obtain is that panel1 is added only to panel4 and it is removed from panel3.
It seems that the latest "Add" overwrites the others "Add". Is it true?
Why? How can I add the same panel to some differents controls?
Thank you

Your title says it all:
There is only one control and it can only be in one place, read it can only have one parent.
Therefore, if you change the Parent or Add it to another Control's Controls collection, which is ecxactly the same thing, it will disappear from the previous place.. So while Add doesn't sound like it, it amounts to a Move.
If you need more controls you need to create more controls! And of course they will be different Controls, with different properties and contents..
You can have more than one control show the same content if you keep them synch'ed. One prime example with automatic synchronizing would be two DataGridviews, both with the same DataSource. For other content, like Text or Images the syn'ching is up to you!
You may think about writing a clone function, that can create a deep copy but you will still have to do the syn'ing. This may be codeable as well, depending on the details.. Or you could make it into a UserControl and add fresh instances of it.

Related

Best way to add "dynamic" controls?

My program will prompt the user for a number, i.e. 25. The program will then start the "main form" with 25 controls (textbox). The 25 (or whatever number) of textboxes (or whatever control) will need to be formatted evenly. I will also need to be able to retrieve the text (or another property if I use another control) in order, from left to right and up to down. What is the best method of approaching this?
Using WPF MVVM. In a .XAML file, create a DataTemplate with the DataType of a ViewModel that will provide the binding for your TextBoxs, lets call this the TextboxViewModel. Then using a ItemsControl element with an ItemsSource of TextboxViewModel. You'll be able to instantiate as many TextBoxs as you want and be able to get the result by browsing through your list of TextboxViewModel.
Supposing you are using Windows Forms here.
Dynamically create the X controls and add them to the Controls collection of your form. To ease the access to them you can store their reference in a List and set some event handlers too, depending on your needs. You just need to calculate their positions while you add them.
If WinForms, this is exactly what the FlowLayoutPanel is for. Just add the controls to it and they will arrange themselves automatically, wrapping down to the next row as needed. As Mihai already suggested, you could also keep reference to those controls in a List.
Another option would be to use a TableLayoutPanel. It's a little more difficult to learn and use, but is much more flexible and powerful.

Windows Store App: prevent duplicate code in two almost identical User Controls

So I've got two User Controls in my Windws Store App, that look and work almost the same:
Control 1 http://image-upload.de/image/FbKYsa/319b71644f.png
Control 2 http://image-upload.de/image/Hd9eMW/b78fca7c85.png
The events and event handling on the controls is identical, the only difference is that the second control has an additional ComboBox as you can see.
How can I prevent code duplication and doubled work whenever I change some functionality?
I tried to pack the identical controls into a resource dictionary but could not figure out how to access the control elements in the dictionary's code-behind.
As far as I know I also can't derive the second control, since the first uses XAML.
My last idea would be to use only one class and give a flag to the constructor to add a ComboBox if needed. But it just doesn't appear to be the right way. Any suggestions?
I have just done something similar on a app. My functionality required two text boxes for password entry and the OK, Cancel buttons to be available in both controls, however one of the controls also needed to display Radio Buttons for encoding types (these buttons were in a StackPanel). I added a parameter to the constructor as shown below:
public PasswordInputBox(bool isForImage)
{
this.InitializeComponent();
if (isForImage)
//initialize actions for that part of the EmbedTypePanel;
else
EmbedTypePanel.Visibility = Visibility.Collapsed;
}
Then if you need the control with the extra combo box you can initialize it, else you just collapse it so the user is none the wiser.

C# WinForms add controls programmatically

Can someone suggest the best way to achieve my goal?
So, I have a form with three buttons. What I want is, depending on what button is pressed on panel should be shown different controls (user control). I made this in a simple way: all are added from the beginning, I just make change to the visibility. But what would be nice is, if someone can suggest a more appropriate way, because there is no need to have objects created from beginning.
You can always create the appropriate UserControl, and add it to the Panel.Controls at runtime. This will allow you to create the control(s) as needed, instead of on initialization of your Form.
I would indeed create the controls at design time - if there's advantage no to dynamically create them. Why complicate matters?
If there are a number of controls I would put them all in a panel (within the panel you've already mentioned) so you're only changing the visibility of a single control (the panel) rather than each one within it.
When you press the appropriate button show the appropriate panel (and remember to hide the others in case you've previously shown them)

To Show a Control in two Windows Forms

I have inherited a control from Panel-Class.
I have added some events to this control. I gave move - ability to this control
and so on ..
I have two display screens. I have a main program where the inherited
panel is displaying an image on a small area. I want to show this panel
fullscreen on a second.
I created a new form and use the same control... But i can not move both screens
together. What should I do ?
If you want to be able to manipulate both forms at the same time, show the second form with Show() instead of ShowDialog(). You can certainly pass the original panel to the second form and add it to the form's Controls collection. I am not sure if this is the best way to do it (sharing one control across two forms), but I don't know your requirements either.
I wouldn't use a second form, but a second 'mode' (fullscreen vs. not) on your existing form.
You can have 2 panel controls, or just one and resize.
I think this kind of behaviour calls for a model-view pattern. If that's implemented, the rest should fall into place.
The problem is that you only have one instance of your inherited panel. You actually have to make another "copy" of it, a new instance, before you can add it to the other form.
Mypanel mypanel1 = new Mypanel();
Mypanel mypanel1copy = new Mypanel();
You can either edit these instances to contain the same data all the time through the run, or use something like "Deep Copy":
How do you do a deep copy of an object in .NET (C# specifically)?
Keep in mind, that any changes to mypanel1 should be done to mypanel1copy, too.

How to display the same control on two different tabs?

I'm using VB.NET
I need same control (ListBox) to be displayed on 2 different tabs.
Is it mandatory to create 2 different ListBox instances?
If you don't need design-time support you can simply, at runtime, change the ListBox instance's Parent from one tab to the other (making sure to set the positioning appropriately, of course).
Essentially, it's:
listBox1.Parent = tabControl1.TabPages[1];
In the end though, you'll probably find it easier to just have two ListBox's with the same data source.
Yes, I think you'll need a ListBox control on each tab. If they have the same data you can use the same DataSource for both though.
Yes, add a new instance on each tab.
If you want full designer support, you'll need two boxes. If doing it in code is enough, you can create a single listbox on form load, and manually add a reference to it to each tab page.

Categories