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();
}
}
}
}
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();
}
}
}
}