Best way to add "dynamic" controls? - c#

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.

Related

How do you bind 2 controls?

In my app, there is a feature to customize it's own Controls (like Textbox, Labels, Textblocks, Buttons, etc), this feature interface is located inside a TabItem. Lets say i want to modify Button A, which is located in ANOTHER container. On the feature interface, i set it's Foreground property to White, at this moment i don't know whether the Button looks better or not, so i have to go to the container which contains that Button.
What am i trying to do is, i want to create a "preview" Control (which is the same type as the actual target) inside the feature interface. I want any changes on this "preview" control are reflected to the actual target Control. With this, i won't need to navigate to where the target Control located.
When i used the title ("How do you bind 2 controls)" with google, all results actually gives me "how to bind SINGLE property of a control to another control's property". What i want is how do you bind/link 2 Controls literally, i mean, i want to bind ALL properties of Control A to ALL properties of Control B.
Binding them one by one is one (tiring) way. Is there another way to achieve this?
I would prefer code-behind method.
There is no "fast" way to do this, you will have to bind one by one according to your buisness logic.
Also a binding is not cheap regarding performance so binding each and every property of a control even those you dont explicitly need, is a warning sign.

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)

Generic slideshow getting started

This is a getting started question about how to create a reusable wpf slideshow control:
that displays a sequence of any visualizable elements e.g. a series of Image controls or a series of UserControls (should I target ContentControl, or is there a broader type that encompasses more visualizables/controls?)
the control should be able to accept an IList of some kind, which would be the elements/slides to present
the control should expose an Interval property that determines the duration of each slide, but i dont even know the basics of how to get started with that in terms of offering that property to be configured in xaml?
and what should the container be, if any, for the individual slides/controls that are passed in?
To start with, you should probably create a UserControl which contains an Image control, and perhaps Next/Previous Buttons, and anything else you may need. These would all be laid out as normal using a variety of panels, you could probably style most of it with just a Grid.
After that, your UserControl will implement the ImageSource (your IList, or IEnumerable of images), and your interval as dependency properties. These are then settable in XAML.
You would then write the logic which loads the next image and sets it as the Image's Source property, this could happen in the change event for the ImageSource property. You can then get as advanced as you wish with Image preloading/caching etc.
I've just delved into WPF myself for a "Slideshow" like project where I'm showing customer order numbers on screen for a period of time before showing the next, and using Effect/Transitions/Storyboards to move to the next frame. I found a good article on CodeProject
I used a Grid with 2 rows:
Contains my "Changing area".
Contains static information (logo, controls etc).
Rememeber to set "cliptobounds = true" on your changing area if you use any sort of transforms on it. (I know you said you aren't using transitions initially, but once people see it, they'll be asking).
Dependency properties are also easily built in C# if you just type propdp and hit tab.

Custom items in ListView control

I need to add a custom item to ListView control (like on the picture), is it possible ? And if it is, what's the best way to do it ?
I don't know that this is possible with Winforms. The list items in a System.Windows.Forms.ListView are System.Windows.Forms.ListViewItem objects contained in a strongly typed collection.
You could try to create a subclass of ListViewItem, but since that class inherits directly from System.Object and is not an actual windows form control, you might be borrowing trouble, as you would need to replicate all the functionality of the inheritance chain of an actual control.
Now, if you are not too far in to the project, you might consider looking at switching to WPF. The ListView in WPF uses controls as items, so you could easily create a usercontrol that you would use as your list items.
You might be able to find a control library with a control that gives you the functionality you want, but for the most part, the good libraries are commercial and can be prohibitively expensive for small shops and individual .
I did a quick google search for any library that offers this capability, but I could not find one that displayed custom controls.
Not sure if this is what you're after, but ListViewItems have a Tag property that can store custom data about each item.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.tag.aspx
How about creating a user control. In that case you can set your user controls at the top and listview below. Or are you trying to add these controls as items in the listview itself?

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