Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Change (or disable) selection color of listview [SOLVED partially]

Change (or disable) selection color of listview [SOLVED partially]

Scheduled Pinned Locked Moved C#
helpquestion
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S sodevrom

    Hello guys, First, I don't want to use OwnerDraw, that is a condition I have :( . What I want is set a custom color to the selection of a listview. I want the color to be visible even if the listview is not focused. I know about the HideSelection member, but that's not what I need. I tried to overrwrite the onselectionindexchanged, and make the item.selected=false, and after that, set a custom back/fore color. This does not work because I have multiple items, and it deselects all items but one. I need one of 2 things: a)Keep selection visible (with blue color, not gray, this is the problem with HideSelection) always b)Remember selected objects, but do not show the selection at all (because if the selection is visible, than the custom back/fore color are not visible), so I can "mimic" the selection by setting a custom back/fore color to the items. Now I have been spending over 4 hours now on this stupid issue, and no luck. Anyone has a suggestion? It would help a lot. *******************solution Instead of setting the color in the on selected changed event, we can do it on mouse up. It's not perfect, but it's the best I got. The best way to do this is using a custom draw listview. Here is the code:

    List selections=new List();

    protected override void OnMouseUp(MouseEventArgs e)
    {
    selections = this.SelectedIndices.Cast().ToList(); //store selection
    this.SelectedIndices.Clear(); //clear selection

            for (int i = 0; i < this.Items.Count; i++)
            {
                if (this.selections.Contains(i))
                    this.Items\[i\].BackColor = Color.Blue; //if selected, make it blue
                else
                    this.Items\[i\].BackColor = Color.White; //if not selected, make it white
            }
            base.OnMouseUp(e);
        }
    

    modified on Monday, August 2, 2010 9:01 PM

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    sodevrom wrote:

    a)Keep selection visible (with blue color, not gray, this is the problem with HideSelection) always

    That's by design; whatever is focused is drawn in the "selected" color. That way I can (as a user) visually differentiate between the ListView that's focused (and will react to keyboard-input) and which won't. If both were using blue, it would imply that both have the input-focus. That's not possible in Windows, since there can only be one active control with input-focus. There's no "neat" way to do this. Why no owner-drawing btw?

    sodevrom wrote:

    b)Remember selected objects, but do not show the selection at all (because if the selection is visible, than the custom back/fore color are not visible), so I can "mimic" the selection by setting a custom back/fore color to the items.

    Remembering selected objects can be done by adding them to a generic list, whenever you select one. Sounds like you want to replace the default multiselect by your own version?

    I are Troll :suss:

    S 1 Reply Last reply
    0
    • L Lost User

      sodevrom wrote:

      a)Keep selection visible (with blue color, not gray, this is the problem with HideSelection) always

      That's by design; whatever is focused is drawn in the "selected" color. That way I can (as a user) visually differentiate between the ListView that's focused (and will react to keyboard-input) and which won't. If both were using blue, it would imply that both have the input-focus. That's not possible in Windows, since there can only be one active control with input-focus. There's no "neat" way to do this. Why no owner-drawing btw?

      sodevrom wrote:

      b)Remember selected objects, but do not show the selection at all (because if the selection is visible, than the custom back/fore color are not visible), so I can "mimic" the selection by setting a custom back/fore color to the items.

      Remembering selected objects can be done by adding them to a generic list, whenever you select one. Sounds like you want to replace the default multiselect by your own version?

      I are Troll :suss:

      S Offline
      S Offline
      sodevrom
      wrote on last edited by
      #3

      Hello, I know this is possible because I seen it in action somewhere. I do not want to use owner draw, because I would have to implement tons of things, and I still want the list to look exactly like in windows (with the theme from windows). Still waiting for some suggestions. Thanks!

      L 1 Reply Last reply
      0
      • S sodevrom

        Hello guys, First, I don't want to use OwnerDraw, that is a condition I have :( . What I want is set a custom color to the selection of a listview. I want the color to be visible even if the listview is not focused. I know about the HideSelection member, but that's not what I need. I tried to overrwrite the onselectionindexchanged, and make the item.selected=false, and after that, set a custom back/fore color. This does not work because I have multiple items, and it deselects all items but one. I need one of 2 things: a)Keep selection visible (with blue color, not gray, this is the problem with HideSelection) always b)Remember selected objects, but do not show the selection at all (because if the selection is visible, than the custom back/fore color are not visible), so I can "mimic" the selection by setting a custom back/fore color to the items. Now I have been spending over 4 hours now on this stupid issue, and no luck. Anyone has a suggestion? It would help a lot. *******************solution Instead of setting the color in the on selected changed event, we can do it on mouse up. It's not perfect, but it's the best I got. The best way to do this is using a custom draw listview. Here is the code:

        List selections=new List();

        protected override void OnMouseUp(MouseEventArgs e)
        {
        selections = this.SelectedIndices.Cast().ToList(); //store selection
        this.SelectedIndices.Clear(); //clear selection

                for (int i = 0; i < this.Items.Count; i++)
                {
                    if (this.selections.Contains(i))
                        this.Items\[i\].BackColor = Color.Blue; //if selected, make it blue
                    else
                        this.Items\[i\].BackColor = Color.White; //if not selected, make it white
                }
                base.OnMouseUp(e);
            }
        

        modified on Monday, August 2, 2010 9:01 PM

        K Offline
        K Offline
        Keith Barrow
        wrote on last edited by
        #4

        I'm not sure I understand your question fully, but I think you can set the Enabled Property to true and the Readonly property to true also. Apologies in advance if I have misunderstood your question.

        ragnaroknrol The Internet is For Porn[^]
        Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

        S 1 Reply Last reply
        0
        • S sodevrom

          Hello guys, First, I don't want to use OwnerDraw, that is a condition I have :( . What I want is set a custom color to the selection of a listview. I want the color to be visible even if the listview is not focused. I know about the HideSelection member, but that's not what I need. I tried to overrwrite the onselectionindexchanged, and make the item.selected=false, and after that, set a custom back/fore color. This does not work because I have multiple items, and it deselects all items but one. I need one of 2 things: a)Keep selection visible (with blue color, not gray, this is the problem with HideSelection) always b)Remember selected objects, but do not show the selection at all (because if the selection is visible, than the custom back/fore color are not visible), so I can "mimic" the selection by setting a custom back/fore color to the items. Now I have been spending over 4 hours now on this stupid issue, and no luck. Anyone has a suggestion? It would help a lot. *******************solution Instead of setting the color in the on selected changed event, we can do it on mouse up. It's not perfect, but it's the best I got. The best way to do this is using a custom draw listview. Here is the code:

          List selections=new List();

          protected override void OnMouseUp(MouseEventArgs e)
          {
          selections = this.SelectedIndices.Cast().ToList(); //store selection
          this.SelectedIndices.Clear(); //clear selection

                  for (int i = 0; i < this.Items.Count; i++)
                  {
                      if (this.selections.Contains(i))
                          this.Items\[i\].BackColor = Color.Blue; //if selected, make it blue
                      else
                          this.Items\[i\].BackColor = Color.White; //if not selected, make it white
                  }
                  base.OnMouseUp(e);
              }
          

          modified on Monday, August 2, 2010 9:01 PM

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #5

          Create your own list view and your own list view items. This is the easiest way to get the results you desire. In fact, it would have taken less than 4 hours, I bet.

          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

          S 1 Reply Last reply
          0
          • S sodevrom

            Hello, I know this is possible because I seen it in action somewhere. I do not want to use owner draw, because I would have to implement tons of things, and I still want the list to look exactly like in windows (with the theme from windows). Still waiting for some suggestions. Thanks!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            sodevrom wrote:

            I know this is possible because I seen it in action somewhere

            Yup, it's possible :)

            sodevrom wrote:

            I do not want to use owner draw, because I would have to implement tons of things

            You want non-standard behavior, and that has to be implemented somewhere. Owner-drawing might sound like overkill, especially if you've been looking at examples that draw a lot manually. The example on MSDN[^] looks rather short and might do the trick.

            sodevrom wrote:

            and I still want the list to look exactly like in windows

            Make sure you're using the SystemColors[^] when drawing.

            I are Troll :suss:

            1 Reply Last reply
            0
            • S sodevrom

              Hello guys, First, I don't want to use OwnerDraw, that is a condition I have :( . What I want is set a custom color to the selection of a listview. I want the color to be visible even if the listview is not focused. I know about the HideSelection member, but that's not what I need. I tried to overrwrite the onselectionindexchanged, and make the item.selected=false, and after that, set a custom back/fore color. This does not work because I have multiple items, and it deselects all items but one. I need one of 2 things: a)Keep selection visible (with blue color, not gray, this is the problem with HideSelection) always b)Remember selected objects, but do not show the selection at all (because if the selection is visible, than the custom back/fore color are not visible), so I can "mimic" the selection by setting a custom back/fore color to the items. Now I have been spending over 4 hours now on this stupid issue, and no luck. Anyone has a suggestion? It would help a lot. *******************solution Instead of setting the color in the on selected changed event, we can do it on mouse up. It's not perfect, but it's the best I got. The best way to do this is using a custom draw listview. Here is the code:

              List selections=new List();

              protected override void OnMouseUp(MouseEventArgs e)
              {
              selections = this.SelectedIndices.Cast().ToList(); //store selection
              this.SelectedIndices.Clear(); //clear selection

                      for (int i = 0; i < this.Items.Count; i++)
                      {
                          if (this.selections.Contains(i))
                              this.Items\[i\].BackColor = Color.Blue; //if selected, make it blue
                          else
                              this.Items\[i\].BackColor = Color.White; //if not selected, make it white
                      }
                      base.OnMouseUp(e);
                  }
              

              modified on Monday, August 2, 2010 9:01 PM

              W Offline
              W Offline
              William Winner
              wrote on last edited by
              #7

              This is a very basic solution, but here it is anyway:

              private List selections = new List();
              private bool dontChange = false;

              private void listView1_SelectedIndexChanged(object sender, EventArgs e)
              {
              if (!dontChange)
              {
              dontChange = !dontChange;
              int selectedIndex = listView2.SelectedIndices[0];

                  listView1.SelectedIndices.Clear();
              
                  //check if we're deselecting/selecting
                  if (selections.Contains(selectedIndex))
                  {
                      selections.Remove(selectedIndex);
                      listView1.Items\[selectedIndex\].BackColor = Color.White;
                  }
                  else
                  {
                      selections.Add(selectedIndex);
                      listView1.Items\[selectedIndex\].BackColor = Color.Blue;
                  }
              }
              else
              {
                  dontChange = !dontChange;
              }
              

              }

              of course this only works if you're selecting one item at a time. If you're doing an extended selection (meaning using Ctrl or Shift to select items) it wouldn't work because when you click the first time, it will add the item, then when you click the second time, it tries to make all of the items selected, which would deselect the first item. You'd have to include some key checking to see if Ctrl or Shift was down.

              S 2 Replies Last reply
              0
              • W William Winner

                This is a very basic solution, but here it is anyway:

                private List selections = new List();
                private bool dontChange = false;

                private void listView1_SelectedIndexChanged(object sender, EventArgs e)
                {
                if (!dontChange)
                {
                dontChange = !dontChange;
                int selectedIndex = listView2.SelectedIndices[0];

                    listView1.SelectedIndices.Clear();
                
                    //check if we're deselecting/selecting
                    if (selections.Contains(selectedIndex))
                    {
                        selections.Remove(selectedIndex);
                        listView1.Items\[selectedIndex\].BackColor = Color.White;
                    }
                    else
                    {
                        selections.Add(selectedIndex);
                        listView1.Items\[selectedIndex\].BackColor = Color.Blue;
                    }
                }
                else
                {
                    dontChange = !dontChange;
                }
                

                }

                of course this only works if you're selecting one item at a time. If you're doing an extended selection (meaning using Ctrl or Shift to select items) it wouldn't work because when you click the first time, it will add the item, then when you click the second time, it tries to make all of the items selected, which would deselect the first item. You'd have to include some key checking to see if Ctrl or Shift was down.

                S Offline
                S Offline
                sodevrom
                wrote on last edited by
                #8

                Hello, I tried something like you said, before posting on this forum, but I don't think it's going to work. Now the problem is when selecting multiple items (by dragging). It can work only with 1 item, but if you have multiple items, and remove one item at a time from the SelectedIndices, it will call the event again and again. Don't know exactly how to explain. Thanks anyways

                1 Reply Last reply
                0
                • E Ennis Ray Lynch Jr

                  Create your own list view and your own list view items. This is the easiest way to get the results you desire. In fact, it would have taken less than 4 hours, I bet.

                  Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                  S Offline
                  S Offline
                  sodevrom
                  wrote on last edited by
                  #9

                  Hello, Thanks for the suggestion, I was trying to avoid this...

                  1 Reply Last reply
                  0
                  • K Keith Barrow

                    I'm not sure I understand your question fully, but I think you can set the Enabled Property to true and the Readonly property to true also. Apologies in advance if I have misunderstood your question.

                    ragnaroknrol The Internet is For Porn[^]
                    Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

                    S Offline
                    S Offline
                    sodevrom
                    wrote on last edited by
                    #10

                    Hi, I don't think you understand exactly what I want. I simply want to view the selection in blue always (with multiple items), even if the listview has no focus. In a normal case, when the listivew loses focus, the selection turns gray, and you can't see it good.

                    1 Reply Last reply
                    0
                    • W William Winner

                      This is a very basic solution, but here it is anyway:

                      private List selections = new List();
                      private bool dontChange = false;

                      private void listView1_SelectedIndexChanged(object sender, EventArgs e)
                      {
                      if (!dontChange)
                      {
                      dontChange = !dontChange;
                      int selectedIndex = listView2.SelectedIndices[0];

                          listView1.SelectedIndices.Clear();
                      
                          //check if we're deselecting/selecting
                          if (selections.Contains(selectedIndex))
                          {
                              selections.Remove(selectedIndex);
                              listView1.Items\[selectedIndex\].BackColor = Color.White;
                          }
                          else
                          {
                              selections.Add(selectedIndex);
                              listView1.Items\[selectedIndex\].BackColor = Color.Blue;
                          }
                      }
                      else
                      {
                          dontChange = !dontChange;
                      }
                      

                      }

                      of course this only works if you're selecting one item at a time. If you're doing an extended selection (meaning using Ctrl or Shift to select items) it wouldn't work because when you click the first time, it will add the item, then when you click the second time, it tries to make all of the items selected, which would deselect the first item. You'd have to include some key checking to see if Ctrl or Shift was down.

                      S Offline
                      S Offline
                      sodevrom
                      wrote on last edited by
                      #11

                      Just a quick thought. The easiest way for this to work would be to "overwrite" the SystemColors . If it's possible to change the unfocused selection color form gray to blue, all my issues would have been solved...

                      1 Reply Last reply
                      0
                      • S sodevrom

                        Hello guys, First, I don't want to use OwnerDraw, that is a condition I have :( . What I want is set a custom color to the selection of a listview. I want the color to be visible even if the listview is not focused. I know about the HideSelection member, but that's not what I need. I tried to overrwrite the onselectionindexchanged, and make the item.selected=false, and after that, set a custom back/fore color. This does not work because I have multiple items, and it deselects all items but one. I need one of 2 things: a)Keep selection visible (with blue color, not gray, this is the problem with HideSelection) always b)Remember selected objects, but do not show the selection at all (because if the selection is visible, than the custom back/fore color are not visible), so I can "mimic" the selection by setting a custom back/fore color to the items. Now I have been spending over 4 hours now on this stupid issue, and no luck. Anyone has a suggestion? It would help a lot. *******************solution Instead of setting the color in the on selected changed event, we can do it on mouse up. It's not perfect, but it's the best I got. The best way to do this is using a custom draw listview. Here is the code:

                        List selections=new List();

                        protected override void OnMouseUp(MouseEventArgs e)
                        {
                        selections = this.SelectedIndices.Cast().ToList(); //store selection
                        this.SelectedIndices.Clear(); //clear selection

                                for (int i = 0; i < this.Items.Count; i++)
                                {
                                    if (this.selections.Contains(i))
                                        this.Items\[i\].BackColor = Color.Blue; //if selected, make it blue
                                    else
                                        this.Items\[i\].BackColor = Color.White; //if not selected, make it white
                                }
                                base.OnMouseUp(e);
                            }
                        

                        modified on Monday, August 2, 2010 9:01 PM

                        V Offline
                        V Offline
                        V 0
                        wrote on last edited by
                        #12

                        You could write your own control? Create a user control and add a panel with with background and add labels one below the other, you can provide a property that allows to set the background of the labels. Add some internal logic to maintain selection, colors, text, indexes, etc... Should be less then 4 hours to get you going.

                        V.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups