We have a WinForms application that is currently being internationalized. While some controls are straightforward to be i18n'ed, combo boxes with drop down items are giving us a hard time. Take a combo box with the following drop down items:
year
month
day
Currently these items are provided in the designer and the text values are persisted in the application database. I know it was not too bright idea to begin with but this where we are now.
I suspect I will need to go for a key/value type list and assign that as the data source to the combo box. The question is what would be the best practice to accomplish this task?
Thanks for the help in advance.
When you use combo box, we have two properties, one is "Text" and "Value". You have to set the text value from the localized resource file and Value field always from your code.
cbxJobTile.Item.Text ="Localized string from resource file";
cbxJobTile.Item.Value = "SoftwareEngineer";
Related
I am new to C# and I am trying to create a drop-down box with a label and value property. For example in HTML I can have <option value"user_name"> Username </option> , how do I get this done in WinForms using C#.
I understand how to create a combo box and add string collections to it using the WinForm designer, but how do I assign value to this strings? The value is quite important, as this is what is needed to interact with a different server.
Thanks.
Break up your problem
1) You said - "I understand how to create a combo box and add string collections to it using the WinForm designer"
2) You said - "but how do I assign value to this strings?"
Option 1, is not at all related to Option 2.
Option 1 - You are adding data to the combobox in design mode.
Option 2 - Which string you are talking about? If you have already assigned the data(collection of strings) to combobox then combobox will display collection of strings only. M i right?
You don't need Option 1, if you are implementing Option 2.
You need data to be displayed in your combobox which your user will select.
So, now where the data coming up? It is from Sql Server/XML/Text File/...? Or you are creating a List/Dictionary in your code and assigning to the combobox?
So finally, you can use any one approach...either Option 1 - "add data
to your combobox in design mode" Or Option 2 - "add data to your
combobox by retrieving from a datasource(Sql Server/XML/Text
File/List/Dictionary)".
How to do that, refer the links mentioned in your comments.
Both the case/s, data will be available inside combobox and user can interact with the data inside combobox.
I have a DataGridView with several TextBoxColumns and one ComboBox column called 'combo' that holds the client type. The problem is that I'd like to show both the currently selected client-type value along with the dropdown client-type list to validate future changes by the user. In SQL Server, I have a DB with two table columns, 'client_type_dropdown.name' and 'clients.client_type'. The 'client_type_dropdown.name' column is a validation list. The 'clients.client_type' column contains the current client type for clients in the database. Is there a way to show in 'combo' both 'client_type_dropdown.name' and 'clients.client_type' , i.e., one source for the ComboBoxColumn dropdown and a different source for the textbox part of 'combo'? Or do I need to have two columns in my grid?
I appreciate your help.
I'm using a third party grid, but I usually handle this by setting the combo drop down style to DropDown instead of DropDownList. This will allow your original database value to display, even if it isn't in the list.
This also allows free typing of values into the combo field, so the trick after that is to validate the user input to make sure it matches a value in the list before you allow them to save updated values. You could play around with the LimitToList property of the combo to possibly save you doing the validation manually, but with most controls I have worked with it will give you more grief than help.
I have written a GUI application that contains a DataGridView in which users can add new instances of an Arrow class by creating a new line in the view and filling in the new Arrow's properties. One of those properties is called transferType and is a string.
I allow users to set a list of valid transferTypes in a settings form. I am using Properties.Settings to save the settings, using the convent tool built into Visual Studio to create and manage application settings.
Rather than having the user enter the transferType field by hand and have the DataGridView reject the entry if it does not match any of the valid transferTypess, I have been trying to set up the column of the DataGridView to use drop down menus populated with the valid options. To do this, using Visual Studios GUI Design tool(which was used to build the GUI) I edited the column and changed the "Column Type" from DataGridViewTextBoxColumn to DataGridViewComboBoxColumn. That change allows me to select a DataSource to populate the selections of the combo box, so I went and under "Other data sources" attempt to select "Properties" which stores my settings (Properties.Settings.Default), but for some reason Visual Studio won't allow me to select it.
I then tried set the DataSource of the combo box in code after the initialization of the GUI, using the line" ((DataGridViewComboBoxColumn)arrowView.Columns[3]).DataSource = Properties.Settings.Default.validTransferTypes; (Transfer Type is the fourth column), but when I ran my program I get this error when I click on the combo box and try to "drop it down":
The following exception occured in the DataGridVIew:
System.ArgumentException: DataGridViewComboBoxCell value is not valid.
To replace this default dialog please handle to DataError event.
The error loops and reappears immediately after hitting OK or exiting the window.
I'm assuming that there has to be a reasonable way to use my settings to populate a combo box, but can't figure out how. I also don't understand why Visual Studio won't allow me to create a DataSource using Properties. Any help would be appreciated.
(This is also my first posted question, so be gentle with the criticism please :) )
UPDATE:
It turns out that using ((DataGridViewComboBoxColumn)arrowView.Columns[3]).DataSource = Properties.Settings.Default.validTransferTypes; does correctly populate the combo box, but any time the combo box is moused over, the previously mentioned error comes up.
If you are using Properties.Settings.validTransferTypes as a string then the value of that combobox would only be a single string value. Do you have more values in your settings that you'd like to populate the combobox with? It seems that the error is coming because you have nothing else for the combobox to read other that the single string you are loading in. Trying to load an array with Properties.Settings.Default may be an option and then setting the datasource equal to that array.
I think I've stumbled upon a bug in the .NET (or the native windows API) concerning combo boxes.
I'll give a specific scenario:
I have two database tables, with a one to many relation ship between the two, a brief description is as follows:
Table: FinSage
Column Type
SageCode nvarchar(10) (PK)
Desc nvarchar(100)
Table: FinCode
Column Type
FinCode nvarchar(10) (PK)
Desc nvarchar(100)
SageCode nvarchar(10) (FK)
These are entirely user maintained, with only the PK obviously being constrained to be unique. When creating or editing a record on the FinCode table I have a combobox on the screen which has the display member bound to the 'Desc' column and the value member bound to SageCode.
The problem arises when there are duplicate entries in the 'Desc' column, which is quite common under the expected use of the system. It seems the .net combo boxes does a reverse lookup using the display value to get the value member. So when the combo box gains focus it will change the value to the first occurrence of the display member.
I understand that in theory, if there are duplicates in the display value then the combo box effectively has no use as the user doesn't know which value they have selected. However as we know users are not perfect and so they will and do enter the same descriptive value for a code.
I currently get around this by combining the code and the description, however my boss wants them to be able to edit this value by using either the code or the description (as some uses prefer to use one or the other). I tried using two combo boxes bound to the same binding source but saw this problem arise.
Apologies for the verbose description, its not really a problem that can be summed up in code.
On a slightly related note, the combo boxes that Access use don't have this bug. But I believe Access is comparable to WPF in that it draws all its own UI components rather than using the Win32 API.
Is there not a way of 'forcing' a native combo box to find the correct item in the list based on the associated value or was the concept of value-description pair developed after Microsoft designed their native combo box?
Thank you for any insights
Marlon
*edit*
The answer below prompted me to quickly dev up a test application and I'm not getting the same behaviour unless the combobox is inside a datagridview (which is unrelated to the original question).
Sorry Microsoft, I would delete this question but I cant now, ah well, mistakes are good.
The solution is set DropDownList in the DropDownStyle property of the designer
ComboBoxes and ListBoxes do have value-description pairs in the form value(index)-Description(text).
Therefore if the combobox includes to of the same description they are still unique as they don't have the same index.
I'd say rather than there being a bug in .net there's a bug in your code.
I have a noob question.
I have a WinForm (.net 2.0 in VS 2008) on my form I have a DataGrid. One of the fields in that DataGrid is a combo box that is bound to a separate table. This works, but I am unable to edit or add. I cannot add a value that is not on the list.
I want to create a lookup box (for lack of a better term).
The form is for Parts Order Entry. In the DataGrid Data Source is PartsOrder_table, the Work order Field is an int in the PartsOrder_table, and the combo box is bound to the WorkOrder_table. The WorkOPRder_Table has WorkOrd_ID int and WrkOrd nvarchar(10)
The Combo Box Data Source is WorkOrd_Table Display Member is WorkOrd Value Member is WorkOrder_ID.
This works great with the problem that I cannot add or select anything not in WorkOrder_table.
Any help would be greatly appreciated.
Seems you cannot in a straight forward manner. This thread shows a way of doing this, by adding the needed values during the Validation of the ComboBox control. See the URL for more details.
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/31a28e99-6f9f-4cce-b256-9b7cf1ddf69a/