Checking a listview item while mouse click


M

Manikandan

Hi,
I have a listview items with checkbox .
I want the checbox to be checked or unchecked when the user clicks the
listview items or check box.
When i click the checkbox for list view item, it is not checked.
But if i select the listview items by mouse click, the checkbox is
checked.
I want the user to select the items by checking the checkbox in
lisview
I written the code in listview mouse down a

private void listview1_MouseDown(object sender, MouseEventArgs e)
{
ListViewItem oLVitem;
if(e.Button == MouseButtons.Left)
{
oLVitem = this.listview1.GetItemAt(e.X, e.Y);
int t=oLVitem.Index;

if (oLVitem != null)
{

if (oLVitem.Checked == true)
{
//oLVitem.Checked = false;
listview1.Items[oLVitem.Index].Checked=false;
}
else
{
//oLVitem.Checked = true;
listview1.Items[oLVitem.Index].Checked=true;

}
}
}

}
I tried the above for checking the checkbox in listview when user
click the checbox.
It is not working, is there any problem in the code

Thanks,
Mani
 
Ad

Advertisements

G

Guest

Hello,
Apart from you didn't tell whats exactely happens (HAve you debuged it?),
there are some mistakes.
int t=oLVitem.Index;

if (oLVitem != null)
{
This doesn't make sence at all, because you accessing a member of "oLVitem"
before you do the "!= null" check. Could end up in an exception.

Manikandan said:
Hi,
I have a listview items with checkbox .
I want the checbox to be checked or unchecked when the user clicks the
listview items or check box.
When i click the checkbox for list view item, it is not checked.
But if i select the listview items by mouse click, the checkbox is
checked.
I want the user to select the items by checking the checkbox in
lisview
I written the code in listview mouse down a

private void listview1_MouseDown(object sender, MouseEventArgs e)
{
ListViewItem oLVitem;
if(e.Button == MouseButtons.Left)
{
oLVitem = this.listview1.GetItemAt(e.X, e.Y);
int t=oLVitem.Index;

if (oLVitem != null)
{

if (oLVitem.Checked == true)
{
//oLVitem.Checked = false;
listview1.Items[oLVitem.Index].Checked=false;
}
else
{
//oLVitem.Checked = true;
listview1.Items[oLVitem.Index].Checked=true;

}
}
}

}
I tried the above for checking the checkbox in listview when user
click the checbox.
It is not working, is there any problem in the code

Thanks,
Mani
 
P

Pete Kane

Manikandan said:
Hi,
I have a listview items with checkbox .
I want the checbox to be checked or unchecked when the user clicks the
listview items or check box.
When i click the checkbox for list view item, it is not checked.
But if i select the listview items by mouse click, the checkbox is
checked.
I want the user to select the items by checking the checkbox in
lisview
I written the code in listview mouse down a

private void listview1_MouseDown(object sender, MouseEventArgs e)
{
ListViewItem oLVitem;
if(e.Button == MouseButtons.Left)
{
oLVitem = this.listview1.GetItemAt(e.X, e.Y);
int t=oLVitem.Index;

if (oLVitem != null)
{

if (oLVitem.Checked == true)
{
//oLVitem.Checked = false;
listview1.Items[oLVitem.Index].Checked=false;
}
else
{
//oLVitem.Checked = true;
listview1.Items[oLVitem.Index].Checked=true;

}
}
}

}
I tried the above for checking the checkbox in listview when user
click the checbox.
It is not working, is there any problem in the code

Thanks,
Mani
try this in the SelectedIndexChanged event

if(this.listview1.SelectedItems.Count > 0)
this.listview1.SelectedItems[0].Checked = true;
 
T

Tom Spink

Manikandan said:
Hi,
I have a listview items with checkbox .
I want the checbox to be checked or unchecked when the user clicks the
listview items or check box.
When i click the checkbox for list view item, it is not checked.
But if i select the listview items by mouse click, the checkbox is
checked.
I want the user to select the items by checking the checkbox in
lisview
I written the code in listview mouse down a

private void listview1_MouseDown(object sender, MouseEventArgs e)
{
ListViewItem oLVitem;
if(e.Button == MouseButtons.Left)
{
oLVitem = this.listview1.GetItemAt(e.X, e.Y);
int t=oLVitem.Index;

if (oLVitem != null)
{

if (oLVitem.Checked == true)
{
//oLVitem.Checked = false;
listview1.Items[oLVitem.Index].Checked=false;
}
else
{
//oLVitem.Checked = true;
listview1.Items[oLVitem.Index].Checked=true;

}
}
}

}
I tried the above for checking the checkbox in listview when user
click the checbox.
It is not working, is there any problem in the code

Thanks,
Mani

Hi Mani,

I can see one error in your code, and multiple ways to streamline it.

Firstly, you're checking for oLVItem to be null *after* you've already tried
to dereference it by obtaining the index! "This Bad Idea."

Try the following code:

///
private void listview1_MouseDown (object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ListViewItem lvi = listview1.GetItemAt(e.X, e.Y);
if (lvi == null)
return;
lvi.Checked = !lvi.Checked;
}
}
///

It works for me!
 
K

Kevin Spencer

First, you haven't posted any information about what other event handlers
you're using, so it's hard to know why you're getting the behavior you are
experiencing in the first place. Second, the MouseDown event happens before
the MouseUp event, which occurs before the Click event, so you are probably
experiencing the effect of more than one event happening.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
M

Manikandan

Manikandan said:
Hi,
I have a listview items with checkbox .
I want the checbox to be checked or unchecked when the user clicks the
listview items or check box.
When i click the checkbox for list view item, it is not checked.
But if i select the listview items by mouse click, the checkbox is
checked.
I want the user to select the items by checking the checkbox in
lisview
I written the code in listview mouse down a
private void listview1_MouseDown(object sender, MouseEventArgs e)
{
ListViewItem oLVitem;
if(e.Button == MouseButtons.Left)
{
oLVitem = this.listview1.GetItemAt(e.X, e.Y);
int t=oLVitem.Index;
if (oLVitem != null)
{
if (oLVitem.Checked == true)
{
//oLVitem.Checked = false;
listview1.Items[oLVitem.Index].Checked=false;
}
else
{
//oLVitem.Checked = true;
listview1.Items[oLVitem.Index].Checked=true;

}
I tried the above for checking the checkbox in listview when user
click the checbox.
It is not working, is there any problem in the code
Thanks,
Mani

try this in the SelectedIndexChanged event

if(this.listview1.SelectedItems.Count > 0)
this.listview1.SelectedItems[0].Checked = true;


Hi,
I tried the code in selectedindexchanged vents, when we select the
listview items the checkbox is checked.
When i click the checkbox, it is not checked.
But when i double click the checkbox, it is checked .
Anything wrong ?

Mani
 
Ad

Advertisements

M

Manikandan

Manikandan said:
Hi,
I have a listview items with checkbox .
I want the checbox to be checked or unchecked when the user clicks the
listview items or check box.
When i click the checkbox for list view item, it is not checked.
But if i select the listview items by mouse click, the checkbox is
checked.
I want the user to select the items by checking the checkbox in
lisview
I written the code in listview mouse down a
private void listview1_MouseDown(object sender, MouseEventArgs e)
{
ListViewItem oLVitem;
if(e.Button == MouseButtons.Left)
{
oLVitem = this.listview1.GetItemAt(e.X, e.Y);
int t=oLVitem.Index;
if (oLVitem != null)
{
if (oLVitem.Checked == true)
{
//oLVitem.Checked = false;
listview1.Items[oLVitem.Index].Checked=false;
}
else
{
//oLVitem.Checked = true;
listview1.Items[oLVitem.Index].Checked=true;
}
}
}
}
I tried the above for checking the checkbox in listview when user
click the checbox.
It is not working, is there any problem in the code
Thanks,
Mani
try this in the SelectedIndexChanged event
if(this.listview1.SelectedItems.Count > 0)
this.listview1.SelectedItems[0].Checked = true;

Hi,
I tried the code in selectedindexchanged vents, when we select the
listview items the checkbox is checked.
When i click the checkbox, it is not checked.
But when i double click the checkbox, it is checked .
Anything wrong ?

Mani

Hi,
I have written code for mouse down event only for the listview.
There is no other event handler code for listview


Mani
 
Ad

Advertisements

M

Manikandan

Manikandan said:
Hi,
I have a listview items with checkbox .
I want the checbox to be checked or unchecked when the user clicks the
listview items or check box.
When i click the checkbox for list view item, it is not checked.
But if i select the listview items by mouse click, the checkbox is
checked.
I want the user to select the items by checking the checkbox in
lisview
I written the code in listview mouse down a
private void listview1_MouseDown(object sender, MouseEventArgs e)
{
ListViewItem oLVitem;
if(e.Button == MouseButtons.Left)
{
oLVitem = this.listview1.GetItemAt(e.X, e.Y);
int t=oLVitem.Index;
if (oLVitem != null)
{
if (oLVitem.Checked == true)
{
//oLVitem.Checked = false;
listview1.Items[oLVitem.Index].Checked=false;
}
else
{
//oLVitem.Checked = true;
listview1.Items[oLVitem.Index].Checked=true;

}
I tried the above for checking the checkbox in listview when user
click the checbox.
It is not working, is there any problem in the code
Thanks,
Mani

Hi Mani,

I can see one error in your code, and multiple ways to streamline it.

Firstly, you're checking for oLVItem to be null *after* you've already tried
to dereference it by obtaining the index! "This Bad Idea."

Try the following code:

///
private void listview1_MouseDown (object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ListViewItem lvi = listview1.GetItemAt(e.X, e.Y);
if (lvi == null)
return;
lvi.Checked = !lvi.Checked;
}}

///

It works for me!

Hi,
I tried the code as below
private void lvClassTypeList_MouseDown(object sender, MouseEventArgs
e)
{
ListViewItem oLVitem;
if(e.Button == MouseButtons.Left)
{
oLVitem = this.lvClassTypeList.GetItemAt(e.X, e.Y);

if (oLVitem != null)
{

oLVitem.Checked=!oLVitem.Checked;
}
}
}
The above code work perfectly when i click the list view item.
but when i click the check box for that list view item, nothing
happens, it is not checked
when I double click the checkbox or that list view item, it is checked

Mani
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top