add two value to combobox - c#

In c# we can add a text and a value to each items of combobox, I want to know is it possible in Java also? if not please advice.

I Solved my problem by create a class like
public class ItemInfo {
public String Name;
public String Value;
public ItemInfo(String Name , String Value) {
this.Name = Name;
this.Value = Value;
}
public String toString() {
return Name ;
}
public String getValue() {
return Value ;
}
}
than I just create a new object from this class & pass it to my combobox
combbox1.addItem(new ItemInfo(item[0],item[1]));
job done :)!

You should learn how to use ComboBox in java.

Related

Using a Class as ValueMember in ComboBox

I have a ComboBox, which currently uses a simple class, containing Name and ID. Name is used as DisplayMember whereas ID is used as ValueMember. However, I would actually like to pass both the Name and the ID, when selecting an item, since this would spare me the operation of looking up the name later. Of course I could store those seperately, but that seems rendundat, since they come from the same place.
Hence arises my question: Is it possible to use the class (from which I get the Name and ID) as ValueMember for the ComboBox?
I was thinking something like this:
cboCategory.DataSource = viewModel.categoryOptions; // Type: BindingList<Equipment>
cboCategory.DisplayMember = "Name";
cboCategory.ValueMember = ??? // <--- This is where I run out of ideas
My Equipment class looks like this:
public class Equipment
{
private int id;
private string name;
public Equipment (int id, string name)
{
this.id = id;
this.name = name;
}
public int Id
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
You can access selected instance with SelectedItem property of combobox.
Only you need is cast to Eqipment type before using because SelectedItem is of type object.
var selectedEquipment = (Equipment)combobox.SelectedItem;
You can use data-binding as well to keep your viewmodel "loosely coupled"
cboCategory.DataSource = viewModel.categoryOptions;
cboCategory.DisplayMember = "Name";
cboCategory.ValueMember = "Id";
cboCategory.DataBinding.Add("SelectedItem", viewModel, "SelectedEquipment", true);
With data-binding viewmodel.SelectedEquipment property will be updated when you change selected item in combobox.
There's no way how you can achieve this with pure C# without adding third property where you combine Name and ID.
You can consider that 3rd property security like:
Is it enough to have only get?
Is it enough to have it protected?
etc.
When you're using XAML or WinForms, there's MultiBinding mechanism to achieve similar behavior. IMHO, multi-binding is in most cases overhead and it is more beneficial to create 3rd property.
So your class would look like:
public class Equipment
{
private int id;
private string name;
public Equipment (int id, string name)
{
this.id = id;
this.name = name;
}
public int Id
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Identifier
{
get { return Id.ToString() + " " + Name; }
}
}
You can extent you ViewModel with INotifyPropertyChanged and notify about Identifier change when Name or ID changes.
More sophisticated (if needed) will be returning array of objects instead of string so you wont lose data at conversion (ID.ToString()) <- require more memory.

Change the xtragrid detailsView type programmatically devepxress

I have this code
private string _nom;
private string _reference;
private BindingList<Contact> _listContact = null;
public string Nom
{
get { return _nom; }
set { _nom = value; }
}
public string Reference
{
get { return _reference; }
set { _reference = value; }
}
public BindingList<Contact> ListContact
{
get { return _listContact; }
set { _listContact = value; }
}
And Ihave a function that return BindingList
In my XtraForm I have this
listPartenaire = Partenaires.getPartenairesObjectUsingLinq();
dgv.DataSource = listPartenaire;
Well with that, I have an xtraGrid filled by Partenaire in the MasterView, and with the Contact in the DetailsView
What I'm looking for is to change the type of the detailsview to CardView or something else with c#
any help please
I believe the following help article should be helpful: Specify Views to Represent Detail Data
Please check the example:
How to: Replace a View Used to Represent a Specific Master-Detail Relationship

forcing a variable to hold certain values only

I am using vs 2012. I have a simple string property
string _someString;
public string MyString
{
get
{
return _someString;
}
}
I want this property to hold only certain values. So that when the client uses this property only those certain values can be used.
It sounds like what you really want is an enum:
public enum MyValues //TODO rename all the things
{
SomeValue,
SomeOtherValue,
FinalValue,
}
Then your property can be:
private MyValues value;
public MyValues MyValue
{
get { return value; }
}
If you need to get a string representation of that value just call ToString on the enum value:
string stringValue = value.ToString();
Use an enum as in :
enum MyEnum
{
AllowableValue#1,
AllowableValue#2,
...
}
public MyEnum myEnum { get; set; }
Then populate some UI element with only the values of the enum.
I suppose you want to have some validation on the setter then:
public string MyString
{
get
{
return _someString;
}
set
{
if (value == "a" || value == "b" /* ... */)
_someString = value;
else
throw new InvalidArgumentException("Invalid value!");
}
}
Make sure to set it via the property, not the actual member variable.

Automatic Property Values and Defaults [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you give a C# Auto-Property a default value?
I have a property in a class like so
public String fontWeight { get; set; }
I want it's default to be of "Normal"
Is there a way to do this in "automatic" style rather than the following
public String fontWeight {
get { return fontWeight; }
set { if (value!=null) { fontWeight = value; } else { fontWeight = "Normal"; } }
}
Yes you can.
If your looking for something like:
[DefaultValue("Normal")]
public String FontWeight
{
get;
set;
}
Do a google search for 'Aspect Oriented Programming using .NET'
..if this is overkill for you do this:
private string fontWeight;
public String FontWeight {
get
{
return fontWeight ?? "Normal";
}
set {fontWeight = value;}
}
No, an automatic property is just a plain getter and/or setter and a backing variable. If you want to put any kind of logic in the property, you have to use the regular property syntax.
You can use the ?? operator to make it a bit shorter, though:
private string _fontWeight;
public String FontWeight {
get { return _fontWeight; }
set { _fontWeight = value ?? "Normal"; }
}
Note that the setter is not used to initialise the property, so if you don't set the value in the constructor (or assign a value in the variable declaration), the default value is still null. You could make the check in the getter instead to get around this:
private string _fontWeight;
public String FontWeight {
get { return _fontWeight ?? "Normal"; }
set { _fontWeight = value; }
}
You will need to use a backing field.
private string fontWeight;
public String FontWeight
{
get { String.IsNullOrEmpty(fontWeight) ? "Normal" : fontWeight;}
set {fontWeight = String.IsNullOrEmpty(value) ? "Normal" : value;}
}
You either need to use a backing field and initialize that to your default value
private String fontWeight = "Normal";
public String FontWeight
{
get { return fontWeight; }
set { fontWeight = value; }
}
or, keep the auto property and call the setter in your constructor
public constructor()
{
FontWeight = "Normal";
}
public String FontWeight { get; set; }
One way to do it is using PostSharp as detailed in this answer to a similar question.
You could use the DefaultValue attribute:
[DefaultValue("Normal")]
public string FontWeight { get; set; }
Although it notes that
A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.
so you could use this in conjunction with initialisation in the constructor or via a backing field and default handling.
You'd need either a variable like so:
string fontWeight;
public string FontWeight
{
get
{
if (string.IsNullOrEmpty(fontWeight))
fontWeight = "Normal";
return fontWeight;
}
set { fontWeight = value; }
}
Or use a Constructer to set an initial value:
class FontClass
{
public string FontWeight { get; set; }
public FontClass()
{
FontWeight = "Normal";
}
}

GridView DataBinding

I have a problem with a GridView(I'm using Telerick but i think the .NET GridView is similar for this situation).
I have a List that contains some user define object with some properties that will be displayed in a GridView. This list is loaded from SQL.
My problem is that i have an int property that i want to be parsed and displayed in GridView with some strings.
public class Vehicles
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string vehName;
public string VehName
{
get { return vehName; }
set { vehName = value; }
}
private int gpsInterval;
public int GpsInterval
{
get { return gpsInterval; }
set { gpsInterval = value; }
}
private int isStolen;
public int IsStolen
{
get { return isStolen; }
set { isStolen = value; }
}
...
}
...
List<Vehicles> vehs = DBveh.getAllVehicles();
GridViewUnitsList.DataSource = vehs;
Is stolen is curently displayed as an int in the GridView. So is there a method to parse "isStolen" value and replace it with somenting like "YES"/"NO" without using a foreach and iterating throw the hole GridView after the binding?
There are 2 easy options:
1) Add a property to your object and reference that property in the DataGrid:
public string IsStolenStr
{
get { return isStolen == 1? "Yes" : "No"; }
}
2) Or add the logic to a <asp:template> column in the DataGrid:
<%# Eval("IsStolen") == 1 ? "Yes" : "No" %>
I would modify the SQL statement so that it returned the Yes/No string based on the isStolen value.

Categories

Resources