i'm trying to add data to a combo box, i'm adding an ID and a name, with both appearing on the same line. I'm using ~ to seperate the names and IDs. However I can't figure out how to put these values into a combobox without also adding the ~
try
{
StreamReader sr = new StreamReader("nameForSkiTimes.txt");
string line = sr.ReadLine();
while (line != null)
{
addSkiTimesPupilCB.Items.Add(line);
line = sr.ReadLine();
}
}
catch (Exception ex)
{
MessageBox.Show("Error" ex.Message);
}
I don't really know how to do much in c#, please help.
CodePudding user response:
As your data are delimited with a "~", the easiest way to separate them is using the Split method. Split return an array of strings with the elements (in your case, the ID and the name)
try
{
StreamReader sr = new StreamReader("nameForSkiTimes.txt");
string line = sr.ReadLine();
string[] data;
string label;
while (line != null)
{
data = line.Split("~"); // split on "~" char
if (data.Length > 1) // check if we have at least two elements
{
label = $"{data[0]} {data[1]}"; // Access your ID and your name with index, format as you wish
addSkiTimesPupilCB.Items.Add(label);
}
line = sr.ReadLine();
}
}
catch (Exception ex)
{
MessageBox.Show("Error" ex.Message);
}
CodePudding user response:
If you like it short:
In the function, you simply replace this special character with nothing.
try
{
StreamReader sr = new StreamReader("nameForSkiTimes.txt");
string line = sr.ReadLine();
while (line != null)
{
addSkiTimesPupilCB.Items.Add(line.Replace("~", ""));
line = sr.ReadLine();
}
}
catch (Exception ex)
{
MessageBox.Show("Error" ex.Message);
}
CodePudding user response:
Unless this is to learn working with files and streams a better solution is to use a json file and deserialize with Newtonsoft.Json or System.Text.Json.
Sample json, in this case named People.json residing in the same folder as the app.
[
{
"Identifier": 1,
"Name": "Alfreds Futterkiste"
},
{
"Identifier": 2,
"Name": "Die Wandernde Kuh"
},
{
"Identifier": 3,
"Name": "Chop-suey Chinese"
},
{
"Identifier": 4,
"Name": "Antonio Moreno Taquería"
},
{
"Identifier": 5,
"Name": "Antonio Moreno Taquerí"
}
]
Class for json
public class Person
{
public int Identifier { get; set; }
public string Name { get; set; }
public override string ToString() => Name;
}
Using Newtonsoft.Json to deserialize json to a list of Person and assign the ComboBox. A Button to get the currently selected Person in the ComboBox.
public partial class JsonToComboBoxForm : Form
{
public JsonToComboBoxForm()
{
InitializeComponent();
Shown = OnShown;
}
private void OnShown(object sender, EventArgs e)
{
var fileName =
Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
"People.json");
var json = File.ReadAllText(fileName);
PeopleComboBox.DataSource =
JsonConvert.DeserializeObject<List<Person>>(json);
}
private void CurrentButton_Click(object sender, EventArgs e)
{
Person current = (Person)PeopleComboBox.SelectedItem;
MessageBox.Show($@"{current.Identifier, -5}{current.Name}");
}
}
Or to display both Identifier and Name
public class Person
{
public int Identifier { get; set; }
public string Name { get; set; }
public override string ToString() => $"{Identifier} - {Name}";
}