How to suggest ComboBox values by searching - c#

I'm creating a windows application and I want to show combo box value by filtering from the user input value. There is a property called AutoComplete but I'm assigning values for comboBox items using an object. So I couldn't fill AutoCompletedSource. How can I assign my item list to it?

There are two main ways to get autocomplete on a combo box. The first is to set the source to the comboBox.Items:
comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
comboBox1.Items.AddRange(new []{"Omg", "So Kewel"," I love it"});
The second is to set up a custom source. In the second case the drop down arrow will reveal nothing but the autocomplete will show when you start typing.
comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
var list = new List<string>() {"Omg", "So Kewl", "I love it"};
var collection = new AutoCompleteStringCollection();
collection.AddRange(list.ToArray());
comboBox1.AutoCompleteCustomSource = collection;

Related

Trying to disable few items in a ListBox c#

I'm trying to disable a few items dynamically from a Listbox. I used the following code:
How To Disable Selected Item In List Box
But, all I see is System.Data.DataRow in the Listbox, not the actual string.
I tried adding new BindingContext()
lbInsTypes.BindingContext = new BindingContext();
Also,
lbInsTypes.DataBindings.Add("DisplayMember", dtInsType, "InsuranceDescription", false, DataSourceUpdateMode.OnPropertyChanged);
but nothing worked.
DataSet dsInsType = db.GetAllInsuranceTypes(InstutionId);
lbInsTypes.DisplayMember = "InsuranceDescription";
lbInsTypes.ValueMember = "InsuranceTypeId";
lbInsTypes.DataSource = dsInsType.Tables[0];
I want to see the actual InsuranceDescription values in the ListBox from the table. Please show me how to use datasource with custom ListBox control.

Add more items to same Row and same column in listview

I know how to add/delete items but I don't know how to add more items to the same field (same row and same column). I want whenever I click a button, an item is added to same selected row but not to new row in the listView.
I uploaded a photo you can check to see what I exactly mean.
Consider looking at ObjectListView or DataGridView instead of what you are currently. It may be more flexible to your needs.
Your question is somewhat unclear. Clearly you are using listView and you have columns and rows resulting in a cell / box / grid location. I gather that, after its initial creation, you wish to append or alter the data at that location.
To get to the point: Multi-line text within a given 'cell' is not supported (as best I can tell). The picture you have shown is likely a custom object or something similar to a listView, but different (such as a ObjectListView). Or perhaps a picture.
listView2.Items[0].SubItems[4].Text = "123\nabc"; //Doesn't add a proper newline like a normal string
listView2.Items[0].SubItems[4].Text = "123\rabc"; //Doesn't add a proper return carriage like a normal string
listView2.Items[0].SubItems[4].Text = "123\r\nabc"; //Doesn't add a proper newline like a normal string
I am assuming you are using the details view
listView1.View = View.Details;
First adding your headers, listView1.Columns.Add(text, width);
listView1.Columns.Add(First Name", 50);
listView1.Columns.Add("Middle Name", 100);
listView1.Columns.Add("Last Name", 100);
You then add data to the listView. However, this is not done directly. You build a listViewITEM then add that item to the list view.
string[] row = new string[3];
row[0] = "john";
row[1] = "someone";
row[2] = "doe";
ListViewItem lvi = new ListViewItem(row);
listView1.Items.Add(item);
listView1.SelectedItems[#].SubItems[#].Text = "string" + "\n" + "string2";
CrazyPaste suggested adding a row, which could be practical and is something you often see with listViews.
However, If you choose to add or "redo" the rows, be sure to remove any old information before inputting new information to avoid duplicates.
Taken from the popup within visual studio 2013 pro
listView1.Items.RemoveAt(int index)
listView1.Items.Insert(int index, string key, string text, int imageIndex)
OR
listView1.Items.Clear(); //Clears all items
then
//Add populate logic here
Two arrays or a multidimensional array in a loop would be effective if you wish to populate the listview in that manner.
To achieve this programmatically, you could...
listView2 = new ListView();
listView2.View = View.Details;
listView2.Location = new Point(50, 50);
listView2.Size = new Size(400, 100);
this.Controls.Add(listView2);
listView2.Columns.Add("AAA");
listView2.Columns.Add("BBB");
listView2.Columns.Add("CCC");
listView2.Columns.Add("DDD");
listView2.Columns.Add("EEE");
ListViewItem item1 = new ListViewItem();
item1.Text = "0"; //The way to properly set the first piece of a data in a row is with .Text
item1.SubItems.Add("1"); //all other row items are then done with .SubItems
item1.SubItems.Add("2");
item1.SubItems.Add("3");
item1.SubItems.Add("");
item1.SubItems.Add("");
ListViewItem item2 = new ListViewItem();
item2.Text = "00";
item2.SubItems.Add("11");
item2.SubItems.Add("22");
item2.SubItems.Add("33");
item2.SubItems.Add("");
item2.SubItems.Add("");
ListViewItem item3 = new ListViewItem();
item3.Text = "000";
item3.SubItems.Add("111");
item3.SubItems.Add("222");
item3.SubItems.Add("333");
item3.SubItems.Add("");
item3.SubItems.Add("");
//item1.SubItems.Clear();
//item1.SubItems.RemoveAt(1);
listView2.Items.Add(item1);
listView2.Items.Add(item2);
listView2.Items.Add(item3);
//listView2.Items.Insert(2, item1); //0 here is the row. Increasing the number, changes which row you are writing data across
listView2.Items[0].SubItems[4].Text = "123\rabc";
To 'update' the information:
listView1.Items.Clear();
listView1.Items.Add(item1);
listView1.Items.Add(item2);
...etc
NOTES:
I was not able to get .Insert to work with subitems.
If you already inserted a listViewItem, You cannot insert an item
without first removing it
SubItems are not automatically created to fill empty space. Commands like 'listView2.Items[0].SubItems[4].Text' will not work with null/non-existent SubItems
I don't have much to go on. But this adds a new row:
string[] row = { "1", "snack", "2.50" };
var listViewItem = new ListViewItem(row);
listView1.Items.Add(listViewItem);
Here's a post discussing how to update an existing listitem:
C#: How do you edit items and subitems in a listview?
Ok. after I searched the internet for ages, it turned out that listView does not support text wrap. so instead I used DataGridView. thank you for your help

The difference between ListBox.DataSource collection versus ListBox.Items?

I am dynamically creating a Winforms multi-select ListBox and adding it into a flowpanel control. I bind a datasource from an object I created and verified that the DataSource has does in fact have approximately 14 elements. When I do a listBox.SetSelected(0, true) I get an System.ArgumentOutOfRangeException error thrown.
I have determined the problem is that while the DataSource has 14 elements, the Item collection has none (0) and is therefore throwing the exception. My question is why are these two different from one another, and why would I not simply do a foreach item in datasource add to the item collection?
The following is the code I have so far:
case InsertableItemParameter.ParameterType.ListBox:
//note: two-way bindings are not possible with multiple-select listboxes
Label lblListBox = new Label();
lblListBox.Text = param.DisplayText;
ListBox listBox = new ListBox();
listBox.DataSource = param.Values;
listBox.DisplayMember = "Value";
listBox.SelectionMode = SelectionMode.MultiExtended;
listBox.Size = new System.Drawing.Size(flowPanel.Size.Width - lblListBox.Size.Width - 10, 100);
listBox.SetSelected(0, true); //will throw argument out of range exception here!
listBox.SetSelected(1, true);
flowPanel.Controls.Add(lblListBox);
flowPanel.Controls.Add(listBox);
flowPanel.SetFlowBreak(listBox, true);
break;
Below is an alternative solution I attempted and worked, but again why would I use DataSource versus Items collection?
case InsertableItemParameter.ParameterType.ListBox:
//note: two-way bindings are not possible with multiple-select listboxes
Label lblListBox = new Label();
lblListBox.Text = param.DisplayText;
ListBox listBox = new ListBox();
//listBox.DataSource = param.Values;
listBox.DisplayMember = "Value";
listBox.SelectionMode = SelectionMode.MultiExtended;
listBox.Size = new System.Drawing.Size(flowPanel.Size.Width - lblListBox.Size.Width - 10, 100);
listBox.BeginUpdate();
foreach (String paramater in param.Values)
{
listBox.Items.Add(paramater);
}
listBox.EndUpdate();
listBox.SetSelected(0, true);
listBox.SetSelected(1, true);
flowPanel.Controls.Add(lblListBox);
flowPanel.Controls.Add(listBox);
flowPanel.SetFlowBreak(listBox, true);
break;
ANSWER: Thanks for all the responses. The issue here is visibility and win-form rendering. While the difference between DataSource and Items collections were not really addressed save a few people, the true source of my problem was resolved by calling the SetSelected() method after the form was done drawing. This causes a lot of problems in the design of my application which I have to resolve, but this is was the problem. See the reply I marked as the answer.
Your problem probably lies elsewhere, because this code works fine:
string[] ds = {"123","321"};
listBox1.DataSource = ds;
listBox1.SetSelected(1, true);
MessageBox.Show(listBox1.Items.Count.ToString()); //returns 2
Tested in a brand new C# project with a listBox1 put on the form, and the above code sitting in Form_Load.
EDIT: I did not realize that creating a ListBox in runtime could make a difference, and especially because it matters when to set selected items. This code works:
string[] ds = { "123", "321" };
ListBox lst = new ListBox();
lst.DataSource = ds;
lst.Size = new Size(100,100);
this.Controls.Add(lst);
//make sure to call SetSelected after adding the ListBox to the parent
lst.SetSelected(1, true);
Thanks to #Brad for pointing this out. So back on the original question, replace this:
listBox.SetSelected(0, true);
listBox.SetSelected(1, true);
flowPanel.Controls.Add(lblListBox);
flowPanel.Controls.Add(listBox);
with this:
flowPanel.Controls.Add(lblListBox);
flowPanel.Controls.Add(listBox);
listBox.SetSelected(0, true);
listBox.SetSelected(1, true);
And it should work.
You have two options of how to get data to be available in a ListBox. You can set the DataSource or you can add the items manually via listBox.Items.Add(paramater). You cannot do both because they will step on each other hence your error
...cannot add items to the Item collection when DataSource is set.
Items From MSDN
This property enables you to obtain a reference to the list of items that are currently stored in the ListBox. With this reference, you can add items, remove items, and obtain a count of the items in the collection. For more information about the tasks that can be performed with the item collection, see the ListBox.ObjectCollection class reference topics.
Datasource From MSDN
An object that implements the IList or IListSource interfaces, such as a DataSet or an Array. The default is null
I'm not an expert on this matter, but from what I read it appears that Items allows you to add/modify the contents in the list whereas Datasource retrieves and sets the content.
The Items collection is populated from the DataSource only when the Control is visible. since you create your control dynamically, it is not added to the parent control and therefore not visible. Therefore you first need to have a Control that is visible on the screen. In your code you set DataSource and then set the selected items before your Control is visible on the FlowChart since it isn't added to the Parent control. You should change the sequence of the statements. You should add the listBox to the FlowPanel which will populate Items collection from the DataSource upon which you can execute SetSelected() method. Try this and note the changed order of the execution of your initial code:
ListBox listBox = new ListBox();
listBox.DataSource = param.Values;
listBox.DisplayMember = "Value";
listBox.SelectionMode = SelectionMode.MultiExtended;
listBox.Size = new System.Drawing.Size(flowPanel.Size.Width - lblListBox.Size.Width - 10, 100);
flowPanel.Controls.Add(lblListBox);
flowPanel.Controls.Add(listBox); //notice that you first add the listBox to the flowChart
listBox.SetSelected(0, true); //and then you have items in the Items collection which you can select
listBox.SetSelected(1, true);
Im not sure why there are two different Collections. The Items property seems more simple.
I found the reason for the exception:
apparently you have to do things in a specific order, like this:
//init the listbox
var listBox1 = new ListBox();
listBox1.Location = new System.Drawing.Point(122, 61);
listBox1.Size = new System.Drawing.Size(205, 147);
listBox1.SelectionMode = SelectionMode.MultiExtended;
Controls.Add(listBox1); //<-- point of interest
//then set the DataSource
listBox1.DataSource = lst;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "Age";
//then set the selected values
listBox1.SetSelected(0, true);
listBox1.SetSelected(1, true);
My Test class look like this:
public class Test
{
private static Random r = new Random();
public Test (string name)
{
Name = name;
Age = r.Next(16, 45);
}
public string Name { get; set; }
public int Age{ get; set; }
}
And lst is declared like this:
var lst = new List<Test>()
{
new Test("jens"),
new Test("Tom"),
new Test("John"),
new Test("Don"),
new Test("Jenny"),
};

Adding custom options in bound dropdown in asp.net

I have a bound dropdown list populated with a table of names through a select, and databinding. it shoots selectedindexchanged that (through a postback) updates a certain gridview.
What happens is, since it runs from changing the index, the one that always comes selected (alexander) can only me chosen if you choose another one, then choose alexander. poor alexander.
What I want is to put a blanc option at the beginning (default) and (if possible) a option as second.
I can't add this option manually, since the binding wipes whatever was in the dropdown list and puts the content of the datasource.
Set the AppendDataBoundItems property to True. Add your blank, then data bind.
ddl.AppendDataBoundItems = true;
ddl.Items.Add("Choose an item");
ddl.DataSource = foo;
ddl.DataBind();
The AppendDataBoundItems property
allows you to add items to the
ListControl object before data
binding occurs. After data binding,
the items collection contains both the
items from the data source and the
previously added items.
protected void SetAddrList()
{
TestDataClassDataContext dc = new TestDataClassDataContext();
dc.ObjectTrackingEnabled = false;
var addList = from addr in dc.Addresses
from eaddr in dc.EmployeeAddresses
where eaddr.EmployeeID == _curEmpID && addr.AddressID == eaddr.AddressID && addr.StateProvince.CountryRegionCode == "US"
select new
{
AddValue = addr.AddressID,
AddText = addr.AddressID,
};
if (addList != null)
{
ddlAddList.DataSource = addList;
ddlAddList.DataValueField = "AddValue";
ddlAddList.DataTextField = "AddText";
ddlAddList.DataBind();
}
ddlAddList.Items.Add(new ListItem("<Add Address>", "-1"));
}
I created this code example using adventure works to do a little practice with Linq and it is very similar to the previous answer. Using linq still shouldn't matter for the answer the last dddlAddList.Items.Add is what you need. "Add Address" = first selected option and -1 = the value.

DataBind listBox selected item to textboxes

I have ListBox data bind to list of users (collection):
usersListBox.DataSource = null;
usersListBox.DataSource = _users;
usersListBox.DisplayMember = "Name";
usersListBox.ValueMember = "Id";
Now I want properties of selected item to be shown in different text boxes, so I do the binding:
nameTextBox.DataBindings.Add("Text", usersListBox.SelectedItem, "Name");
loginTextBox.DataBindings.Add("Text", usersListBox.SelectedItem, "Login");
When form load I can see that values of selected item appear in textboxes, but when selected item in listBox is changed, values in text boxes are still the same. Do I have to catch selectedItemChanged of listbox and repeat binding of textboxes? Or I'm missing something and values in textboxes should change with changing selected item?
If someone needs answer: you have to create binding source object and assign it list box and textboxes:
usersBindingSource = new BindingSource();
usersBindingSource.DataSource = _presenter.Users;
usersListBox.DataSource = usersBindingSource;
usersListBox.DisplayMember = "Name";
usersListBox.ValueMember = "Id";
nameTextBox.DataBindings.Add("Text", usersBindingSource, "Name", true, DataSourceUpdateMode.OnPropertyChanged);
loginTextBox.DataBindings.Add("Text", usersBindingSource, "Login", true, DataSourceUpdateMode.OnPropertyChanged);

Categories

Resources