Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I need fill a ComboBox (Windows Forms) where have a Text and ID values. Example:
("Team1", 15)
("Team2", 27)
...
My code didnt work :/
List<Team> teams = new List<Team>();
teams = sq.loadTeams();
foreach (Team t in teams){
Combobox.Items.Add(t.getName(), t.getId());
}
heeelp please
Use data-binding, best feature Windows Forms come up with ;)
var list = new[]
{
new Team { Id = 1, Name = "One" },
new Team { Id = 2, Name = "Two" },
new Team { Id = 3, Name = "Three" }
};
combobox.ValueMember = "Id"; // Name of property to represent a Value
combobox.DisplayMember = "Name"; // Name of property to represent displayed text.
combobox.DataSource = list; // Bind all items to the control
Selections can be accessed by Selected.. properties of combobox.
var selectedTeamId = (int)combobox.SelectedValue;
var selectedTeamName = combobox.SelectedText;
var selectedTeam = (Team)combobox.SelectedItem;
Notice that SelectedValue and SelectedItem return object type, so you need cast it to correct type before using.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have the following : var navitems = new Button[5] .. Now how do I add to the array? I keep getting a null reference at runtime. To add, i am doing the following in a for loop :
for(int i=0;i<6;i++)
{
button b=new Button();
navitems[i]=b;
}
Note : No need to worry about how the buttons will be formatted, I already have that covered.
Fine here is the actual code, didn't wanna give too much away..
var groups = Connection.Groups();
var navitems = new TileNavItem[5];
for (int i=0;i > groups.Count; i++)
{
TileNavItem item = new TileNavItem()
{
Caption = groups[i].Description,
TileText = "Dashboards"
};
navitems[i] = item;
}
I'm using devexpress trial and i would like to create my tilenavpane items dynamically.. If i do the following navitems.Items.AddRange(new TileNavItem[] { item1, item2, item3 });, it works great so I figured I could easily implement this dynamically.
You should define your array the correct size for your items, and be more careful with your for-loops:
var groups = Connection.Groups();
var navitems = new TileNavItem[groups.Count];
for (int i=0; i < groups.Count; i++)
{
navitems[i] = new TileNavItem
{
Caption = groups[i].Description,
TileText = "Dashboards
};
}
Note that I also removed a superfluous variable.
This code may still fail if Connection.Groups can return null items.
You need increase array size
var navitems = new Button[6]
of decrease loop
for(int i=0;i<5;i++)
{
button b=new Button();
navitems[i]=b;
}
You can add TileNavItem(s) to the collection at run time after adding the initial 5 (not tested):
TileNavItem item6 = new TileNavItem() { TileText = "Item 6" };
navitems.Items.Add(item6);
https://documentation.devexpress.com/WindowsForms/18127/Controls-and-Libraries/Navigation-Controls/TileNav-Pane/How-to-Create-and-Customize-the-TileNavPane-Control-in-Code
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Firstly please forgive me as I am still trying to get to grips with C# and OOP.
I am trying to build a simple console shopping basket as part of a challenge and I have a number of products which I need to be able to pull on to populate 5 different scenarios of the basket.
However I am unsure as to the best approach to listing each of the products which each have three different values (Desc, Dept, Price) and I wish to be able to select the items I need through a array, possibly.
Currently I have the items listed as such:
itemOnePrice = 10.50m;
itemTwoPrice = 54.65m;
itemThreePrice = 03.50m;
itemOneDept = "Clothing";
itemTwoDept = "Clothing";
itemThreeDept = "Head Gear";
itemOneDesc = "Hat";
itemTwoDesc = "Jumper";
itemThreeDesc = "Head Light";
I have looked at Lists and at Tuple, but I haven't been able to figure out how to really make these work for me. Can somebody please explain the best approach to list these products to pull from to populate my basket contents.
First, create a class
class Item
{
public decimal Price {get;set;}
public string Department {get;set;}
public string Description {get;set;}
}
Second, create the list
List<Item> items = new List<Item>();
items.Add(new Item{Price = 10.5m, Department = "Clothing", Description = "Hat"});
items.Add(new Item{Price = 54.65m, Department = "Clothing", Description = "Jumper"});
items.Add(new Item{Price = 03.50m, Department = "Head Gear", Description = "Head Light"});
I have a combobox and I see that I am not able to set SelectedValue like this:
cmbA.SelectedValue = "asd"
So I tried to do this
cmbA.SelectedIndex = cmbA.FindString("asd");
Based on How to set selected value from Combobox?
I realised that my combobox is a System.Windows.Controls.ComboBox and not a System.Windows.Forms.ComboBox.
That means that FindString() is not available.
Based on User Control vs. Windows Form I get that forms are the container for controls, but I dont get why the Controls.ComboBox does not implement FindString().
Do I have to write my own code to do what FindString() does for Forms.ComboBox?
WPF ComboBoxes are not the same as WinForms ones. They can display a collection of objects, instead of just strings.
Lets say for example if I had
myComboBox.ItemsSource = new List<string> { "One", "Two", "Three" };
I could just use the following line of code to set the SelectedItem
myComboBox.SelectedItem = "Two";
We're not limited to just strings here. I could also say I want to bind my ComboBox to a List<MyCustomClass>, and I want to set the ComboBox.SelectedItem to a MyCustomClass object.
For example,
List<MyCustomClass> data = new List<MyCustomClass>
{
new MyCustomClass() { Id = 1, Name = "One" },
new MyCustomClass() { Id = 2, Name = "Two" },
new MyCustomClass() { Id = 3, Name = "Three" }
};
myComboBox.ItemsSource = data;
myComboBox.SelectedItem = data[0];
I could also tell WPF I want to consider the Id property on MyCustomClass to be the identifying property, and I want to set MyCombbox.SelectedValue = 2, and it will know to find the MyCustomClass object with the .Id property of 2, and set it as selected.
myComboBox.SelectedValuePath = "Id";
myComboBox.SelectedValue = 2;
I could even set the Display Text to use a different property using
myComboBox.DisplayMemberPath = "Name";
To summarize, WPF ComboBoxes work with more than just Strings, and because of the expanded capabilities, FindString is not needed. What you are most likely looking for is to set the SelectedItem to one of the objects that exist in your ItemsSource collection.
And if you're not using ItemsSource, then a standard for-each loop should work too
foreach(ComboBoxItem item in myComboBox.Items)
{
if (item.Content == valueToFind)
myComboBox.SelectedItem = item;
}
I don't know what you are trying to do but I think it would be easier to just do
cmbA.Text = "String";
That way you get your selected item
Else I found an intersting article that could help you out:
Difference between SelectedItem, SelectedValue and SelectedValuePath
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The first 3 lines of code works fine..
How can I do the same when using object initializer ?
// works
Customer MyCustomerx = new Customer();
MyCustomerx.Location[0].place = "New York";
MyCustomerx.Location[1].place = "France";
// problem here
List<Customer> MyCustomer = new List<Customer>
{
new Customer() { Name= "Me",Location[0].place = "New York" }
}
There's no equivalent of that code within object initializers - you can't specify indexers like that. It's slightly unusual that it works even directly... I'd expect to have to add to a Locations property, rather than there being two already available which I could set an unconventionally-named property on. For example, this would be idiomatic:
Customer customer = new Customer {
Name = "Me",
Locations = {
new Location("New York"),
new Location("France")
}
};
(I'd probably put the name into a constructor parameter, mind you.)
You could then use that within a collection initializer, of course.
If I have the following enum
enum RequestStatus
{
Open = 1,
InProgress = 4,
Review = 7,
Accepted = 11,
Rejected = 12,
Closed = 23
}
and we have the following List
List<RequestStatus> nextStatus = new List<RequestStatus>();
nextStatus.Add(RequestStatus.Review);
nextStatus.Add(RequestStatus.InProgress);
If we want to bind nextStatus to dropDownListwe do it as belllow
foreach (RequestStatus req in nextStatus)
dropDownList.Items.Add(new ListItem(req.ToString(), ((int)req).ToString()));
Is there any other best way to do this bind ?
Binding is setting a data source and bindings, not adding items to a list, what you are looking for is (assuming this is ASP.NET Forms here):
dropdownlist.DataSource = Enum.GetNames(typeof(RequestStatus))
dropdownlist.DataBind();
That being said, I think this is pretty poor user friendliness as I certainly would not care to see "InProgress" in a dropdown. I think it would be more appropriate to store this data with a ID/Name/Key and DisplayName combo somewhere and then bind the ID and DisplayName like so:
var items = new[] {{ID = "Review", DisplayName = "Review"}, {ID = "InProgress", DisplayName="In Progress"}};
dropdownlist.DataSource = items;
dropdownlist.DataValueField = "ID";
dropdownlist.DataTextField = "DislayName";
dropdownlist.DataBind();
To clarify I am not advocating to hardcode this list and would probably load this from DB and cache, but I really can only guess at your requirements here.