CancelEdit causes Listbox scrolling?

CancelEdit causes Listbox scrolling?

Old forum URL: forums.lhotka.net/forums/t/4685.aspx


ErikPhilips posted on Sunday, April 13, 2008

I must be going crazy.  Scroll down to the bottom object, then select any object, the listbox will always scroll up so the selected item is at the bottom of the visible list.  Occurs on the CancelEdit.

using System;
using System.Windows.Forms;
using Csla;
using Csla.Core;

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

      BusinessBase _currentItem = null;
      BindingSource _bindingSource = new BindingSource();
      People people = new People();
      private void Form1_Load(object sender, EventArgs e)
      {
         Height = 400;
         Width = 600;

         ListBox listbox1 = new ListBox();
         listbox1.Width = 500;
         listbox1.Height = 300;
         listbox1.Top = 50;
         listbox1.Left = 50;
         Controls.Add(listbox1);

         for ( int i = 0; i < 50; i++ )
         {
            Person person = new Person();
            person.Name = "Test" + i.ToString();
            people.Add(person);
         }

         _bindingSource.CurrentChanged += new EventHandler(bs_CurrentChanged);
         _bindingSource.DataSource = people;
         listbox1.DataSource = _bindingSource;
      }

      void bs_CurrentChanged(object sender, EventArgs e)
      {
         if ( _currentItem != null )
            _currentItem.CancelEdit();
         _currentItem = (BusinessBase) _bindingSource.Current;
         _currentItem.BeginEdit();
      }
   }

   public class People: BusinessListBase
   {
   }

   public class Person: BusinessBase
   {
      private string _name;
      public string Name
      {
         get
         {
            return _name;
         }
         set
         {
            _name = value;
         }
      }
      public override string ToString()
      {
         return _name;
      }
   }
}

ErikPhilips replied on Monday, April 14, 2008

Is anyone using BeginEdit without attaching it to an "Edit" button?  My thought process is that if the position is changing on the BindingSource (CurrentChanged), to Cancel any edits if not dirty (and if dirty I have a popup in my real code) on the original current item, and do a BeginEdit on the new current item.  So far my users hate hitting and edit button and automatically.  The new process works but for whatever reason the listbox selection changes and I'm not sure why.

sergeyb replied on Monday, April 14, 2008

I use BeginEdit without edit button.  I think, you will need to unbind and rebind your controls.  On a few occasions I added code to Cancel to remember what item was highlighted and then selected the same item after cancel and re-bind (if available – it could have been new item)

 

Sergey Barskiy

Senior Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: ErikPhilips [mailto:cslanet@lhotka.net]
Sent: Monday, April 14, 2008 2:28 PM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] CancelEdit causes Listbox scrolling?

 

Is anyone using BeginEdit without attaching it to an "Edit" button?  My thought process is that if the position is changing on the BindingSource (CurrentChanged), to Cancel any edits if not dirty (and if dirty I have a popup in my real code) on the original current item, and do a BeginEdit on the new current item.  So far my users hate hitting and edit button and automatically.  The new process works but for whatever reason the listbox selection changes and I'm not sure why.



tetranz replied on Monday, April 14, 2008

Hi Erik,

I tried your little test program. It's happening because the bindingSource fires it's ListChanged event with a ListChangedType of Reset. I think it's relaying the event from People. That tells the ListBox to reload the list. I think it effectively goes to the top but remembers it's value and then scrolls down just far enough for it to be visible.
 
A solution seems to be to set _bindingSource.RaiseListChangedEvents to false during the CancelEdit. I didn't quite understand it all when I had a quick play with breakpoints. It seemed like the BeginEdit was also firing ListChanged but that wasn't affecting the ListBox. There may be more to it.

Ross

ErikPhilips replied on Monday, April 14, 2008

 

That's a fantastic solution as far as I can see.  I went through all the events of the listbox but apparently wasn't thorough enough.  Thanks a lot! Smile [:)]

Copyright (c) Marimer LLC