Link to home
Start Free TrialLog in
Avatar of gbzhhu
gbzhhuFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Listview - synchronise selected and checked items - how?

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

Avatar of REA_ANDREW
REA_ANDREW
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of gbzhhu

ASKER

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
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;
            }
        }
Avatar of gbzhhu

ASKER

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?
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;
            }
        }

    }
}
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
Avatar of gbzhhu

ASKER

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();
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
Avatar of gbzhhu

ASKER

What is V 3 like I am about to start playing round with WPF and XAML, is it difficult
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

You will like the XAML Browser Applications.
Avatar of gbzhhu

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of REA_ANDREW
REA_ANDREW
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.

Avatar of gbzhhu

ASKER

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


Avatar of gbzhhu

ASKER

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
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
Avatar of gbzhhu

ASKER

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.
Avatar of gbzhhu

ASKER

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

Cheers
H
Avatar of gbzhhu

ASKER

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
No Probs. Hope you find the solution.

Cheers

Andrew