Dropdown box that when a key is pressed, jumps to selection - c#

I have a Combo box that defines the contents of other boxes on the screen.
So if I was to, for example, type "Apple" in my Combo box for employee names, it will cause an error because there are no "Apple" employees in my database, meaning nothing will be retrieved to populate the rest of the form.
Is there a property I can set or code snippet I can write to make sure anything the user types in the combo box will instead highlight a selection from the draw?

You can set DropDownStyle to DropDownList (instead of DropDown). Then the user can stil type, but the Combobox only switches the selection if such an Element exists.
(However this does change the ComboBox-Appearence, so it no longer looks like "Freetext" available)
Set AutoCompleteMode to Suggest and AutoCompleteSource to ListItems in order to allow typing of more than just the first letter. (The Combobox will expand and switch the element as the user types in more letters.)

check the datasource(datatable,dataset etc etc) count that you are binding into the ComboBox
if(DataSource!=null && DataSource.rows.count>0)
{
combobox.datasource=DataSource;
combobox.refresh();
}

I would suggest you to do the following things:
1.Take a TextBox and set the properties of the TextBox as
AutoCompleteMode=SuggestAppend and AutoCompleteSource = CustomSource.
2.Load a values you want, in the datatable (probably Employees name or any other column you want).
3.Call the below method in you page load or any event you want.
public bool AutoComplete()
{
try
{
DataTable dtEmpName=/////// store the employees name in this DataTable.
var empNames = dtEmpName.Select(s => s.EmpName('the column you want').Distinct().ToArray();
/////// Auto complete Name from Surname
AutoCompleteStringCollection instcol = new AutoCompleteStringCollection();
instcol.AddRange(empNames);
txtEmpNames.AutoCompleteCustomSource = instcol;
}
catch (Exception ex)
{
throw ex;
}
}

Related

Use typed text from AutoComplete in DataGridViewComboBoxColumn, even if that value is not in the Combo Box DataSource

I have a DataGridViewComboBoxColumn, sometimes the ComboBox has a list of items, but sometimes the list is empty.
I have enabled the AutoCompleteMode, in order for user to type in what item he wants.
I want to have the possibility to use the typed text from the user as a valid item.
I mean, in case the list is empty, the user can type in a text into ComboBox and to use that one as input.
Also, when the list is not empty, the user can type in a text into ComboBox and even if that item name is not in the list, to use it as input.
At the moment, it works just when the user types in a value that is in the list, then that value can be used. If the user types in a value that is not in the list, the value is not used as desired one.
This is how I enabled the AutoComplete:
private void DataGridNewOrders_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox comboBox = e.Control as DataGridViewComboBoxEditingControl;
if (comboBox != null)
{
comboBox.DropDownStyle = ComboBoxStyle.DropDown;
comboBox.AutoCompleteMode = AutoCompleteMode.Suggest;
comboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
}
}
Is there any property that allows the typed text from ComboBox to be used even if that text is not in the list ?
Thank you.

Datagrid binding to a list based on a value of a property

The datagrid is databound to List.
class Channel
{
string Name;
string id;
customEnum val;
}
where
Enum CustomEnum {All=0,TypeA, TypeB }
Now based on the selection from a combobox i want to display in the grid.Suppose the combobox selected value is TypeA. Only they have to be displayed in the grid. others has to be removed/hid. How do i do that? Also the first column in the grid displayed is index of the row. I have added id to keep the index(i used to generate the index based on the item index in the list). Since the rows will be changed on selection of the combobox how do i show the index now ?
Check this link , and use the Filter option for the ICollection

Is there a way to make a cell editable where column is readonly in XtraGrid?

My grid looks like this.
Key Value
1 A
2 B
3 C
I have Value column read-only in my grid. "Value" column's columnedit is associated with memoexedit repository. Now what I want to do is on some condition like when key=2,
I want my value cell to be editable.
I tried making the whole column ReadOnly = false.
and then handled ShowingEditor to cancel edit on everything else than Key=2, but that prevents even opening up the editor for other values.
What I want is able to see the editor but it should be readonly for Others
and for Key=2, It should be editable.
Please help!
Try to handle the ShownEditor event as the following (semi-pseudo code):
var grid = sender as GridView;
if (grid.FocusedColumn.FieldName == "Value") {
var row = grid.GetRow(grid.FocusedRowHandle) as // your model;
// note that previous line should be different in case of for example a DataTable datasource
grid.ActiveEditor.Properties.ReadOnly = // your condition based on the current row object
}
This way you could refine the already opened editor with your needs.

Add 2 or more field to value member of C# coding

I have table with 4 primary key fields. I load that in to drop down list in my WinForm application created by using C#.
On the TextChanged event of drop down list I have certain TextBox and I want to fill the information recived by the table for the certain field I selected by the drop down list.
So as I say the table having 4 fields. Can I get those all 4 fields into value member from the data set, or could you please tell me whether is that not possible?
Thank you.
Datatable dt=dba.getName();
cmb_name.ValueMember="id";
cmb_name.DisplayMember="name";
cmb_name.DataSource=dt;
this is normal format.. but i have more key fields.. so i need to add more key fields..
You can use DataSource property to bind your source data to the ComboBox (e.g. a List of Entities, or a DataTable, etc), and then set the DisplayMember property of the ComboBox to the (string) name of the field you want to display.
After the user has selected an Item, you can then cast the SelectedItem back to the original row data type (Entity, DataRow, etc - it will still be the same type as you put in), and then you can retrieve your 4 composite keys to the original item.
This way you avoid the SelectedValue problem entirely.
Edit:
Populate as follows:
cmb_name.DisplayMember = "name";
cmb_name.DataSource = dt;
// Ignore ValueMember and Selected Value entirely
When you want to retrieve the selected item
var selectedRow = (cmb_name.SelectedItem as DataRowView );
Now you can retrieve the 4 values of your PK, e.g. selectedRow["field1"], selectedRow["field2"], selectedRow["field3"] etc
If however you mean that you want to DISPLAY 4 columns to the user (i.e. nothing to do with your Table Key), then see here How do I bind a ComboBox so the displaymember is concat of 2 fields of source datatable?
cmb_name.DisplayMember = "name";
cmb_name.DataSource = dt;
DataRowView selectedRow = (cmb_name.SelectedItem as DataRowView );
The result will be here:
MessageBox.Show(selectedRow.Row[0].ToString());
MessageBox.Show(selectedRow.Row[1].ToString());
MessageBox.Show(selectedRow.Row[2].ToString());
MessageBox.Show(selectedRow.Row[3].ToString());
.....
If you want to get some data from a ComboBox in to a List you can use something like this
List<string> ListOfComboData = new List<string>();
ListOfComboData = yourComboBox.Items.OfType<string>().ToList<string>();
I have no real idea if this is what you mean as the question is very poorly structured. I hope this helps...
Edit: To put the selected text in to some TextBox use
yourTextBox.Text = youComboBox.Text;
in the SelectedIndexChanged event of your ComboBox.
You could follow the approach here with the following class:
public class ComboBoxItem
{
public string Text { get; set; }
public object[] PrimaryKey { get; set; }
}
private void Test()
{
ComboboxItem item = new ComboboxItem();
item.Text = "Item text1";
item.PrimaryKey = new object[] { primaryKey1, primaryKey2, primaryKey3, primaryKey4};
comboBox1.Items.Add(item);
comboBox1.SelectedIndex = 0;
MessageBox.Show((comboBox1.SelectedItem as ComboboxItem).Value.ToString());
}

Setting default item in combo box

I have a function for setting items in a combobox and one item is to be set by default like
--SELECT LIST--
public void SetOperationDropDown()
{
int? cbSelectedValue = null;
if(cmbOperations.Items.Count == 0)
{
//This is for adding four operations with value in operation dropdown
cmbOperations.Items.Insert(0, "PrimaryKeyTables");
cmbOperations.Items.Insert(1, "NonPrimaryKeyTables");
cmbOperations.Items.Insert(2, "ForeignKeyTables");
cmbOperations.Items.Insert(3, "NonForeignKeyTables");
cmbOperations.Items.Insert(4, "UPPERCASEDTables");
cmbOperations.Items.Insert(5, "lowercasedtables");
//ByDefault the selected text in the cmbOperations will be -SELECT OPERATIONS-.
cmbOperations.Text = "-SELECT OPERATIONS-";
}
else
{
if(!string.IsNullOrEmpty("cmbOperations.SelectedValue"))
{
cbSelectedValue = Convert.ToInt32(cmbOperations.SelectedValue);
}
}
//Load the combo box cmbOperations again
if(cbSelectedValue != null)
{
cmbOperations.SelectedValue = cbSelectedValue.ToString();
}
}
Can anyone suggest a way to do this?
I've rewritten this answer to clarify some stuff.
First, the "default" text must be added as combo item as well.
Usage of combo.Text property just adds descriptive text to combobox which is "lost" first time user do something with a control.
If you like to permanently have "default" text in your combo, you must add it as an combobox item.
By the code you provided, just modify the
cmbOperations.Text = "-SELECT OPERATIONS-"; to
cmbOperations.Items.Insert(0, "-SELECT OPERATIONS-");
Note that this way you add the item "-SELECT OPERANDS-" to the 0th (read first) position in the list.
Also make sure that all your following items are increased by 1, because they are now moved by one space down in list.
Finally, put cboOperations.SelectedIndex = 0; line at the end of code. By doing so, you're telling combobox to display your "default" item initially when the form (or control) loads.
One more thing. I'm not pretty sure what do you want to achieve with the code beyond setting combo items, but if you like to check what user selected use cboOperations.SelectedIndex property which contains currently selected item in combo. You can add simple if(cboOperations.SelectedIndex == someIntValue){...}
The rest is your program logic ;)

Categories