Click to See Complete Forum and Search --> : ComboBox within the ListView


pre_wreck
May 1st, 2007, 09:48 PM
I'm just wondering how can I make or how can I put a combobox within my listview. And I search over the web, and fortunate I got this at http://www.dotnet247.com/247reference/message.aspx?id=71810. however, I still found some build errors through the code. Please help me sove this please.

Here's the piece of code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication5
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private ListViewItem MyItem;

private System.Windows.Forms.ListView clcmainlistview1;
private System.Windows.Forms.ComboBox clcmaincombobox1;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;


public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.clcmainlistview1 = new System.Windows.Forms.ListView();
this.clcmaincombobox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// clcmainlistview1
//
this.clcmainlistview1.Location = new System.Drawing.Point(24, 136);
this.clcmainlistview1.Name = "clcmainlistview1";
this.clcmainlistview1.Size = new System.Drawing.Size(200, 64);
this.clcmainlistview1.TabIndex = 0;
//
// clcmaincombobox1
//
this.clcmaincombobox1.Location = new System.Drawing.Point(24, 104);
this.clcmaincombobox1.Name = "clcmaincombobox1";
this.clcmaincombobox1.Size = new System.Drawing.Size(104, 21);
this.clcmaincombobox1.TabIndex = 1;
this.clcmaincombobox1.Text = "comboBox1";
this.clcmaincombobox1.SelectedIndexChanged += new System.EventHandler(this.clcmaincombobox1_SelectedValueChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.clcmaincombobox1);
this.Controls.Add(this.clcmainlistview1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
int x;

// Add a few items to the combo box list
this.clcmaincombobox1.Items.Add("Male");
this.clcmaincombobox1.Items.Add("Female");

// Set view of listview to list
this.clcmainlistview1.View = View.List;

// Turn the grid on for reference
this.clcmainlistview1.GridLines = true;

// Add some data to the listview
for (x = 1; x < 100; x++)
{
clcmainlistview1.Items.Add(new System.Windows.Forms.ListViewItem("Test" +
x.ToString()));
}

}
private void clcmaincombobox1_KeyUP(object sender, KeyEventArgs e)
{
// Check if user types "ESC"
if (e.KeyCode == Keys.Escape)
{
// Hide the combobox
this.clcmaincombobox1.Visible = false;
}

}

private void clcmaincombobox1_SelectedValueChanged(object sender, System.EventArgs e)
{
// Set text of listview item
MyItem.Text = this.clcmaincombobox1.Text;
//this.clcmaincombobox1.Text = "";

// Hide the combobox
this.clcmaincombobox1.Visible = false;

}
private void clcmainlistview1_MouseUp(object sender, KeyEventArgs e)
{

// Get the item that was clicked
MyItem = this.clcmainlistview1.GetItemAt(e.X, e.Y);

// Check if an actual item was clicked
if (MyItem != null)
{
Rectangle ClickedItem;

// Get the bounds of the item clicked
ClickedItem = MyItem.Bounds;

// Adjust the top and left of the control
ClickedItem.X += this.clcmainlistview1.Left;
ClickedItem.Y += this.clcmainlistview1.Top;

// Set combobox bounds to match calculation
this.clcmaincombobox1.Bounds = ClickedItem;

// Set the default text for the combobox to be
// the clicked item's text
this.clcmaincombobox1.Text = MyItem.Text;

// Show the combobox and make sure it is on top
// and give focus to the combobox
this.clcmaincombobox1.Visible = true;
this.clcmaincombobox1.BringToFront();
this.clcmaincombobox1.Focus();
}
}

}
}

cjard
May 2nd, 2007, 11:58 AM
People seeking an easier life would probably just use a DataGridView with a DataGridViewComboBoxColumn

pre_wreck
May 3rd, 2007, 08:21 AM
People seeking an easier life would probably just use a DataGridView with a DataGridViewComboBoxColumn
Thanks for the reply.
I think theres a way to do it in listview. I'm sorry but my study all about Listview so I think I don't have any choice.

Please help.

Shuja Ali
May 3rd, 2007, 10:03 AM
Thanks for the reply.
I think theres a way to do it in listview. I'm sorry but my study all about Listview so I think I don't have any choice.

Please help.
DataGridView is better and easier. Why do you want to make things complicated?

pre_wreck
May 5th, 2007, 12:34 AM
DataGridView is better and easier. Why do you want to make things complicated?I've already done that, yes I agree thats more easier than doing what i want. My point is, this is my need. why should i bother this thing im not interested. Let say for example, why should I ate rice, if theres a bread here? The thing is, bread can not satisfy my appetite, why should i go and ate some bread? In short satisfaction is key why should I ask this if it is possible.

I tell this bro, I'm fun of doing complicated things possible. Sorry about that, but this is my satisfaction.

Now if its not possible then so be it.

But I think everything is possible in programming. Let me think of possibilities if this is really possible or is there any none complecated solution for this and I post it here later.

Thanks for the help.

hitai
August 14th, 2007, 11:10 PM
So hav you found a solution? i would really like to know how if it can be done