473,321 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Listview, reselect old selection C#

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
1 3048
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: InvisibleDuncan | last post by:
I have a ListView that populates some fields whenever the user selects an item. However, if they change the data in the fields and then select a new item without saving, I want to display a message...
6
by: Vanessa | last post by:
With this program I can do one selection, but upon the second I get an error where ///////////////// is indicated. Please help. using System; using System.Drawing; using System.Collections;...
2
by: Aron Henning | last post by:
I have a ListView that contains the subfoldes of a local drive. Whan i click on a subfolder the listbox should display the subfolders of this folder instead. the funktion is as follows: private...
7
by: GTi | last post by:
I have a listview that with selected items. But I want to "reselect" items after a refresh. Each items have a uniqe value in the Tag object. After a refresh some new items may be added or removed...
0
by: Brian Henry | last post by:
Ok whats going on here... got a couple problems... this is relevant now to this group since .NET 2.0 is finally RTM 1) draws really slow when making it a large form (resizing speed is jerky) to...
6
by: Brandon McCombs | last post by:
Hello, I have a form that contains a listview on the left side and a column of buttons on the right side. Only some of the buttons do I want enabled all the time. The other buttons should be...
5
by: Jure Bogataj | last post by:
Hi all! I have a problem (performance issue) with listview. I have implemented an ItemSelectionChange on my listview and put some code in it (I build some toolbar based on selection and update...
12
by: Tom Bean | last post by:
I am trying to display a ContextMenuStrip when a user right-clicks on an item in a ListView and have encountered a something that seems strange to me. When the ListView is initially populated,...
4
by: Brandon | last post by:
HI all, I am working on a WPF listview that get items through the XMLDataProvider and making a listview with two actual columns and in both CellTemplete XAML in a grid bind more than one snippet...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.