|
-
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.
-
October 15th, 2010, 11:18 AM
#2
Re: Double click on listview subitems not working properly
Please edit your post, to use CODE TAGS
Pretty hard to read, without indentation. If adding tags doesn't work, paste it again, and add the tags
-
October 18th, 2010, 03:12 AM
#3
Re: Double click on listview subitems not working properly
Bumped, still having problems with this.
Sorted the code out too. Thanks.
-
October 18th, 2010, 09:28 AM
#4
Re: Double click on listview subitems not working properly
You wrote that you sorted the code and that you're still having problems with this. I don't know it you have this resolved or not. In case you don't; in the double click code you have in a for loop:
Code:
if (start > position && start < end)
the variable 'start' never changes. So the code gets executed the first time around; but that's it as far as I can tell.
-
October 19th, 2010, 10:47 AM
#5
[Resolved] Double click on listview subitems not working properly
The Start variable was changing, in the end it was
Code:
for (int i = 0; i < this.Columns.Count; i++)
{
if (start > position && start < end)
{
selectedSubItem = i;
break;
}
position = end;
end += this.Columns[i].Width;
}
That had the problem with it, the end += this.Columns[i].Width; part needed changing to [i+1] as it wasnt calculating which column header the mouse was under correctly. The example I took it off - all the headers were set at 100 so it didnt matter. Changed to
Code:
end += this.Columns[i+1].Width;
and everything started working as I wanted.
Thankyou for your help mate, you got me looking in the right direction !!!
-
October 19th, 2010, 11:08 AM
#6
Re: Double click on listview subitems not working properly
The only place where I see the 'start' variable change is when you initialized to the value of X. So if it is changing I guess X is global. As long as you're on the right track then that's good news.
-
October 19th, 2010, 06:36 PM
#7
Re: Double click on listview subitems not working properly
 Originally Posted by viperbyte
The only place where I see the 'start' variable change is when you initialized to the value of X. So if it is changing I guess X is global. As long as you're on the right track then that's good news.
Except start is an int, which is a value type, which means that changing X would not change start post-assigment, "global" or not (there really are no "global" variables in the classical sense in C#).
Last edited by BigEd781; October 19th, 2010 at 07:19 PM.
-
October 19th, 2010, 07:14 PM
#8
Re: Double click on listview subitems not working properly
Cool; good to know. I read code and try to find the problem. It's like a crossword puzzle type activity for me. I'm definitly not a goto guy on C#. Thanks for the clarification
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
|