473,854 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 OnSelectedChang e() 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 OnSelectedItemC hanged(object sender, System.EventArg s e)
{
if( listView1.Selec tedItems.Count != 0 )
{
int idx = listView1.Selec tedItems[0].Index;
bool ret = execScript.isEx ecutable(idx);
if(ret == false)
{
listView1.Items[execScript.getC urrentExecNum()].Selected = true;
listView1.Selec t();
listView1.Ensur eVisible(execSc ript.getCurrent ExecNum());
listView1.Focus ();
}
}

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

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

ResetSelection( )
{
listView1.Items[execScript.getC urrentExecNum()].Selected = true;
listView1.Selec t();
listView1.Ensur eVisible(execSc ript.getCurrent ExecNum());
listView1.Focus ();
}

Neither does using a delegate like this

listView1.Begin Invoke(new
OnSelectedItemC hangedDelegate( ResetSelection) ,null);

Neither does adding a filter on to the listview control and trapping
the LVN_ITEMCHANGIN G 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 3079
Try this:

ListViewItem selectedItem = null;

private void listView1_Mouse Up(object sender, MouseEventArgs e)

{

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

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

{

item.Selected = false;

if (selectedItem != null)

selectedItem.Se lected = true;

}

}

private void listView1_Mouse Down(object sender, MouseEventArgs e)

{

if (listView1.Sele ctedItems.Count == 1)

selectedItem = listView1.Selec tedItems[0];

else

selectedItem = null;

}

<yo*****@youngi e.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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 OnSelectedChang e() 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 OnSelectedItemC hanged(object sender, System.EventArg s e)
{
if( listView1.Selec tedItems.Count != 0 )
{
int idx = listView1.Selec tedItems[0].Index;
bool ret = execScript.isEx ecutable(idx);
if(ret == false)
{
listView1.Items[execScript.getC urrentExecNum()].Selected = true;
listView1.Selec t();
listView1.Ensur eVisible(execSc ript.getCurrent ExecNum());
listView1.Focus ();
}
}

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

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

ResetSelection( )
{
listView1.Items[execScript.getC urrentExecNum()].Selected = true;
listView1.Selec t();
listView1.Ensur eVisible(execSc ript.getCurrent ExecNum());
listView1.Focus ();
}

Neither does using a delegate like this

listView1.Begin Invoke(new
OnSelectedItemC hangedDelegate( ResetSelection) ,null);

Neither does adding a filter on to the listview control and trapping
the LVN_ITEMCHANGIN G 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
602
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 box that warns that their changes will be lost, and asks them to confirm whether to continue. Sounds simple Unfortunately, the ListView's SelectedIndexChanged event cannot be cancelled, and it's actually called for each change in the selection. So,...
6
2547
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; using System.ComponentModel; using System.Windows.Forms; using System.Data;
2
3386
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 void FolderList_SelectedIndexChanged(object sender, System.EventArgs e) { try
7
24127
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 so using item index is not possible. To get the list of selected items I use: ListView.SelectedListViewItemCollection selItems = this.listView1.SelectedItems;
0
1648
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 a res from 100x100 to 1280x1024 once it gets above 500x500 it starts jerking you can see the screen refreshing as it paints 2) sometimes the background selection when you select multiple items then move the mouse around on the screen dissapear...
6
5563
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 enabled only if something is selected in the listview. That part specifically works but not very well. It seems that I can only get the buttons to disable if I click off the text of the items in the listview but still within about 10-20 pixels of...
5
12678
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 info in statusbar). When selecting one item (clicking on listview) it works fast, without noticing. However if selecting multiple items with SHIFT (approx. 500 items) or selecting with mouse, for each item selected through all listview an event...
12
4045
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, no items are selected. When the first item is selected by clicking either the left or right button, the SelectedIndexChanged event fires but not the MouseUp event. After the first item is selected and another item clicked on, both the...
4
7470
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 of the XML data to the cell. Now this works fine no problem... When I try to setup a IsSelected trigger I lose the default listview selection UI and only the new look of the ListViewItem after the trigger takes effect shows what Item is...
0
9750
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11024
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10672
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9510
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7077
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4150
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3182
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.