[英]Unable to cast object of type 'System.string' to type '....Event'
I'm making a personal calendar/planner thing and I want to be able to click on an even in the list box and have it fill out the title and time forms.我正在制作个人日历/计划表,我希望能够单击列表框中的偶数并让它填写标题和时间表格。 I've been searching through every forum post on similar issues but they usually just give more errors.
我一直在搜索关于类似问题的每个论坛帖子,但他们通常只会给出更多错误。 Currently, when I click on the event in the list box, the program exits and just gives an Unhandled Exception error saying
System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'ProjectName.Event'.'
目前,当我单击列表框中的事件时,程序退出并只给出一个未处理的异常错误,提示
System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'ProjectName.Event'.'
This is the code that throws the error:这是引发错误的代码:
private void LstEvents_SelectedIndexChanged(object sender, EventArgs e)
{
btnDelete.Enabled = true; // When something is selected, enable delete button
Event eventtest = (Event)lstEvents.SelectedItem; // <--- This line throws error
txtTitle.Text = eventtest.eventTitle.ToString();
}
And this is my Event class:这是我的事件类:
public class Event
{
int month, day, year, startHour, startMin, endHour, endMin;
private DateTime startTime, endTime;
public string eventTitle;
public Event(int cMonth, int cDay, int cYear, int cStartHour, int cStartMin, int cEndHour, int cEndMin, string cEventTitle)
{
month = cMonth;
day = cDay;
year = cYear;
startHour = cStartHour;
startMin = cStartMin;
endHour = cEndHour;
endMin = cEndMin;
eventTitle = cEventTitle;
string tempDate = year.ToString() + "-" + month.ToString() + "-" + day.ToString() + " " + startHour.ToString() + ":" + startMin.ToString();
startTime = DateTime.Parse(tempDate); // Convert startHour to DateTime
tempDate = year.ToString() + "-" + month.ToString() + "-" + day.ToString() + " " + endHour.ToString() + ":" + endMin.ToString();
endTime = DateTime.Parse(tempDate); // Do the same thing to endTime
}
public DateTime whenStartTime
{
get { return startTime; }
}
public DateTime whenEndTime
{
get { return endTime; }
}
public override string ToString()
{
return startTime.Year + "-" + startTime.Month + "-" + startTime.Day + " " + startTime.Hour + ":" + startTime.Minute;
}
}
Populating the list box:填充列表框:
private void BtnAdd_Click(object sender, EventArgs e)
{
Event newEvent;
newEvent = new Event(monthCalendar.SelectionStart.Month, monthCalendar.SelectionStart.Day, monthCalendar.SelectionStart.Year,
Convert.ToInt32(cboStartHour.SelectedItem), Convert.ToInt32(cboStartMinute.SelectedItem),
Convert.ToInt32(cboStopHour.SelectedItem), Convert.ToInt32(cboStopMinute.SelectedItem),
txtDescription.Text.ToString());
form.lstEvents.Items.Add(newEvent.ToString());
}
lstEvents.SelectedItem is a String; lstEvents.SelectedItem 是一个字符串; you want an Event.
你想要一个事件。 How you go from a String to an Event is going to depend on what that String is, and what class lstEvents is (and what other properties that class has).
您如何从字符串到事件将取决于该字符串是什么,以及 lstEvents 是什么类(以及该类具有的其他属性)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.