C# multiple user controls or listbox

Posted on 2004-04-04
Last Modified: 2008-03-10
I am trying to create a listbox with much more extended information.  The best way for me to show you what I mean is to show you another program with this so lets take this one for example

as you can see there is a list of objects that you can select from in a listview type but its not a basic listview control.  I am wondering how to make this.  Should I make a user control then put them into a listview or what?  I am not sure how to do any of this.  Please help


Question by:ICaldwell
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
  • 6
  • 3
  • 2

Expert Comment

ID: 10754459
The particular screen shot you referenced can be done fairly easily with the repeater class.

I hope this helps.

Expert Comment

ID: 10755700
The repeater is only for web apps - i suppose you could embed a browser and manipulate it that way..

I'd of thought a datagrid with some custom columns would be a better winforms approach;

This article explains how to a picture column to a datagrid

The link is from my collection of 'useful snippets for later....'   I have a similar need to what you require coming up......

Expert Comment

ID: 10756698
First of all, sorry, I was thinking all the way.  Talk about missing the boat!

A grid control will do, but you'll not have much fun trying to group multiple rows to give the same effect as the screen shot you reference.  The grid can handle a single row fairly easily.

I would take a look at a panel control, which embeds a collection of panels containing your controls (or some other control container).  The containing panel control can scroll, you can populate programmatically the contained panel with the controls you need.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 11

Author Comment

ID: 10757709
ok, sounds like a panel control is the best way to do what I am looking for.  I know how to add and remove one pannel but not multiple.  Would it be best to create the panel in a user control and the user control to list?  and how do I add multiple controls to a scrollable list?


Expert Comment

ID: 10761918
Yes, you would design the contained panel as a user control.  The contained panel (user control) will have the controls placed in it that you want to display.  You would then populate the outer panel control with a collection of the contained panels (user controls).  You can create multiple instances of the user control you design using the new operator and add them to the panel programmatically.

The code for the contained control would be responsible for reading the data from a DataRow, and would also be responsible for any updates to the database.

As for your last question, you would use the CreateControlCollection method of the outer panel to add controls:
// Create child controls.
ChildControl myControl[i] = new ChildControl();


This is just pseudo code, so don't try to cut and paste.

Hope this helps.
LVL 11

Author Comment

ID: 10771301
Ok, I understand how to create the array of controls and how to add them to a control but th part you mentioned about CreateControlCollection(); I do not.  I tried looking up info on google about this but there is very little.  I am not sure how to add these controls multiple controls, could you add some code for this or something?



Expert Comment

ID: 10771394
google's fair for .Net stuff, but you need to download the .Net SDK and look at the docs.  If you don't want to do that, then is the only way to go.  Here's a reference that presents the code you need:

I basically use the online version when I have access to it, because it gets updated.  Otherwise, the docs that ship with the .Net SDK are fabulous with many samples and quickstarts.

Here's another attempt:
outerPanel.controls.add(new containedPanel(parm1, parm2...))

You would simply create a user control named containedPanel, which contains the text boxes, check boxes, etc that you want to display.  The contained panel would look like one row of data on your display.
Then to populate your scrolling panel (outerPanel) you would loop through your database secords, populating new containedPanel controls and add them to the controls collection.  As the new containedPanel controls exceed the view area of the outerPanel, scroll bars are added and you get a scrolling display.

Step one: design a user control containedPanel based on a panel control.  Add your other controls to the usercontrol so it looks like one row of the picture you referenced.  (You can do this in visual studio or the old fashioned way with notepad.  If you don't have visual studio, steal a copy.)

Step two.  Open a datatable or some other data source (an array collection, etc.)
Step three.  Loop through the dataset and execute
outerPanel.controls.add(new containedPanel(parm1, parm2...))
Do this for each record.

If you create the outerPanel programmatically, you would use the CreateControlCollection().  As in outPanel.CreateControlCollection.  The you would still have to loop through your data and execute
outerPanel.controls.add(new containedPanel(parm1, parm2...)).

Good luck.
LVL 11

Author Comment

ID: 10771649
that msdn site was the one that I hit.  Ok here is a little more info

I am using Visual Studio 2003 to create this

I created a user control with a panel in it containing all the labels, ect.

when the program loads it runs

LineItem[] myLineItem = new LineItem[5];
myLineItem[0] = new LineItem("sadasd, sadasd");
myLineItem[1] = new LineItem("asdasda,as dasd");
myLineItem[2] = new LineItem("asdasd,as dasd");
myLineItem[3] = new LineItem("sdhkjas, ashdasdhasdjwe");
myLineItem[4] = new LineItem("asdas,a sdhauihwe");

This is just basic to see if it worked, no loop yet.  I only see the first one appear, if I remove panel6.Controls.Add(myLineItem[0]); then I see only the second one.  ect.  Each one will show up individually but not together

I must be doing something wrong here but not sure what


Accepted Solution

codewiz51 earned 50 total points
ID: 10773903
OK, I see.  You have to position the controls in the panel.  You can adjust the myLineItem position relative to the top, left position of the panel.

Something like:
myLineItem[0].positionx = 1;
myLineItem[0].positiony = 1;
myLineItem[1].positionx = 1;
myLineItem[1].positiony = 25;
myLineItem[4].positionx = 1;
myLineItem[4].positiony = 100;
// Where each line item has a height of 25 pixels.

If you have questions.  Just draw a panel on a form, then add a few panels into the container.  Then, switch to code view a look at how the designed positioned the panels inside the container panel.  You may need to add a couple of properties to your panel control to allow it to be repositioned.

Expert Comment

ID: 10774474
you could just set the dock property of the subpanel to DockStyle.Top ; then the outer panel will keep track of the positioning for you.

Expert Comment

ID: 10774864

Here's some code that does what you want:

// Place this just after the class definition of your form
private System.Windows.Forms.Panel outerPanel;
private System.Windows.Forms.Panel containedPanel1;
private System.Windows.Forms.TextBox inContainedPanel1;
private System.Windows.Forms.Panel containedPanel2;
private System.Windows.Forms.TextBox inContainedPanel2;
private System.Windows.Forms.Panel containedPanel3;
private System.Windows.Forms.TextBox inContainedPanel3;
private System.Windows.Forms.Panel containedPanel4;
private System.Windows.Forms.TextBox inContainedPanel4;
private System.Windows.Forms.Panel containedPanel5;
private System.Windows.Forms.TextBox inContainedPanel5;
private System.Windows.Forms.Panel containedPanel6;
private System.Windows.Forms.TextBox inContainedPanel6;

// Place this code in your form's constructor function
this.outerPanel = new System.Windows.Forms.Panel();
this.containedPanel1 = new System.Windows.Forms.Panel();
this.inContainedPanel1 = new System.Windows.Forms.TextBox();
this.containedPanel2 = new System.Windows.Forms.Panel();
this.inContainedPanel2 = new System.Windows.Forms.TextBox();
this.containedPanel3 = new System.Windows.Forms.Panel();
this.inContainedPanel3 = new System.Windows.Forms.TextBox();
this.containedPanel4 = new System.Windows.Forms.Panel();
this.inContainedPanel4 = new System.Windows.Forms.TextBox();
this.containedPanel5 = new System.Windows.Forms.Panel();
this.inContainedPanel5 = new System.Windows.Forms.TextBox();
this.containedPanel6 = new System.Windows.Forms.Panel();
this.inContainedPanel6 = new System.Windows.Forms.TextBox();
// outerPanel
this.outerPanel.AutoScroll = true;
this.outerPanel.AutoScrollMinSize = new System.Drawing.Size(0, 82);
this.outerPanel.Location = new System.Drawing.Point(16, 16);
this.outerPanel.Name = "outerPanel";
this.outerPanel.Size = new System.Drawing.Size(400, 208);
this.outerPanel.TabIndex = 0;
// containedPanel1
this.containedPanel1.Location = new System.Drawing.Point(8, 8);
this.containedPanel1.Name = "containedPanel1";
this.containedPanel1.Size = new System.Drawing.Size(368, 56);
this.containedPanel1.TabIndex = 0;
// inContainedPanel1
this.inContainedPanel1.Location = new System.Drawing.Point(144, 16);
this.inContainedPanel1.Name = "inContainedPanel1";
this.inContainedPanel1.Size = new System.Drawing.Size(192, 20);
this.inContainedPanel1.TabIndex = 0;
this.inContainedPanel1.Text = "inContainedPanel1";
// containedPanel2
this.containedPanel2.Location = new System.Drawing.Point(8, 64);
this.containedPanel2.Name = "containedPanel2";
this.containedPanel2.Size = new System.Drawing.Size(368, 72);
this.containedPanel2.TabIndex = 1;
// inContainedPanel2
this.inContainedPanel2.Location = new System.Drawing.Point(144, 24);
this.inContainedPanel2.Name = "inContainedPanel2";
this.inContainedPanel2.Size = new System.Drawing.Size(192, 20);
this.inContainedPanel2.TabIndex = 1;
this.inContainedPanel2.Text = "inContainedPanel2";
// containedPanel3
this.containedPanel3.Location = new System.Drawing.Point(8, 136);
this.containedPanel3.Name = "containedPanel3";
this.containedPanel3.Size = new System.Drawing.Size(368, 72);
this.containedPanel3.TabIndex = 0;
// inContainedPanel3
this.inContainedPanel3.Location = new System.Drawing.Point(144, 24);
this.inContainedPanel3.Name = "inContainedPanel3";
this.inContainedPanel3.Size = new System.Drawing.Size(192, 20);
this.inContainedPanel3.TabIndex = 1;
this.inContainedPanel3.Text = "inContainedPanel3";
// containedPanel4
this.containedPanel4.Location = new System.Drawing.Point(8, 208);
this.containedPanel4.Name = "containedPanel4";
this.containedPanel4.Size = new System.Drawing.Size(368, 72);
this.containedPanel4.TabIndex = 3;
// inContainedPanel4
this.inContainedPanel4.Location = new System.Drawing.Point(144, 24);
this.inContainedPanel4.Name = "inContainedPanel4";
this.inContainedPanel4.Size = new System.Drawing.Size(192, 20);
this.inContainedPanel4.TabIndex = 1;
this.inContainedPanel4.Text = "inContainedPanel4";
// containedPanel5
this.containedPanel5.Location = new System.Drawing.Point(8, 280);
this.containedPanel5.Name = "containedPanel5";
this.containedPanel5.Size = new System.Drawing.Size(368, 72);
this.containedPanel5.TabIndex = 4;
// inContainedPanel5
this.inContainedPanel5.Location = new System.Drawing.Point(144, 24);
this.inContainedPanel5.Name = "inContainedPanel5";
this.inContainedPanel5.Size = new System.Drawing.Size(192, 20);
this.inContainedPanel5.TabIndex = 1;
this.inContainedPanel5.Text = "inContainedPanel5";
// containedPanel6
this.containedPanel6.Location = new System.Drawing.Point(8, 352);
this.containedPanel6.Name = "containedPanel6";
this.containedPanel6.Size = new System.Drawing.Size(368, 72);
this.containedPanel6.TabIndex = 5;
// inContainedPanel6
this.inContainedPanel6.Location = new System.Drawing.Point(144, 26);
this.inContainedPanel6.Name = "inContainedPanel6";
this.inContainedPanel6.Size = new System.Drawing.Size(192, 20);
this.inContainedPanel6.TabIndex = 2;
this.inContainedPanel6.Text = "inContainedPanel6";
// Form1
// Change Form1 to you your form's name!
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(472, 237);
this.Name = "Form1";
this.Text = "Form1";


Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
itextsharp with c# 3 35
C# LINQ 5 45
How do I "share" on social sites? 2 40
Which is best Image Resizing Web service 11 42
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 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