0

I write a program, it is necessary to switch the current status in it, as well as it is necessary to plan it when you plan an event, it is perceived as an object, the object has its own fields, such as the start time and end time of the event, I want this object to be output when generated sheet boxing. Tell me how can this be done?

List<ChangeStatus> events = new List<ChangeStatus>();

private void toPlanButton_Click(object sender, EventArgs e)
{
    string comboBoxTypeNumber = comboBoxType.SelectedItem.ToString();

    DateTime Time = new DateTime();
    Time = dateTimePicker1.Value;
    DateTime longTime = new DateTime();
    longTime = dateTimePicker2.Value;

    ChangeStatus statusEvent = new ChangeStatus();
    statusEvent.StartEvent = Time;
    statusEvent.LongEvent = longTime;
    statusEvent.TypeEvent = comboBoxTypeNumber;
    events.Add(statusEvent);

    TimeComparer tc = new TimeComparer();
    events.Sort(tc);
}

How to display an object in listbox? It is necessary to display a list of objects, because in the future I want to make editing objects

listBoxEvent.Items.Add("type: " + statusEvent.TypeEvent + ";" + " start: " + statusEvent.StartEvent + ";" + " long: " + statusEvent.LongEvent + " min;"); - work
Rufus L
  • 36,127
  • 5
  • 30
  • 43
neji
  • 3
  • 3
  • There's no purpose in assigning variables to one thing and then another on consecutive lines. You can just do `DateTime time = dateTimePicker1.Value;`. And since these variables are only being used to assign a property of the `statusEvent`, they really aren't needed at all - you can just do `statusEvent.StartEvent = dateTimePicker1.Value;` – Rufus L Mar 15 '19 at 13:56
  • @neji: If you want the objects to appear in a specific format in the list box, you can override `ToString` in your class. If you want a specific property of an object to be what is shown in the listbox, then you can hand that property name to the list box and tell it to display that property – Flydog57 Mar 15 '19 at 13:59
  • Cannot use a local variable before declaring it. give an example of working code – neji Mar 15 '19 at 14:03
  • I agree, it works, thank you, the main question is relevant – neji Mar 15 '19 at 14:07
  • The question is not clear to me. Are you asking how to add an object to a listbox? Or how to customize the display of an object in a listbox? Or how to add a list of items to a listbox? Or how to databind a list to a listbox so it automatically reflects the list contents even when the list changes? – Rufus L Mar 15 '19 at 14:28
  • how to add output to the listbox where each line is an object field – neji Mar 15 '19 at 14:37

1 Answers1

0

You can use System.Linq Linq to get the string text and can call the AddRange() method on Items collection like

List<string> listData = events.Select(x => "type: " + x.TypeEvent + ";" + " start: " + x.StartEvent + ";" + " long: " + x.LongEvent + " min;").ToList();

listBoxEvent.DataSource = listData;
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • argument1: cannot convert from changestatus to listbox.object =( – neji Mar 15 '19 at 13:58
  • @neji my bad .. didn't interpreted the question properly. See edit if helps – Rahul Mar 15 '19 at 14:04
  • conversion error again, argument1: cannot convert from string to objectcollection, if i use the listBoxEvent.Items.Add(listData); method then the output happens but it is not correct – neji Mar 15 '19 at 14:13
  • @neji give it a try now. see edit again – Rahul Mar 15 '19 at 14:18
  • almost succeeded, now the output to the listbox is one word - (collection), but there must be a line, something is wrong, Any more ideas? – neji Mar 15 '19 at 14:36
  • @neji should be fine now. – Rahul Mar 15 '19 at 14:39
  • happened! My happiness has no limits! thank you very much! The most interesting thing I have tried many times in this way, but without a list list! – neji Mar 15 '19 at 14:41