Filtering a combobox with a textbox - c#

As the title suggests, I want to filter the values in a combobox according to what is written in a textbox. The combobox takes values from a list. I have tried AutoCompleteMode and AutoCompleteSource but it doesn't let me add any values to the combobox when I use these. The combobox holds values of a list of the following class.
class Groep
{
//Fields
private string naamGroep;
//Properties
public string NaamGroep
{
get { return this.naamGroep; }
set { naamGroep = NaamGroep; }
}
//Constructor
public Groep(string naam)
{
this.naamGroep = naam;
}
This is the list:
List<Groep> Groepen = new List<Groep>();
I have two textboxes. One to add items to the list and the other to filter the combobox.

Do it using a foreach loop
private void Button1_Click(object sender, EventArgs e)
{
ComboBox1.Items.Clear();
foreach (Groep g in Groepen.Where(g => g.NaamGroep.Contains(TextBox1.Text)))
ComboBox1.Items.Add(g.NaamGroep);
}

Related

C# add item to listbox from list

When a shortcut is pressed, a value is added to the list and passed to the listbox to add an item. However, if AddRange(RecentColors.ToArray() is used, not only new list items are added, but they are added continuously. If a new value is added to a list value, is there a way to add only that new value to the listbox?
private void mainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.C)
{
RecentColors.Add(hexToRGB(hexcode.Text));
for(int i = 0; i < RecentColors.Count; i++)
{
color_list.Items.AddRange(RecentColors.ToArray());
}
}
}
Capture1
Capture2
When i add one item, i can't change the format that is already added..
The problem is that you has RecentColors with all items and each key press you add all items in the listbox. Try adding one element in RecentColors and also in the listbox:
var item = hexToRGB(hexcode.Text);
RecentColors.Add(item);
color_list.Items.Add(item);
UPDATE
Ok, first we are going to create a class for ListBox and ComboBox items.
public class ComboBoxItem
{
public bool Rgb { get; set; }
public override string ToString()
{
// Text to show in ComboBox
return this.Rgb ? "RGB" : "HTML";
}
}
public class ListBoxItem
{
public bool Rgb { get; set; }
public Color Color { get; set; }
public override string ToString()
{
// Text to show in ListBox
return this.Rgb ?
$"{this.Color.R},{this.Color.G},{this.Color.B}" :
$"{this.Color.R:X2}{this.Color.G:X2}{this.Color.B:X2}";
}
}
Each item in these controls will be an object of these classes. Add the items to the ComboBox:
this.comboBox1.Items.Add(new ComboBoxItem { Rgb = true });
this.comboBox1.Items.Add(new ComboBoxItem { Rgb = false });
When you need add items to ListBox:
this.listBox1.Items.Add(
new ListBoxItem { Rgb = true, Color = Color.Red });
Now, when you change between RGB and HTML in the ComboBox, you get the selected ComboBox item to know when show colors in RGB or in HTML. Then, iterate all ListBox items and set the RGB with this value. In this form, ListBox items will draw text in that mode. The ListBox doesn't know that your items has changes: you must force to redraw the items.
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var comboBoxItem = (ComboBoxItem)this.comboBox1.SelectedItem;
for (int i = 0; i < this.listBox1.Items.Count; i++)
{
var item = (ListBoxItem)this.listBox1.Items[i];
item.Rgb = comboBoxItem.Rgb;
// To force redraw
listBox1.Items[i] = listBox1.Items[i];
}
}
Here is the definition for AddRange:
Adds the elements of the specified collection to the end of the List<T>.
(source).
I could be misinterpreting what you're trying to do here, but you're adding the entire "RecentColors" sequence, every time your for loop is called.

Adding a ComboBox to a DataGridView programmatically

Suppose, I created this DataGridView in design view.
Now, I need to add items to the ComboBox column pro grammatically and show the item with index = 0.
The following code does load a ComboBox.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Rows.Add("1st Col", "2nd Col");
Column3.Items.Add("AAAA");
Column3.Items.Add("BBBB");
Column3.Items.Add("CCCC");
}
}
But, doesn't show 0-th item.
You could try:
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.Items.Add("AAAA");
cmb.Items.Add("BBBB");
cmb.Items.Add("CCCC");
dataGridView1.Rows.Add("1st Col", "2nd Col");
dataGridView1.Columns.Add(cmb);
}
You have to set current value for each cell in your column.
for (int i = 0; i < dataGridView.RowCount; i++)
dataGridView[3, i].Value = "AAAA";
I'm proposing you an alternative way the set the default/initial value of a DataGridViewComboBoxColumn, using a custom CellTemplate.
Assuming that (from the image shown), your ComboBox column is Column[2], you could assign it this custom CellTemplate, which can be initialized with a default value:
(Here, the default value is of type String, but you could make it generic if needed)
public class ComboBoxCell : DataGridViewComboBoxCell
{
private static string psDefaultValue;
public ComboBoxCell()
: base() { this.Value = psDefaultValue; }
public void DefaultValue(string Value)
{
psDefaultValue = Value;
this.Value = Value;
}
public override Type ValueType => typeof(String);
public override object DefaultNewRowValue => psDefaultValue;
}
When initializing a DataGridView, assign the custom CellTemplate to the DataGridViewComboBoxColumn:
ComboBoxCell ComboBoxCellTemplate = new ComboBoxCell();
dataGridView1.Columns[2].CellTemplate = ComboBoxCellTemplate;
Then, after filling in the ComboBox items list, define the default value and assign it using the custom DefaultValue() method of the CellTemplate:
((DataGridViewComboBoxColumn)dataGridView1
.Columns[2])
.Items.AddRange(new string[] { "Item1", "Item2", "Item3" });
((ComboBoxCell)dataGridView1.Columns[2]
.CellTemplate)
.DefaultValue(((DataGridViewComboBoxColumn)dataGridView1
.Columns[2]).Items[0].ToString());
After that, define the DatagridView DataSource. This way, for all new rows, the DataGridVieewComboBoxColumn will present the default value, the first item of the ComboBox in this case.

Add data from a list to a ListBox C#

I have list, listbox, button and a textbox.
My idea is to make that by clicking on the button, the content of the textbox is added to the list, and then pass the data to a listbox.
My problem is, if you add what I write, but the items that are in the listbox are overwritten by the new one that you insert. and I want only more items added. to the list and to go to the listbox. Thank you very much for your answers. This is the code of my button:
private void button54_Click(object sender, RoutedEventArgs e)
{
List<Playlists> List1 = new List<Playlists>();
List1.Add(new Playlists(textBox3.Text, #rutaalbum));
lbListbox.ItemsSource = List1;
}
I am just making a demo stand in for your Playlists class.
ToString has been overridden in order to display some text in the listbox. Without it, you will only display the class name.
ObservableCollection is used instead of List, so that the listbox updates when a new item is added.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MyList = new ObservableCollection<Playlists>();
MyListBox.ItemsSource = MyList;
}
private ObservableCollection<Playlists> MyList { get; }
private void Button_Click(object sender, RoutedEventArgs e)
{
MyList.Add(new Playlists(textBox3.Text));
}
}
public class Playlists
{
public Playlists(string title) { Title = title; }
public string Title { get; }
public override string ToString() { return Title; }
}
It seems , you are always creating items with new list. need to add items to existing list.
use below code. it would be useful.
private void button54_Click(object sender, RoutedEventArgs e) {
lbListBox.Items.Add(new Playlists(textBox3.Text, #rutaalbum));
}

Get/Set store multiple values for numerous buttons c#

I am trying to store multiple values from numerous buttons so I can return values of two or more things e.g. if chocolate and vanilla clicked both prices and names can be returned. I will also need to make calculations on the data set later. Whenever I return the data only the most recent values return rather than all of those I have selected.
private void VanillaBtn_Click(object sender, RoutedEventArgs e)
{
items.Price = 450;
items.Name = "Vanilla"
}
private void ChocolateBtn_Click(object sender, RoutedEventArgs e)
{
items.Price = 500;
items.Name = "Chocolate";
}
This is my class, any help or tips would be appreciated.
class Items
{
private int thePrice;
private string theName;
public int Price
{
get
{
return thePrice;
}
set
{
thePrice = value ;
}
}
public string Name
{
get
{
return theName;
}
set
{
theName = value;
}
}
Keep a list of whatever was clicked.
private List<Items> selectedItems = new List<Items>();
So, every time something is clicked, you store the object in the list defined above.
private void VanillaBtn_Click(object sender, RoutedEventArgs e)
{
var newItem = new Items();
newItem.Price = 450;
newItem.Name = "Vanilla";
selectedItems.Add(newItem);
}

Adding , removing items to and from a ListBox after binding

In following button event I'm adding items to a list from another list.
private void btnAdd_Click(object sender, EventArgs e)
{
if (lstPermissions.SelectedItem != null)
if (!lstGivenPermissions.Items.Contains(lstPermissions.SelectedItem))
{
lstGivenPermissions.Items.Add(lstPermissions.SelectedItem);
}
}
When the Items were hard coded in lstPermissions and lstGivenPermissions's datasource is not set , it was fine. But After binding data to lstGivenPermissions, when I try to execute this method I'm getting this exception.
Items collection cannot be modified when the DataSource property is set.
I'm using this property to bind data to the lstGivenPermissions
public List<string> GivenPermission
{
get { return lstGivenPermissions.Items.Cast<string>().ToList(); }
set { lstGivenPermissions.DataSource = value; }
}
I can understand that the databinding has caused this exception. But my requirement is that I want to load all permissions to lstPermissions and selected user's permissions to lstGivenPermission from the database. Then I should be able to add and remove items to and from lstGivenPermissions. Could you let me know how to do this?
You shouldn't be using a property to bind to a list control... Properties should just save/load values, like so:
private List<string> _givenPermission;
public List<string> GivenPermission
{
get { return _givenPermission; }
set { _givenPermission = value;}
}
If you must bind, try doing it this way instead:
private List<string> _givenPermission;
public List<string> GivenPermission
{
get { return _givenPermission; }
set { _givenPermission = value; lstGivenPermissions.DataSource = value; }
}

Categories