|
-
October 15th, 2010, 09:17 AM
#1
Double click on listview subitems not working properly
Hi
I've created a class from a listview control which has several columns. What I want to happen is when you double click on an item in the listview it will open a particular input control. The first column is a combo box which works fine, the second is a textbox which works fine, however, I have tried to add a textbox to the 3rd column (txtNextdel). The problem I am getting is when I double click the text in the third column it is opening the textbox on the second column ? I can not see any reason for this.
No matter where I click after the 1st column it seems to only register the 2nd column. I think its got something to do with the selectedSubItem, I seem to remember this part being split into an array in Foxpro years back, cant really remember though.
I've attached the code, any help would be greatly appreciated here. Thanks. Btw its Visual Studio .NET 2010.
Code:
namespace WindowsApplication1
{
public class ListViewWithComboBox : ListView
{
mailorder.mailprocess mailc;
private DataTable productlists;
private string last_search_criteria;
private ListViewItem item;
string subItemText = "";
int selectedSubItem = 0;
private int X = 0;
private int Y = 0;
private System.Windows.Forms.ComboBox cmbProducts = new System.Windows.Forms.ComboBox();
private System.Windows.Forms.TextBox txtDespquan = new System.Windows.Forms.TextBox();
private System.Windows.Forms.TextBox txtNextdel = new System.Windows.Forms.TextBox();
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.ColumnHeader columnHeader4;
private System.Windows.Forms.ColumnHeader columnHeader5;
private System.Windows.Forms.ColumnHeader columnHeader6;
private System.Windows.Forms.ColumnHeader columnHeader7;
string trust_id;
bool search_lock = false;
public ListViewWithComboBox(string connection_string, double vatx, string trust_idx)
{
// set cmbProducts (comboBox1)
trust_id = trust_idx;
mailc = new mailorder.mailprocess(connection_string, vatx);
cmbProducts.Size = new System.Drawing.Size(0, 0);
cmbProducts.Location = new System.Drawing.Point(0, 0);
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.cmbProducts });
cmbProducts.SelectedIndexChanged += new System.EventHandler(this.cmbProductsSelected);
cmbProducts.LostFocus += new System.EventHandler(this.cmbProductsLostFocus);
cmbProducts.KeyUp += new KeyEventHandler(this.ProductKeyPress);
cmbProducts.DropDownStyle = ComboBoxStyle.DropDown;
cmbProducts.Hide();
// set txtDespquan
txtDespquan.Size = new System.Drawing.Size(0, 0);
txtDespquan.Location = new System.Drawing.Point(0, 0);
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.txtDespquan });
txtDespquan.LostFocus += new System.EventHandler(this.txtDespQuanLostFocus);
txtDespquan.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtDespquanKeyUp);
txtDespquan.Hide();
// set txtNextdel
txtNextdel.Size = new System.Drawing.Size(0, 0);
txtNextdel.Location = new System.Drawing.Point(0, 0);
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.txtNextdel });
txtNextdel.LostFocus += new System.EventHandler(this.txtNextdelLostFocus);
txtNextdel.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtNextdelKeyUp);
txtNextdel.Hide();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
this.columnHeader7 = new System.Windows.Forms.ColumnHeader();
this.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3,
this.columnHeader4,
this.columnHeader5,
this.columnHeader6,
this.columnHeader7
});
this.Name = "listViewWithComboBox1";
this.Size = new System.Drawing.Size(0, 0);
this.TabIndex = 0;
this.View = System.Windows.Forms.View.Details;
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ListViewMouseDown);
this.DoubleClick += new System.EventHandler(this.ListViewDoubleClick);
this.columnHeader1.Text = "Product Description";
this.columnHeader1.Width = 300;
this.columnHeader1.Tag = "1";
this.columnHeader2.Text = "Desp";
this.columnHeader2.Width = 50;
this.columnHeader2.Tag = "2";
this.columnHeader3.Text = "Next Del.";
this.columnHeader3.Width = 100;
this.columnHeader3.Tag = "3";
this.columnHeader4.Text = "Total Item Value";
this.columnHeader4.Width = 100;
this.columnHeader4.Tag = "4";
this.columnHeader5.Text = "VAT Payable";
this.columnHeader5.Width = 100;
this.columnHeader5.Tag = "5";
this.columnHeader6.Text = "Net Line Value";
this.columnHeader6.Width = 100;
this.columnHeader6.Tag = "6";
this.columnHeader7.Text = "Item Price (ex. VAT)";
this.columnHeader7.Width = 100;
this.columnHeader7.Tag = "7";
}
private void ProductKeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode != Keys.F4 && e.KeyCode != Keys.Up && e.KeyCode != Keys.Down && e.KeyCode != Keys.F5 && e.KeyCode != Keys.F3 && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right)
{
if (e.KeyValue == 13)
{
if (cmbProducts.SelectedValue.ToString() != "")
{
// update_labels_func();
// txtDespquan.Focus();
// label6.Text = cmbProducts.SelectedValue.ToString();
search_lock = true;
}
}
if (e.KeyValue == 27)
{
cmbProducts.Hide();
return;
}
if (cmbProducts.Text.Length == 0)
{
initialise_products_list();
}
if (cmbProducts.Text.Length >= 3)
{
this.columnHeader1.Text = this.columnHeader1.Text + "*";
search_and_bind_data(cmbProducts.Text.Trim());
}
}
}
private void txtDespquanKeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Escape)
{
txtDespquan.Hide();
}
}
private void txtNextdelKeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Escape)
{
txtNextdel.Hide();
}
}
private void cmbProductsSelected(object sender, System.EventArgs e)
{
//int i = cmbProducts.SelectedIndex;
//if (i >= 0)
//{
// string str = cmbProducts.Items[i].ToString();
// item.SubItems[selectedSubItem].Text = str;
//}
}
private void CountrySelected(object sender, System.EventArgs e)
{
//int i = comboBoxCountries.SelectedIndex;
//if (i >= 0)
//{
// string str = comboBoxCountries.Items[i].ToString();
// item.SubItems[selectedSubItem].Text = str;
//}
}
private void cmbProductsLostFocus(object sender, System.EventArgs e)
{
cmbProducts.Hide();
}
private void txtDespQuanLostFocus(object sender, System.EventArgs e)
{
txtDespquan.Hide();
}
private void txtNextdelLostFocus(object sender, System.EventArgs e)
{
txtNextdel.Hide();
}
public void ListViewDoubleClick(object sender, System.EventArgs e)
{
// Check whether the subitem was clicked
int start = X;
int position = 0;
int end = this.Columns[0].Width;
for (int i = 0; i < this.Columns.Count; i++)
{
if (start > position && start < end)
{
selectedSubItem = i;
break;
}
position = end;
end += this.Columns[i].Width;
}
subItemText = item.SubItems[selectedSubItem].Text;
string column = this.Columns[selectedSubItem].Tag.ToString();
MessageBox.Show(this.Columns[selectedSubItem].Text);
if (column == "1")
{
Rectangle r = new Rectangle(position, item.Bounds.Top, end, item.Bounds.Bottom);
cmbProducts.Size = new System.Drawing.Size(end - position, item.Bounds.Bottom - item.Bounds.Top);
cmbProducts.Location = new System.Drawing.Point(position, item.Bounds.Y);
cmbProducts.Show();
cmbProducts.Text = subItemText;
cmbProducts.SelectAll();
cmbProducts.Focus();
}
else if (column == "2")
{
Rectangle r = new Rectangle(position, item.Bounds.Top, end, item.Bounds.Bottom);
txtDespquan.Size = new System.Drawing.Size(end - position, item.Bounds.Bottom - item.Bounds.Top);
txtDespquan.Location = new System.Drawing.Point(position, item.Bounds.Y);
txtDespquan.Width = this.columnHeader2.Width;
txtDespquan.Show();
txtDespquan.Text = subItemText;
txtDespquan.SelectAll();
txtDespquan.Focus();
}
else if (column == "3")
{
Rectangle r = new Rectangle(position, item.Bounds.Top, end, item.Bounds.Bottom);
txtNextdel.Size = new System.Drawing.Size(end - position, item.Bounds.Bottom - item.Bounds.Top);
txtNextdel.Location = new System.Drawing.Point(position, item.Bounds.Y);
txtNextdel.Width = this.columnHeader3.Width;
txtNextdel.Show();
txtNextdel.Text = subItemText;
txtNextdel.SelectAll();
txtNextdel.Focus();
}
}
public void ListViewMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
item = this.GetItemAt(e.X, e.Y);
X = e.X;
Y = e.Y;
}
private void search_and_bind_data(string searchtext)
{
if (search_lock == false)
{
last_search_criteria = cmbProducts.Text;
string currenttext = cmbProducts.Text;
int text_length = currenttext.Length;
cmbProducts.DataSource = null;
cmbProducts.DisplayMember = null;
cmbProducts.Items.Clear();
// label4.Text = @"select * from " + trust_id + "stock where spare = '' and proddesc like '%" + searchtext + "%' or sapcode like '%" + searchtext + "%' order by proddesc";
productlists = mailc.create_datatable("select * from " + trust_id + "stock where spare = '' and proddesc like '%" + searchtext + "%' or sapcode like '%" + searchtext + "%' order by proddesc");
productlists.Rows.InsertAt(productlists.NewRow(), 0);
cmbProducts.DataSource = productlists;
cmbProducts.DisplayMember = "proddesc";
cmbProducts.Text = currenttext;
cmbProducts.SelectedIndex = 0;
cmbProducts.SelectionStart = text_length;
}
}
public void initialise_products_list()
{
last_search_criteria = cmbProducts.Text;
cmbProducts.DataSource = null;
cmbProducts.DisplayMember = null;
productlists = mailc.create_datatable("select * from " + trust_id + "stock where spare = '' order by proddesc");
productlists.Rows.InsertAt(productlists.NewRow(), 0);
cmbProducts.DataSource = productlists;
cmbProducts.DisplayMember = "proddesc";
cmbProducts.ValueMember = "prodcode";
cmbProducts.Text = last_search_criteria;
search_lock = false;
}
}
}
Last edited by stevegray; October 18th, 2010 at 03:08 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|