Displaying Item from Listbox onto Textbox - c#

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?

Related

Dynamically search listbox using textbox in C#

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());

Serialize multiple object of class but list value are the same

I would like to serialize this class by creating multiple objects. the method works and the objects are saved in the xml file but the values of the List variable are always the same for all objects. how can i do to correct.?
[Serializable()]
public class TextureValues
{
[XmlAttribute("Texture_Info")]
public string Name { get; set; }
public string Date { get; set; }
public List<Point> Points { get; set; }
public int Pensize { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public TextureValues() { }
public TextureValues(string name, string date ,List<Point> points,int pensize,int width,int heigth)
{
this.Name = name;
this.Date = date;
this.Points = points;
this.Pensize = pensize;
this.Width = width;
this.Height = heigth;
}
}
Below the method I used in the form to enter object parameters:
public partial class TextureMaker : Form
{
List<TextureValues> textureInfo = new List<TextureValues>();
List<Point> p = new List<Point>();
public TextureMaker()
{
InitializeComponent();
}
private void panelText_Click(object sender, EventArgs e)
{
p.Add(panelText.PointToClient(Cursor.Position));
}
private void buttonSave_Click(object sender, EventArgs e)
{
textureInfo.AddRange(new TextureValues[]
{
new TextureValues(textBoxName.Text,DateTime.Now.ToString(),p,(int)numericPenSize.Value,(int)numTextWidth.Value,(int)numTextHeight.Value)
});
var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\textures.xml";
FileStream stream = File.Create(path);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<TextureValues>));
xmlSerializer.Serialize(stream, textureInfo);
stream.Close();
p.Clear();
}
}

How to bring back selected rows from datagridview to TextBox

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.

C# winform ComboBox

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);

DataGridView in winforms application && List of structs

I'm using a DataGridView control in my winforms application
private struct Ligne
{
public string Jour;
public string LaDate;
public int Heures;
public int Minutes;
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "fichier texte (*.txt)|*.txt";
if (openfile.ShowDialog() == DialogResult.OK)
{
textBox2.Text = openfile.FileName;
List<Ligne> str = ParsingFile(textBox2.Text);
dataGridView1.DataSource = str;
}
}
As you can see, i'm trying to bind a list of structs as a datasource of the grid, but i got empty fields even the listt isn't empty :
I need to know:
Why the grid is empty?
How can fix this error?
You must make the fields of your list Properties like this:
public string Jour; { get; set; }
public string LaDate; { get; set; }
public int Heures; { get; set; }
public int Minutes; { get; set; }
..or else their values can't be accessed.
I assume that the Columns get created OK.

Categories

Resources