简体   繁体   English

无法将“System.string”类型的对象转换为“....Event”类型

[英]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.

相关问题 无法将类型“ System.String”强制转换为类型“ System.Object” - Unable to cast the type 'System.String' to type 'System.Object' 无法将类型类的对象强制转换为“ System.String”类型 - Unable to cast object of type class to type 'System.String' 无法将“DapperRow”类型的对象转换为“System.String”类型 - Unable to cast object of type 'DapperRow' to type 'System.String' 无法将“AttachmentDetailsDto”类型的对象转换为“System.String”类型 - Unable to cast object of type 'AttachmentDetailsDto' to type 'System.String' 无法将类型为“ System.String”的对象转换为类型为“ FileItem”的对象 - Unable to cast object of type 'System.String' to type 'FileItem' 无法将“System.String”类型的对象转换为“System.Collections.Generic.List`1[System.String]”类型 - Unable to cast object of type 'System.String' to type 'System.Collections.Generic.List`1[System.String]' C# 中的 MS Word 自动化 - 无法将类型为“System.String[*]”的 object 转换为类型“System.String[]” - MS Word Automation in C# - Unable to cast object of type 'System.String[*]' to type 'System.String[]' 无法将对象类型“ System.String [*]”转换为类型“ System.String []” - Unable To Cast object type 'System.String[*]' to type 'System.String[]' 如何解决“无法在CallBimlScript中强制转换&#39;System.String&#39;类型的对象” - How to troubleshoot “Unable to cast object of type 'System.String'” in CallBimlScript 无法将类型为“ TextmlBatchDeleteDocument”的对象转换为“ System.String” - Unable to cast object of type 'TextmlBatchDeleteDocument' to 'System.String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM