A C# list of custom childs similar with android ListView - c#

I need to create a list in c# with click-able and customizable child elements.
Check attached image (red square) to get a feeling of what I would like to end up with. I tested on a control called "list view". I can add an icon and a label, it is not enough, I need more customization.
So question: Which c# control should I use? Maybe share a link if you know something useful.
Note: I don't really know or enjoy C# (I am Java guy), as IDE I use Visual Studio C#, express edition and I would love to minimize the coding part as much as possible

First, create a user control (says ContactRow.cs)
Draw what ever UI that you need on it. (Image, text, etc)
Secondly, Create a container form (says ContactList.cs)
Add a TableLayoutPanel (Or FlowLayoutPanel Or equivalent) into container form.
Set AutoScroll property = true so that it scrolls.
In run time, you can new an instance of your ContactRow.cs and set all the values.
Then add it into the TableLayoutPanel using the TableLayoutPanel.Controls.Add(contactrow)

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.

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)

Adding Panels to a Form in C# during execution

I've been diving into C# for the first time by trying to recreate a screensaver that I made in Java comprised of a grid of panels which change color randomly. So far, I've gotten the code for the panels to work, and now I'm trying to add them to a Form to prototype the layout. I plan to determine the number of panels to be displayed at runtime (so that 16:9, 4:3, 16:10, etc. can all make the screensaver look good), but the only ways that I've found from searching the internet to add elements to a Form involve using Visual Studio's design tools. Therefore, I have a few questions:
How can I set up the layout of the Form in something akin to Java's GridLayout?
What's the code needed to add elements to a Form?
Is there a better thing for me to be using instead of a Form?
You can add panels to a form at runtime the same way the designer does - construct the Panel, and add it to the form (via this.Controls.Add(thePanel);).
The easiest way to see the appropriate code is to add a panel to the form using the designer, then open up the "YourForm.designer.cs" file. The designer just generates the required code for you - but you can see the exact code required to duplicate what the designer would create.
As for the layout, I'd recommend watching the Layout Techniques for Windows Forms Developers video. It may give you some good clues as to the various options available for layout. There is nothing exactly like Java's GridLayout, though there are some open source projects that attempt to duplicate this functionality.
You definitely are gong to need to create a form application to get this to work. In addition, every control you see in the designer, can be added programmatically.
You could have a method that cranks out new panels as needed....
Here is the code that will create a new panel:
Panel panel1 = new Panel();
Once it is declared, you can access all the properties.
To add the panel to the form you would do something like this....
myform.controls.add(panel1);
So knowing that, you can create a method that will format your panel and return or add it tor the form....
You'll want to use TableLayoutPanel or something along those lines. Then you can use the Controls property to add panels to it.

Dynamic Windows Form Control Creation - General Question

I'm working with a GUI in PowerShell and trying to make the form such that, after the form is built, the user can interact with the part of the form (+/- button) to dynamically add a group of controls to the form underneath of the existing set.
As an example, there will be a combobox (drop-down) to select a parameter and then a text field to fill in the value for the selected parameter.
What I'm looking for is a way to dynamically add that group of controls to the form when the user clicks the plus button. My current attempts have failed. I've tried pinning the controls inside of a TableLayoutPanel and a Panel object but can't seem to get the items to appear in the first place and then appear in some semblence of order. Is there a GUI object that I can pin the controls to that will just auto-dump the next row of controls below the existing or do I have to figure out the calculations off of the docked parent and offset the Y-values?
Does anyone know a PowerShell accessible Windows form control that would support this type of additions?
One of my attempts is psoted here: .NET 4 (WinForms) Dynamic Form Control Creation in Powershell
Any help is greatly appreciated.
Maybe you could use FlowLayoutPanel. Here is the sample script for using FlowLayoutPanel in Powershell
It's not Winforms, but there is a new module up on Codeplex called ShowUI, which enables you to create WPF based UI's in PowerShell. Take a look at http://showui.codeplex.com. There is some examples up there on using Grid controls and automatic layouts, that I think could meet your needs if you are willing to jump into WPF.
Got the answer from a Microsoft PowerShell forum Moderator:
http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/b2c8abeb-3ae6-4936-9bef-a50a79fdff45/
it's the datagridview object that needs to be used.
Can't you simply build your form (for example with Sapiens free editor tool) with all the controls invisible, and then according to the user choice make them visible ?

Building Controls at Runtime

I have a section of a form where the controls (text boxs, labels etc) need to be built at run time depending on options a user has selected. There will probably be about 7 - 10 different layouts in all.
What's the best way to go about creating and maintaining them?
Cheers
Luke
It would be helpful to know more about the specifics of your situation (what kind of options are we talking about?)
But off the top of my head, I'd guess you probably want to create a set of Panels which would contain the appropriate controls then hide or show them depending on the options.
I have actually had to do just this. I did it with a set of panels (as #David suggests) and also a TreeView. Using the tree view, I customized the visuals to make them mimic an options menu in Microsoft Office, and then I show the appropriate panel based on the user's selection of nodes. If you'd like to see code samples let me know.
All WinForms controls has corresponding classes (Button, Link, EditBox etc.) You can create any controls you want and attach them to form.
Inside the form Init you can add new controls into Controls collection.
public void Init()
{
this.Controls.Add(new TextBox());
}
Some more details in MSDN:
http://msdn.microsoft.com/en-us/library/0h5y8567.aspx

Categories