By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,723 Members | 1,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,723 IT Pros & Developers. It's quick & easy.

Listview, reselect old selection C#

P: n/a
I've got a listview on a form in report style. It's populated with 40
item. I want to prevent the used clicking on some items, and if the
user clicks on one of these items I want the selection to revert back
to the old selection, hideselection property if false, listview is
single select.

In the OnSelectedChange() handler I test to see if the newly selected
item is a selectable one, if it's not, I try to change the selection
like so.

private void OnSelectedItemChanged(object sender, System.EventArgs e)
{
if( listView1.SelectedItems.Count != 0 )
{
int idx = listView1.SelectedItems[0].Index;
bool ret = execScript.isExecutable(idx);
if(ret == false)
{
listView1.Items[execScript.getCurrentExecNum()].Selected = true;
listView1.Select();
listView1.EnsureVisible(execScript.getCurrentExecN um());
listView1.Focus();
}
}

This doesn't work.
Neither does posting a user message to this function by overriding the
WndProc and calling Win32

[DllImport("user32.Dll")]
public static extern Int32 PostMessage(IntPtr Handle, Int32
ConnectionType, IntPtr wParam, IntPtr lParam);

ResetSelection()
{
listView1.Items[execScript.getCurrentExecNum()].Selected = true;
listView1.Select();
listView1.EnsureVisible(execScript.getCurrentExecN um());
listView1.Focus();
}

Neither does using a delegate like this

listView1.BeginInvoke(new
OnSelectedItemChangedDelegate(ResetSelection),null );

Neither does adding a filter on to the listview control and trapping
the LVN_ITEMCHANGING or LVN_ITEMCHANGED message in the WM_NOTIFY.

Interesting to note though if I add a button to the form and call my
ResetSelection() function it works fine.

Three days I've spent on this. Can anyone help?

Thanks

Youngie...

Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Try this:

ListViewItem selectedItem = null;

private void listView1_MouseUp(object sender, MouseEventArgs e)

{

ListViewItem item = listView1.GetItemAt(e.X,e.Y);

if (item != null && item.Checked)

{

item.Selected = false;

if (selectedItem != null)

selectedItem.Selected = true;

}

}

private void listView1_MouseDown(object sender, MouseEventArgs e)

{

if (listView1.SelectedItems.Count == 1)

selectedItem = listView1.SelectedItems[0];

else

selectedItem = null;

}

<yo*****@youngie.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
I've got a listview on a form in report style. It's populated with 40
item. I want to prevent the used clicking on some items, and if the
user clicks on one of these items I want the selection to revert back
to the old selection, hideselection property if false, listview is
single select.

In the OnSelectedChange() handler I test to see if the newly selected
item is a selectable one, if it's not, I try to change the selection
like so.

private void OnSelectedItemChanged(object sender, System.EventArgs e)
{
if( listView1.SelectedItems.Count != 0 )
{
int idx = listView1.SelectedItems[0].Index;
bool ret = execScript.isExecutable(idx);
if(ret == false)
{
listView1.Items[execScript.getCurrentExecNum()].Selected = true;
listView1.Select();
listView1.EnsureVisible(execScript.getCurrentExecN um());
listView1.Focus();
}
}

This doesn't work.
Neither does posting a user message to this function by overriding the
WndProc and calling Win32

[DllImport("user32.Dll")]
public static extern Int32 PostMessage(IntPtr Handle, Int32
ConnectionType, IntPtr wParam, IntPtr lParam);

ResetSelection()
{
listView1.Items[execScript.getCurrentExecNum()].Selected = true;
listView1.Select();
listView1.EnsureVisible(execScript.getCurrentExecN um());
listView1.Focus();
}

Neither does using a delegate like this

listView1.BeginInvoke(new
OnSelectedItemChangedDelegate(ResetSelection),null );

Neither does adding a filter on to the listview control and trapping
the LVN_ITEMCHANGING or LVN_ITEMCHANGED message in the WM_NOTIFY.

Interesting to note though if I add a button to the form and call my
ResetSelection() function it works fine.

Three days I've spent on this. Can anyone help?

Thanks

Youngie...

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.