?
Solved

Listview - synchronise selected and checked items - how?

Posted on 2007-10-01
23
Medium Priority
?
282 Views
Last Modified: 2008-01-09
Hi,

I thought this would be very easy, so it either isn't or I am so overworked I cannot see it.

What I would like to do is:
I have a listview, multiselect = true, checkboxes = true, fullrowselect = true.

I also have a button on the form (this is Winforms by the way).  When user selects/deselects an item or items, I would like to put a check (checked = true) on all the items the user selected and uncheck all unselected then enable or disable the button (btnOk) on the form depending whether there are items in the selection/checked collection or not.  Similarly user can use the checkboxes, in that case I would select (selected = true) all items the user checked and unselect all unchecked then do the same as above for the button.

That make sense?  Shouldn't be hard but the selectedindices and checkedindices seems to be incorrect (probably cos I am not doing something right).

Many thanks

H

0
Comment
Question by:gbzhhu
  • 13
  • 10
23 Comments
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 19992857
so let me get this in my head just to confirm you have a listview and to the left right or bottom or top you have some check boxes. So giving the user two options on how to select their choice.

Based on their choice and using conditional logic the code will determine whether or not the button is enabled or not.

Is this correct?

Andrew
0
 
LVL 12

Author Comment

by:gbzhhu
ID: 19997430
REA_ANDREW,

No, the checkboxes are in the listview :-) That is the Listview's CheckBoxes property is set to true.  I want the user's to be able to select items in the listview either by clicking on the list item itself or it's checkbox.  Clicking on a checkbox should select the item too (highlight) and similarly clicking on the item should set checbox.

>>Based on their choice and using conditional logic the code will determine whether or not the button is enabled or not

The above part of your assumption ios correct

Thanks
Hassan
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 19998146
OK I have wired into two events here to achieve what you are looking for. please have a look and see how I achieved it.

Thanks

Andrew

        private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            e.Item.Selected = e.Item.Checked;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in listView1.SelectedItems)
            {
                lvi.Checked = lvi.Selected;
            }
        }
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 12

Author Comment

by:gbzhhu
ID: 19998257
Andrew,

There is no ItemChecked event (I cannot see it myself) only ItemCheck which has ItemCheckEventArgs (not ItemCheckedEventArgs ) and so compile error.  There is no e.Item property.  

Can you actually compile your code?
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 19998284
Yes I can compile this and use it.

I am using a ListView. re you sure you are using a ListView?

here is my program. Simple but works.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ExpertsExchange
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            e.Item.Selected = e.Item.Checked;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in listView1.SelectedItems)
            {
                lvi.Checked = lvi.Selected;
            }
        }

    }
}
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 19998347
here you go I have placed the solution file to my example online for you to download and see :-)

www.e-business-systems.com/HelpFiles/ListViewExample.zip

Andrew
0
 
LVL 12

Author Comment

by:gbzhhu
ID: 19998352
Hmmmm!  I can see that you are using .NET 2.0  (System.Collections.Generic).  I am using .NET 1.1.  Although generics have nothing to do with this case I cannot find the ItemChecked event only ItemCheck.  I guess it is a difference between the Framework versions.  Do you have .NET 1.1 to test your code with?

And yes I am using listview.  This is the declaration I have this.lvGroups = new System.Windows.Forms.ListView();
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 19998388
Ah right I did not realise you were on 1.1 .  My solution may still not aid you then as I am in V2.0 -V3.X

Andrew
0
 
LVL 12

Author Comment

by:gbzhhu
ID: 19998413
What is V 3 like I am about to start playing round with WPF and XAML, is it difficult
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 19998449
I would prefer to use exciting and rewarding in the way that it is new and if you are programming with C# now, take it as a next avenue where there are many more things to learn and progress your knowledge and efficiency.

Download the Beta of Visual Studio 2008, maybe buy a book or two, make a coffee and dive right in lol

Andrew

0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 19998455
You will like the XAML Browser Applications.
0
 
LVL 12

Author Comment

by:gbzhhu
ID: 19998685
Cool!  I am mainly a winforms and webservices developer.  Have you tried Linq yet?

As for my question, let me leave it open for a couple of days in case someone knows how to achieve what I am trying to do or confirm it is not doable in 1.1

Thanks
Hassan
0
 
LVL 20

Accepted Solution

by:
REA_ANDREW earned 2000 total points
ID: 19999219
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 19999226
As for LinQ check out this guys blog. It is really good reading. Notice his second most recent blog titled

A simple Business Object wrapper for LINQ to SQL

Here is his Blog.

http://west-wind.com/weblog/

Andrew
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 19999269
Oh also I have noticed a trend on this experts echange.  IF there are lots of comments in a question which say Person A has not looked at or particpated in it takes someone will real enthusiasm to pick up the trail of the question to date, I class myself as someone who would pick up the trail to in the biew of hopefully helping that person.  Then you have what I call vultures, where they can see that the strongest participant is going to post the answer straight away and simply beats them to it, thus maybe getting some or all of the points.

In your case here where you have said i will leave this open and see if anyone else will give me an answer. my advice there would be to post an EE pointer question to raise attention to your question showing users that you still have not got a solution.

On the other side you have the people asking the questions lol who receive your answer but leave it up to the moderators to decide its final dispoition. lol speaking of which i better go check my own open questions lol ;-)

Cheers

Andrew

p.s. seeing your profile, you are probably more than aware of everyhting I have just wrote lol. But anyway thought i might have a rant lol.

0
 
LVL 12

Author Comment

by:gbzhhu
ID: 19999466
Cheers for the links mate.  I will check them out later (Another problem cropped up now and I need to deal with that first)

LOL, your rant is brilliant!!  Yeah I have been a member for a long while and used to contribute as an expert when I had time.  I am aware of what you said and the nastiest is "leave it up to the moderators to decide its final dispoition."  I once gave a guy a complete solution that compiles fine and does what he wanted in the most effective way, guess what he does? yeah, leave it to the mods to clean it up and forced accept it to give me the points!!

For this one, I am not going to bother a pointer question, thanks though for suggesting it.  It is not critical and if your link doesn't give me a solution I will simply get rid of the checkboxes and just use normal selection.  You see my users are farmers hahaha!! yeah you heard that right, so holding down shift or control keys may not come as natural as it does to us but I can always put a message above the listview telling them what to do.

I noticed that you are more of a web developer.  I just hate trying to debug web apps, it never works.  You are either

- not a member of DebuggerUsers
- web.config doesn't enable debugging
- project properties doesn't enable debugging
- all that horrible IIS authenrication setting - something is wrong there
- you are using .net 1.1 vs 2003 on Vista (go hang yourself before getting debugging to work)
- you have .net 1.1 and 2.0 installed and IIS doesn't know what to do
- many more scenarios that just won't allow web project debugging - I have never seen or heard anyone who got remote debugging to work :-(((

Now, how do you survive developing web apps my friend?  Winforms just debug beautifully all the time

Now enough rant!!

H


0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 19999548
lol
0
 
LVL 12

Author Comment

by:gbzhhu
ID: 19999801
Andrew,

As you see you got a BIG A for helping me out.  I haven't tried the code in the link but it looks hopeful.

Now you don't have to but I would appreciate if you could give me your ideas ont his new [strange] issue.

I have a web service which works fine and is deployed to the server.  The web service accesses database  on another server.  I can run the client code to connect to the web service on the server and everything is peachy.  I change the client to point to localhost (I have setup virtual dir etc) because I want to debug it (you know about web dev debugging lol!) surprisingly I can debug but on calling the stored procedure I get "SQL Server is not available or access is denied" I can connect to that SQL server via VS no problem.  The client code is the same, the login info to the DB server is the same!!  I tried SQL profiler but the call actually doesn't make it that far.  Any ideas?

Cheers
H
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 20000312
Does the Sql Server which you are attempting to connect to allow remote connections?  For security a lot of the time I will put the webservices and the database server on the same server.  I will then deny rmote connections so that the web service must be used and remote access to the database server is prohibited, to avoid hacking and what not.

Andrew
0
 
LVL 12

Author Comment

by:gbzhhu
ID: 20000418
I think remote connections is enabled since I can connect to the SQL server from any machine on our network.  I keep the web service on the web server because it is setup as web server - all IIS, firewall settings etc are in place.  The DB server is optimized for databases and contains in excess of 20 dbs so I don't think I can change that configuration.
0
 
LVL 12

Author Comment

by:gbzhhu
ID: 20000424
I may get some ideas when I am fresh in the morning.  If not probably see if if EE can help.

Cheers
H
0
 
LVL 12

Author Comment

by:gbzhhu
ID: 20015300
Andrew,

The issue is still bugging me and so I put it into a question.  I am gonna abandon this one.  Thanks for helping and hope to see you around EE

Hassan
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 20015651
No Probs. Hope you find the solution.

Cheers

Andrew
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question