I have a combo box on a C# Winform that I would like to populate with the string name variables from this list, nothing else. Here is the list code.
class Animals
{
public string averageMass { get; set; }
public string lifeSpan { get; set; }
public string whereToFind { get; set; }
public string name { get; set; }
public string animalImage { get; set; }
}
class Mammals:Animals
{
public static List<Mammals> MammalList = new List<Mammals>();
public string hairColour { get; set; }
}
You can do this on Combobox:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem == "Mammals") //You can also do index e.g. comboBox1.SelectedIndex == 0
{
comboBox2.DataSource = mammalList;
}
else
{
comboBox2.DataSource = reptileList;
}
}
Or you can also do this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.DataSource = fncGetSpecies(comboBox1.SelectedIndex);
}
private string[] fncGetSpecies(int intIndex)
{
//This will return if selected item is 0 which is Mammals or 1 if the selected item is Reptiles.
return intIndex == 0 ? mammalList : reptileList;
}
You can set DataSource of ListBox2 based on selection from ListBox1's within SelectedIndexChanged event handler as follow:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listBox1.SelectedIndex==0)//Which is Mammals list
{
listBox2.DataSource = reptileList;
}
else//Which is Reptiles list
{
listBox2.DataSource = mammalList;
}
}
You can add string items to combobox using following code
combobox.Items.Add(stringItem);
Related
I have a database with some devices in it and I want to be able to dynamically search in my listbox, when typing in my textbox.
I tried some other suggestions on StackOverflow:
Dynamic Search Result C#
So far I have this:
List<Device> devices = new List<Device>();
private void UpdateBindings()
{
deviceFoundListbox.DataSource = devices;
deviceFoundListbox.DisplayMember = "FullInfo";
}
private void searchTextbox_TextChanged(object sender, EventArgs e)
{
DataAccess database = new DataAccess();
devices = database.GetDevice(searchTextbox.Text);
lock(lockObject)
{
lastChange = DateTime.Now;
textChanged = true;
}
}
private void dynamicSearchTimer_Tick(object sender, EventArgs e)
{
lock(lockObject)
{
if(textChanged && lastChange > DateTime.Now.AddSeconds(-2))
{
UpdateBindings();
textChanged = false;
lastChange = DateTime.Now;
}
}
}
However, I can't seem to get it to work.
Any help's appreciated!
EDIT: The Device class:
internal class Device
{
public string DeviceName { get; set; }
public string SerialNumber { get; set; }
public string LoanStatus { get; set; }
public string Initials { get; set; }
public string FullInfo
{
get
{
return $"Device: {DeviceName} S/N: {SerialNumber} Loaner Status: {LoanStatus} Initials: {Initials}";
}
}
}
Result has been assigned to "device" and you are updating "devices" to DataSource?
How to assign :
device = database.GetDevice(searchTextbox.Text);
What return database.GetDevice();
If you have a device declaration when you add the devices list? or are you add device to devices?
You can use basically use devices.Add(database.GetDevice());
I am trying to display an item from a CSV file onto listbox(this part works) and then display individual parts of that item in separate labels.
public partial class InventoryForm : Form
{
public InventoryForm()
{
InitializeComponent();
}
public List<ItemsList> itemsLists(string csvPath)
{
var query = from l in File.ReadAllLines(csvPath)
let data = l.Split('|')
select new ItemsList
{
Name = data[0],
Type = data[1],
DMGTyp = data[2],
DMG = data[3],
Weight = int.Parse(data[4]),
Price = int.Parse(data[5]),
Description = data[5]
};
return query.ToList();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog filePath = new OpenFileDialog();
filePath.ShowDialog();
textBox1.Text = filePath.FileName;
}
private void btnLoad_Click(object sender, EventArgs e)
{
listBox1.DataSource = itemsLists(textBox1.Text);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//to do show individual pieces in labels
}
public class ItemsList
{
public string Name { get; set; }
public string Type { get; set; }
public string DMGTyp { get; set; }
public string DMG { get; set; }
public int Weight { get; set; }
public int Price { get; set; }
public string Description { get; set; }
}
}
The items are broken up into 6 parts and the list box only shows the name of the item but I want the label to show the rest of the item's properties. Any clues on how to do that?
I was trying to create a personal list using WinForms. I try to create a new entry via button click. I have a list of objects with string properties Name and Number.
How can I show the list of objects in my ListBox?
namespace sometestname
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Personallistshow(object sender, EventArgs e)
{
}
public void NewItemButton_Click(object sender, EventArgs e)
{
Personallist.Add(new Entrys() { Name = NameBox.Text, Number = Numbox.Text });
}
public List<Entrys> Personallist= new List<Entrys>();
}
public partial class Entrys
{
public string Name { get; set; }
public string Number { get; set; }
}
}
The user has 2 Textboxfields. If they click the NewItemButton, than create a new Entrys object. This new Entrys object should be added to Personallist object and ListBox should show the updated list.
List<Entrys> someList = ...;
Personallist.DataSource = someList;
You should use a bindinglist and set the datasourcebinding after initializing your form.
Something like:
public partial class Form1 : Form
{
public BindingList<Entrys> Personallist = new BindingList<Entrys>();
public Form1()
{
InitializeComponent();
comboBox1.DataSource = Personallist;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Number";
}
private void button1_Click(object sender, EventArgs e)
{
Personallist.Add(new Entrys() { Name = "TESTNAME", Number = "TESTNR" });
}
}
public partial class Entrys
{
public string Name { get; set; }
public string Number { get; set; }
}
I'm having a problem understanding what code I can use bring back selected rows from data grid view to the text boxes to edit. I think we will use something like dataScreen.SelectedRows something maybe
What code can I use?
DataSource = datagridview (to make it easier to understand)
namespace HospitalManagementSystem
{
public partial class Form1 : Form
{
DataTable table = new DataTable();
public string name { get; set; }
public string Gender { get; set; }
public string DateOfBirth { get; set; }
public string Address { get; set; }
public string MedicalHistory { get; set; }
public string BloodType { get; set; }
public Form1()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
dataScreen.Rows.Add("Iqra", "Female", 20, 721797684, "praha 6", "Migraine", "Blood Type O+");
}
private void radioButton_Male_CheckedChanged(object sender, EventArgs e)
{
Gender = "Male";
}
private void radioButton_Female_CheckedChanged(object sender, EventArgs e)
{
Gender = "Female";
}
private void btnSave_Click(object sender, EventArgs e)
{
name = txtName.Text;
DateOfBirth = Date_dob.Value.ToString();
Address = txtAddress.Text;
MedicalHistory = txtMedicalHistory.Text;
BloodType = txtBloodType.Text;
if (radioButton_Female.Checked)
Gender = "Female";
else if (radioButton_Male.Checked)
Gender = "Male";
dataScreen.Rows.Add(name, Gender, DateOfBirth, Address, MedicalHistory);
}
}
}
It looks like you replace the DataSource of your dataScreen with the value of your table when you click on save.
dataScreen.DataSource = table;
That is what's replacing the value of dataScreen.DataSource.
Depending on what you are trying to do you might want to add that row to the table before you update your dataScreen.DataSource.
I have some label that should display actual amount of items that contain BindingList that bound to the DataGridView.
I tried to bind in this way:
CountOfLoadedItemsLabel.DataBindings.Add("Text", _items.Count, String.Empty);
But when BindingList updates, the label that bound to its Count property not changes.
Never used BindingList<T> but this worked for me:
public partial class Form1 : Form
{
private BindingList<Test> list;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.list = new BindingList<Test>
{
new Test(1,"Entry"),
new Test(2,"Another Entry")
};
dataGridView1.DataSource = new BindingSource(list,null);
list.ListChanged += list_ListChanged;
list.Add(new Test(3, "After Binding"));
}
void list_ListChanged(object sender, ListChangedEventArgs e)
{
CountOfLoadedItemsLabel.Text = string.Format("Items: {0}", list.Count);
}
}
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
public Test(int id, string name)
{
this.Id = id;
this.Name = name;
}
}