Skip to content

Commit

Permalink
add ComboListSource class.
Browse files Browse the repository at this point in the history
  • Loading branch information
clown committed Jun 30, 2022
1 parent b69625f commit 298706b
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@
// limitations under the License.
//
/* ------------------------------------------------------------------------- */
namespace Cube.Mixin.Forms.Controls;
namespace Cube.Forms;

using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

/* ------------------------------------------------------------------------- */
///
/// ComboBoxExtension
/// ComboListExtension
///
/// <summary>
/// Provides the extended methods of the ComboBox class.
/// Provides the extended methods for the ComboBox and ComboListSource
/// classes.
/// </summary>
///
/* ------------------------------------------------------------------------- */
public static class ComboBoxExtension
public static class ComboListExtension
{
/* --------------------------------------------------------------------- */
///
Expand All @@ -41,10 +42,10 @@ public static class ComboBoxExtension
/// </summary>
///
/// <param name="src">ComboBox object.</param>
/// <param name="data">UserData</param>
/// <param name="data">Collection of binding data.</param>
///
/* --------------------------------------------------------------------- */
public static void Bind<T>(this ComboBox src, IList<KeyValuePair<string, T>> data)
public static void Bind<T>(this ComboBox src, ComboListSource<T> data)
{
var obj = src.SelectedValue;
var cmp = EqualityComparer<T>.Default;
Expand All @@ -54,6 +55,6 @@ public static void Bind<T>(this ComboBox src, IList<KeyValuePair<string, T>> dat
src.ValueMember = "Value";

if (obj is T v && data.Any(e => cmp.Equals(e.Value, v))) src.SelectedValue = v;
else if (data.Count > 0) src.SelectedValue = data[0].Value;
else if (data.Any()) src.SelectedValue = data.First().Value;
}
}
121 changes: 121 additions & 0 deletions Libraries/Forms/Sources/ComboListSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* ------------------------------------------------------------------------- */
//
// Copyright (c) 2010 CubeSoft, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/* ------------------------------------------------------------------------- */
namespace Cube.Forms;

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;

/* ------------------------------------------------------------------------- */
///
/// ComboListSource
///
/// <summary>
/// Represents the data source to be bound to the ComboBox object.
/// </summary>
///
/* ------------------------------------------------------------------------- */
public class ComboListSource<T> : IEnumerable<KeyValuePair<string, T>>, IListSource
{
#region Properties

/* --------------------------------------------------------------------- */
///
/// ContainsListCollection
///
/// <summary>
/// Gets a value indicating whether the collection is a collection of
/// IList objects.
/// </summary>
///
/* --------------------------------------------------------------------- */
bool IListSource.ContainsListCollection => false;

#endregion

/* --------------------------------------------------------------------- */
///
/// GetList
///
/// <summary>
/// Returns an IList that can be bound to a data source from an object
/// that does not implement an IList itself.
/// </summary>
///
/// <returns>
/// An IList that can be bound to a data source from the object.
/// </returns>
///
/* --------------------------------------------------------------------- */
public IList GetList() => _inner;

/* --------------------------------------------------------------------- */
///
/// GetEnumerator
///
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
///
/// <returns>
/// Enumerator that can be used to iterate through the collection.
/// </returns>
///
/* --------------------------------------------------------------------- */
public IEnumerator<KeyValuePair<string, T>> GetEnumerator() => _inner.GetEnumerator();

/* --------------------------------------------------------------------- */
///
/// GetEnumerator
///
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
///
/// <returns>
/// IEnumerator object that can be used to iterate through the
/// collection.
/// </returns>
///
/* --------------------------------------------------------------------- */
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

/* --------------------------------------------------------------------- */
///
/// Add
///
/// <summary>
/// Adds the specified key and value pair to the collection.
/// </summary>
///
/// <param name="key">Display contents in the ComboBox object.</param>
/// <param name="value">
/// Value associated with the displayed content of the ComboBox object.
/// </param>
///
/// <remarks>
/// The method is mainly used with the collection initializer.
/// </remarks>
///
/* --------------------------------------------------------------------- */
public void Add(string key, T value) => _inner.Add(new(key, value));

#region Fields
private readonly List<KeyValuePair<string, T>> _inner = new();
#endregion
}

0 comments on commit 298706b

Please sign in to comment.