I am reading a setting from an XML document, converting it to a string array and then looping through each string and adding them to a DropDownList.
Everything appears to be working fine, until I actually go and look at the DropDownList itself. No matter what I do the DropDownList is empty even though when I am debugging through my code everything appears to be adding itself perfectly.
If anyone could shed a little light on why nothing is displaying despite the fact from the code's point of view it is being populated, I would appreciate it.
My code can be found below (Please note I have also tried populating it via Data Binding but I am still having the same issue.):
public class InstrumentDropDownList : DropDownList
{
public InstrumentDropDownList()
{
PopulateDropDown();
}
public void PopulateDropDown()
{
string unsplitList = Fabric.SettingsProvider.ReadSetting<string>("Setting.Location");
string[] instrumentList = unsplitList.Split(',');
DropDownList instrumentsDropDown = new DropDownList();
if (instrumentList.Length > 0)
{
foreach (string instrument in instrumentList)
{
instrumentsDropDown.Items.Add(instrument);
}
}
}
}
You're creating a new DropDownList and adding items to it. The problem is, your not doing anything with the new DropDownList you create. You are just adding the items to the wrong list.
public void PopulateDropDown()
{
string unsplitList = Fabric.SettingsProvider.ReadSetting<string>("Setting.Location");
string[] instrumentList = unsplitList.Split(',');
if (instrumentList.Length > 0)
{
foreach (string instrument in instrumentList)
{
this.Items.Add(instrument);
}
}
}
As an alternative you should be able to do this as well. You would obviously want to put in some more validation, but this is just to show that you can use the DataSource/DataBind
public void PopulateDropDown()
{
this.DataSource = fabric.SettingsProvider.ReadSetting<string>("Setting.Location").Split(',');
this.DataBind();
}
Why are you creating a new instance of the DropDownList when you are inheriting from the same class. Shouldn't you be doing something like. base.Items.Add() ??
You need to call instrumentsDropDown.DataBind after the foreach statement..
public class InstrumentDropDownList : DropDownList
{
public InstrumentDropDownList()
{
PopulateDropDown();
}
public void PopulateDropDown()
{
string unsplitList = Fabric.SettingsProvider.ReadSetting<string>("Setting.Location");
string[] instrumentList = unsplitList.Split(',');
DropDownList instrumentsDropDown = new DropDownList();
if (instrumentList.Length > 0)
{
foreach (string instrument in instrumentList)
{
instrumentsDropDown.Items.Add(instrument);
}
instrumentsDropDown.DataBind();
}
}
}
Related
So I have a parent class called SalesRep and a child class called SeniorSalesRep. I have got it so it displays both classes polymorphically to a listbox. The issue I'm having is that I have a combo box that gives the user the choice of displaying a report of the objects in the SalesRep class which should also display the SeniorSalesRep objects and if it's selected just SeniorSalesRep it only shows SeniorSales Rep objects. However, when I'm implementing this it creates an extra object called object which displays only SalesRepObjects and not SeniorSalesRep objects. How can I get rid of that extra object in my combo box?
public void LoadTypeComboBox()
{
List<string> salesRepTypes = new List<string>();
foreach (SalesRep thisSalesRep in allSalesReps)
{
string s = thisSalesRep.GetType().Name;
string baseType = thisSalesRep.GetType().BaseType.Name;
if (!salesRepTypes.Contains(s))
{
salesRepTypes.Add(s);
}
if (!salesRepTypes.Contains(baseType))
{
salesRepTypes.Add(baseType);
}
}
cboObjectType.DataSource = salesRepTypes;
}
private void cboObjectType_SelectedIndexChanged(object sender, EventArgs e)
{
lstSalesReps.DataSource = null;
lstSalesReps.Items.Clear();
foreach (var i in allSalesReps)
{
if (i.GetType().Name == cboObjectType.SelectedItem.ToString())
{
lstSalesReps.Items.Add(i);
}
else if (i.GetType().BaseType.Name == cboObjectType.SelectedItem.ToString())
{
lstSalesReps.Items.Add(i);
}
}
}
Form Output
The code that shows object in combobox is this:
string baseType = thisSalesRep.GetType().BaseType.Name;
The base type of SalesRep is object. You should either remove object from your collection or never add in the first place.
GetType gives you the runtime type of the object. So I don't think you need .BaseType. Try this:
public void LoadTypeComboBox()
{
List<string> salesRepTypes = new List<string>();
foreach (SalesRep thisSalesRep in allSalesReps)
{
string type = thisSalesRep.GetType().Name;
if (!salesRepTypes.Contains(s))
{
salesRepTypes.Add(s);
}
}
cboObjectType.DataSource = salesRepTypes;
}
Or even better with Linq:
cboObjectType.DataSource = allSalesReps
.Select(r => r.GetType().Name)
.Distinct()
.ToList();
So, it's been 4 hours since I started struggling here.
See, I've got this comboBox and it's bound to a List<>, - loads up like it's supposed to; but here I've also got a textBox which is supposed to contain text for filter criteria of the List<>. Goes nice, packs all the filtered items into a new list, the comboBox displays it... But when I choose to pick an item from it, i.e. a comboBox.Item, it returns the items from the first list. Yes, the first list, displaying the values from the filtered list; those values are class objects I'm packing into a dataGridView later on.
Here's the TextChanged:
private void textBox4_TextChanged_1(object sender, EventArgs e)
{
IEnumerable<artikal> filtered =
from artikal in art
where artikal.naziv.ToUpper().Contains(textBox4.Text.ToUpper()) || artikal.plu.Contains(textBox4.Text) || artikal.barkod.Contains(textBox4.Text)
select artikal;
comboBox1.DataSource = null;
comboBox1.Items.Clear();
List<artikal> filter = filtered.ToList<artikal>();
comboBox1.DataSource = filter;
and here's the class, I mean, if it's this important, but I'm not convinced it is:
public class artikal
{
public string plu { get; set; }
public string naziv { get; set; }
public string kolicina { get; set; }
public string nabavnaCena { get; set; }
public string prodajnaCnea { get; set; }
public string barkod { get; set; }
public override string ToString()
{
return plu + " " + naziv;
}
}
This art list is a global list defined above all else in the world. Here is how I populate the gridview:
public partial class NabavkaFrm : Form
{
#region some stuff lying here
List<item> art = new List<item>();
// other code
row.Cells[0].Value = art[comboBox1.SelectedIndex].plu;
row.Cells[1].Value = art[comboBox1.SelectedIndex].naziv;
}
So, yeah, any suggestions? And good time o' the day to everyone passing by :D
As I mentioned in the comment, the issue is not in the code where you filter the items. It cannot be. The issue is right here:
row.Cells[0].Value = art[comboBox1.SelectedIndex].plu;
row.Cells[1].Value = art[comboBox1.SelectedIndex].naziv;
and because art is the class level field, if the item from the combobox is 0, it will always show item at index 0 in art. You do not want this. You want to show item at index 0 from your filtered list but that list keeps changing. Sometimes item at index 0 is one thing, whilst another thing another time.
Do it like this instead:
var selectedItem = (comboBox1.SelectedValue as item);
row.Cells[0].Value = selectedItem.plu;
I'm stuck at a problem with MvvmCross.
I don't know how it's suppose to be handled.
The situation:
I have a object called
MyTempClass
It looks like this:
public class MyTempClass
{
public string ImageName { get; set; }
public bool IsTheCorrectAnswer { get; set; }
public bool HasBeenClicked { get; set; }
}
I have a list of MyTempClass.
From this list I iterate and create a view with buttons.
For each button I can successfully pass the object I'm iterating over in a command like this:
for (var i = 0; i < vmExercises.Count; i++) //This is the list...
{
//Create btn & add to the view...
var currentExercise = vmExercises[i];
set.Bind(btn).WithClearBindingKey(currentExercise.ImageName).To(vm => vm.TestCommand).CommandParameter(currentExercise).Apply();
}
The method that TestCommand uses looks like this:
public IMvxCommand TestCommand => new MvxCommand<MyTempClass>(ATestMethod);
private void ATestMethod(MyTempClass obj)
{
obj.HasBeenClicked = true;
CurrentTempClass = obj;
}
The problem
I also want to bind the visibility for the button in this loop.
I've tried like this:
set.Bind(btn).WithClearBindingKey(currentExercise.ImageName).For("Visible").To(vm => vm.ShouldBeVisible).Apply();
ShouldBeVisible looks like this:
public bool ShouldBeVisible
{
get
{
if (CurrentTempClass.IsTheCorrectAnswer && CurrentTempClass.HasBeenClicked)
{
return false;
}
return true;
}
}
The problem is:
If one should be hidden, all of the other buttons gets hidden.
What am I doing wrong? Has anyone else done this?
I'm so thankful for any help! :)
for visibility, you should use the MvvmCross Visibility plugin: https://www.mvvmcross.com/documentation/plugins/visibility
I would also recommend:
Removing logic form your getter and setters.
Call SetProperty() on theVisibility.Set for the Binding to work if the property gets updated.
I have a ChaseSelection class which i use in casting my dropdown list objects,
now i am trying to put the the values from the database as a default value in the drop down list, but it does not seem to work, can anyone help? I dont even think my loop runs. Here is my chaseselection class, and also put in the loop below: Thanks
public class ChaseSelectionItems
{
public string code { get; set; }
public string text { get; set; }
public ChaseSelectionItems(string code, string text)
{
this.code = code;
this.text = text;
}
public override string ToString()
{
return this.text;
}
}
foreach (ChaseSelectionItems items in drpdwnChaseSecSelection.Items)
{
if (items.code == _Row.xcs_View)
{
drpdwnChaseSecSelection.SelectedValue = items.text;
}
}
It is not entirely clear how you configured the listbox but most likely you did not configure ValueMember correctly. The following might fix that:
foreach (ChaseSelectionItems items in drpdwnChaseSecSelection.Items)
{
if (items.code == _Row.xcs_View)
{
// drpdwnChaseSecSelection.SelectedValue = items.text;
drpdwnChaseSecSelection.SelectedItem = items;
}
}
I am abit new in C# and i am trying to insert an object to a CheckedListBox,
so this inserted item will have a title inside the checked list (my object contains a string field inside it which I want to be displayed in the CheckedListBox).
for example this is my class:
public class InfoLayer
{
private string LayerName;
private List<GeoInfo> GeoInfos;
public InfoLayer()
{
LayerName = "New Empty Layer";
GeoInfos = new List<GeoInfo>();
}
public InfoLayer(string LayerName)
{
this.LayerName = LayerName;
GeoInfos = new List<GeoInfo>();
}
public InfoLayer(string LayerName,List<GeoInfo> GeoInfosToClone):this(LayerName)
{
foreach (GeoInfo item in GeoInfosToClone)
{
GeoInfos.Add((GeoInfo)((ICloneable)item).Clone());
}
}
public GeoInfo SearchElement(long id)
{
foreach (GeoInfo info in GeoInfos) // foreach loop running on the list
{
if (info.INFOID == id)
return info; // return the item if we found it
}
return null;
}
public GeoInfo SearchElement(string name)
{
foreach (GeoInfo info in GeoInfos)
{
if (info.INFONAME.CompareTo(name)==0)
return info;
}
return null;
}
public override string ToString()
{
string toReturn = "";
for (int i = 0; i < GeoInfos.Count; i++) // for loop running on the list
{
toReturn += String.Format("{0}\n",GeoInfos[i].ToString()); // piping another geoinfo
}
return toReturn;
}
public string LAYERNAME{get{return LayerName;}}
my class also contains a tostring overrider inside her (not what i want to display)
thanks in advance for your help.
Override ToString() in your class, the class that the object is an instance of.
Edit:
You don't want to display the contents of ToString(). You want to display the LayerName, don't you? Perhaps you should display the values with Databinding instead. Then you can set DisplayMember to your new LAYERNAME property.
I believe this is what you are trying to achieve:
checkedListBox1.Items.Add(yourObject.stringField);
((MyObjectType)checkedListBox1.Items(index)).Name = "whatever"
You will have to know the index of the object you want to change.
You'll just have to override the ToString method in your class so that it returns this Name property value.
public overrides string ToString() {
return Name;
}
It will then display its name when added to your CheckedListbox.