there are many titles about my problem but i could not find any solution.
I am taking a collection from web service and i am filtering it with double click in ListBox.
SharedData.cs
public class SharedData
{
private List<OsbDownloadItem> _allItems;
public List<OsbDownloadItem> AllItems
{
get
{
if (null == _allItems)
_allItems = new List<OsbDownloadItem>();
return _allItems;
}
set
{
_allItems = value;
}
}
/// <summary>
/// Returns a list of distinct sob values in AllItems.
/// </summary>
/// <returns></returns>
public List<string> GetDistinctSOBS()
{
List<string> distinctSobNames = new List<string>();
for (int i = 0; i < _allItems.Count; i++)
{
string sob = _allItems[i].SOB;
if (distinctSobNames.Contains(sob))
{
continue;
}
distinctSobNames.Add(sob);
}
return distinctSobNames;
}
/// <summary>
/// Returns distinct streets under a sob.
/// </summary>
/// <param name="sob"></param>
/// <returns></returns>
public List<string> GetDistinctStreetsBySOB(string sob)
{
List<string> streets = new List<string>();
for (int i = 0; i < _allItems.Count; i++)
{
OsbDownloadItem currentOsbItem = _allItems[i];
if (currentOsbItem.SOB != sob)
continue;
if (streets.Contains(currentOsbItem.Street))
continue;
streets.Add(currentOsbItem.Street);
}
return streets;
/*List<OsbDownloadItem> itemsTemp = SharedData.Instance.AllItems.Where(i => i.SOB.Equals(sob)).ToList();
List<string> sobs = itemsTemp.Select(i => i.SOB).ToList();
return sobs;*/
}
/// <summary>
/// Returns distinct connectionarticles under a street.
/// </summary>
/// <param name="street"></param>
/// <returns></returns>
public List<string> GetDistinctConnectionArticlesBySTREET(string street)
{
List<string> connectionArticles = new List<string>();
for (int i = 0; i < _allItems.Count; i++)
{
OsbDownloadItem currentOsbItem = _allItems[i];
if (currentOsbItem.Street != street)
continue;
if (connectionArticles.Contains(currentOsbItem.ConnectionArticle))
continue;
connectionArticles.Add(currentOsbItem.ConnectionArticle);
}
return connectionArticles;
}
/// <summary>
/// Returns distinct installition under a street.
/// </summary>
/// <param name="connection_article"></param>
/// <returns></returns>
public List<string> GetDistinctInstallitionsByCONNECTIONARTICLE(string connection_article)
{
List<string> installitions = new List<string>();
for (int i = 0; i < _allItems.Count; i++)
{
OsbDownloadItem currentOsbItem = _allItems[i];
if (currentOsbItem.ConnectionArticle != connection_article)
continue;
if(installitions.Contains(currentOsbItem.Installation))
continue;
installitions.Add(currentOsbItem.Installation);
}
return installitions;
}
public Level CurrentLevel { get; set; }
private SharedData()
{
}
private static SharedData _instance;
public static SharedData Instance
{
get
{
if (null == _instance)
_instance = new SharedData();
return _instance;
}
}
}
Form1.cs
private void button1_Click(object sender, EventArgs e)
{
DateTime date = datePicker.Value;
bool includeCompleted = false;
string username = usernameTextBox.Text;
string password = passwordTextBox.Text;
string serialNumber = serialNumberTextBox.Text;
AuthHeader hdr = new AuthHeader();
hdr.UserName = username;
hdr.Password = password;
hdr.SerialNumber = serialNumber;
OSBService.MobileService service = new WindowsFormsApplication1.OSBService.MobileService();
service.AuthHeaderValue = hdr;
WebMethodResult result = service.GetOsbItems(date, includeCompleted);
if (result.HasErrors)
{
MessageBox.Show(result.Messages.ToString());
return;
}
SharedData.Instance.AllItems = ((OsbDownloadItem[])result.ReturnValue).ToList();
itemsListBox.DataSource = SharedData.Instance.GetDistinctSOBS();
SharedData.Instance.CurrentLevel = Level.SOB;
}
private void itemsListBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (SharedData.Instance.CurrentLevel == Level.SOB)
{
string value = "";
int index = this.itemsListBox.IndexFromPoint(e.Location);
if (index != System.Windows.Forms.ListBox.NoMatches)
{
value = SharedData.Instance.AllItems[index].ToString();
}
string sob = SharedData.Instance.GetDistinctSOBS()[index];
itemsListBox.DataSource = SharedData.Instance.GetDistinctStreetsBySOB(sob);
SharedData.Instance.CurrentLevel = Level.STREET;
}
else if (SharedData.Instance.CurrentLevel == Level.STREET)
{
string value = "";
int index = this.itemsListBox.IndexFromPoint(e.Location);
if (index != System.Windows.Forms.ListBox.NoMatches)
{
value = SharedData.Instance.AllItems[index].ToString();
}
string sob = SharedData.Instance.GetDistinctSOBS()[index];
string street = SharedData.Instance.GetDistinctStreetsBySOB(sob)[index];
itemsListBox.DataSource = SharedData.Instance.GetDistinctConnectionArticlesBySTREET(street);
SharedData.Instance.CurrentLevel = Level.CONNECTION_ARTICLE;
}
else if (SharedData.Instance.CurrentLevel == Level.CONNECTION_ARTICLE)
{
string value = "";
int index = this.itemsListBox.IndexFromPoint(e.Location);
if (index != System.Windows.Forms.ListBox.NoMatches)
{
value = SharedData.Instance.AllItems[index].ToString();
}
string sob = SharedData.Instance.GetDistinctSOBS()[index];
string street = SharedData.Instance.GetDistinctStreetsBySOB(sob)[index];
string connection_article = SharedData.Instance.GetDistinctConnectionArticlesBySTREET(street)[index];
itemsListBox.DataSource = SharedData.Instance.GetDistinctInstallitionsByCONNECTIONARTICLE(connection_article);
SharedData.Instance.CurrentLevel = Level.INSTALLATION;
}
else if (SharedData.Instance.CurrentLevel == Level.INSTALLATION)
{
// TODO:
}
else
{
//TODO:
}
}
When i click first indexies in listbox, it works but when i click first index and after filtering when i click second or third index i am getting this error"Index was out of range. Must be non-negative and less than the size of the collection".
Aucun commentaire:
Enregistrer un commentaire