none
How to add Controls to ListView Control??/ RRS feed

  • Question

  • Hi Im trying to develop a calendar control that is similar to outlook. Initially I was planning to use a listview and add the controls on the listview whenever an appointment is created. But the control is exactly not added to the listview but to the form. When I change the size of the form then the controls obviously are out of place. I was wondering is there a way to add a control to ListView. Or is there any other control than listview that fulfills my requirments. Your help wil be appreciated. I'm using C# windows Forms.
    Wednesday, April 26, 2006 5:59 PM

Answers

  • You can indeed add controls to a ListBox as well as many other controls out there.

    Take a look at the Add() method on the Controls collection property of your ListBox. To add say a Button it is as simple as:

                listView1.Controls.Add(new Button());

    of course you probably want to tweak it before it goes into the collection so you can expand things a bit like this:

                Button b = new Button();
                b.Name = "Some Name";
                b.Text = "Button Text";
                //other setup here
                listView1.Controls.Add( b );

    Take a look at this MSDN page for more details. It deals with doing this with a ListBox, thankfully doing so in a ListView is nearly identical.

    Wednesday, April 26, 2006 6:43 PM

All replies

  • Maybe you can find something useful here:

    http://www.windowsforms.com/Default.aspx?tabindex=3&tabid=49

    Tony

    Wednesday, April 26, 2006 6:37 PM
  • You can indeed add controls to a ListBox as well as many other controls out there.

    Take a look at the Add() method on the Controls collection property of your ListBox. To add say a Button it is as simple as:

                listView1.Controls.Add(new Button());

    of course you probably want to tweak it before it goes into the collection so you can expand things a bit like this:

                Button b = new Button();
                b.Name = "Some Name";
                b.Text = "Button Text";
                //other setup here
                listView1.Controls.Add( b );

    Take a look at this MSDN page for more details. It deals with doing this with a ListBox, thankfully doing so in a ListView is nearly identical.

    Wednesday, April 26, 2006 6:43 PM
  • Hi

    Thanks for the Reply. Like Brendan said I already added those things Like I mentioned before the problem was when the listview has a scrollbar and when I move the scrollbar the Label stays at the specified location. It doesn't move along with the ListView.

    Label lbl = new Label();

    lbl.Text = "John Doe";

    lbl.Width = 64;

    lbl.Height = 40;

    lbl.BackColor = Color.Blue;

    lbl.Location = new Point(e.X, e.Y);

    lvwScheduling.Controls.Add(lbl);

    lbl.BringToFront();

    lvwScheduling.SendToBack();

     

    Wednesday, April 26, 2006 6:58 PM
  • From your description it appears that you are putting your control in a specific location and never moving it again... so of course it will stay in the same location, it doesn’t know where or how to move, nor does the container know.

    You may want to think about creating your own custom ListView class that handles the movement of the controls internally so as to simply your interaction with them and the overall control.

    Wednesday, April 26, 2006 7:10 PM
  • Thatz the reason why I posted in this MB.

    Thanks for ur reply brendan.

     

    Wednesday, April 26, 2006 7:26 PM
  • hi

    my problem is that . Actually i want to add multiple custom controls in a listview . but a single first  one added control is displayed .
    please help me .
    Thursday, June 26, 2008 8:46 AM
  • but how to generate event on that button click can you please mail some source of event click that get id of row and perform update or some thing else here or at engineernouman786@gmail.com
    Thursday, December 24, 2015 6:30 PM
  • but how to generate event on that button click can you please mail some source of event click that get id of row and perform update or some thing else here or at engineernouman786@gmail.com
    Thursday, December 24, 2015 6:32 PM
  • this thread had been answered

    if you has a question, please create a new question thread, 


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    Friday, December 25, 2015 2:43 AM