C# 当颜色来自加载的对象时,颜色未正确设置为按钮

C# 当颜色来自加载的对象时,颜色未正确设置为按钮,c#,C#,我正在为我的班级做一个小的应用程序来记录所用的时间,为了排序,我想在项目上使用颜色。我的问题是项目正确保存颜色,但不加载它们。如果使用项目创建新任务,则将加载颜色-但如果使用项目创建新任务,然后保存并加载,则将每个按钮都变为默认灰色 从内置的Windows窗体颜色对话框中选择颜色,然后将所选颜色指定给项目对象,作为r、g、b,以便在发现用户希望保留在其他地方以供参考的颜色时打印输出,并作为颜色对象用于实际程序本身 我将发布整个方法,用于通过Pastebin确定将在UI中显示什么,因为这是一个相当

我正在为我的班级做一个小的应用程序来记录所用的时间,为了排序,我想在项目上使用颜色。我的问题是项目正确保存颜色,但不加载它们。如果使用项目创建新任务,则将加载颜色-但如果使用项目创建新任务,然后保存并加载,则将每个按钮都变为默认灰色

从内置的Windows窗体颜色对话框中选择颜色,然后将所选颜色指定给项目对象,作为r、g、b,以便在发现用户希望保留在其他地方以供参考的颜色时打印输出,并作为颜色对象用于实际程序本身

我将发布整个方法,用于通过Pastebin确定将在UI中显示什么,因为这是一个相当多的问题,这将成为一个很长的问题。下面是我认为可能存在的问题。

拯救

        saveFileDialog.Title = "Specify Destination Filename";
        saveFileDialog.Filter = "Extensible Markup Language Files (.xml)|*.xml|All Files (*.*)|*.*";
        saveFileDialog.FilterIndex = 1;
        saveFileDialog.OverwritePrompt = true;


        XmlSerializer serializer = new XmlSerializer(currentTimesheet.GetType());

        try
        {
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                StreamWriter file = new StreamWriter(saveFileDialog.FileName);
                serializer.Serialize(file, currentTimesheet);
                serializer.Serialize(Console.Out, currentTimesheet);
                file.Close();
            }
            else
            {
                logDetails("User closed Save Prompt");
            }
        }
        catch (ArgumentException ex)
        {
            logDetails(ex.ToString());

            MessageBox.Show("Empty filename is not legal.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
装载

        try
        {
            StreamReader file = new StreamReader(newTimesheet);
            XmlSerializer serializer = new XmlSerializer(currentTimesheet.GetType());

            currentTimesheet = (TimesheetObject)serializer.Deserialize(file);

            timesheetMenu.Text = currentTimesheet.name;
            timesheetLoaded = true;
            file.Close();
        }
        catch (ArgumentException argEx)
        {
            logDetails(argEx.ToString());
            MessageBox.Show("No timesheet was loaded! \nYou must select a file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            timesheetLoaded = false;
        }
        catch (XmlException xmlEx)
        {
            logDetails(xmlEx.ToString());
            MessageBox.Show("Something went wrong with your timesheet file. Ask for help perhaps?", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        selectProjectBox.Items.Clear();
        selectProjectBox.Items.Add("All Tasks");
        int index = selectProjectBox.FindString("All Tasks");
        selectProjectBox.SelectedIndex = index;
        for (int i = 0; i < currentTimesheet.projects.Count; i++)
        {
            selectProjectBox.Items.Add(currentTimesheet.projects.ElementAt(i).name);
        }
试试看
{
StreamReader文件=新的StreamReader(newTimesheet);
XmlSerializer serializer=新的XmlSerializer(currentTimesheet.GetType());
currentTimesheet=(TimesheetObject)序列化程序。反序列化(文件);
timesheetMenu.Text=currentTimesheet.name;
timesheetLoaded=真;
file.Close();
}
捕获(argumentx)
{
日志详细信息(argEx.ToString());
MessageBox.Show(“未加载时间表!\n您必须选择一个文件”,“错误”,MessageBoxButtons.OK,MessageBoxIcon.Error);
timesheetLoaded=假;
}
捕获(XmlException xmlEx)
{
日志详细信息(xmlEx.ToString());
Show(“您的时间表文件出现问题。可能需要帮助?”,“错误”,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
选择projectbox.Items.Clear();
选择projectbox.Items.Add(“所有任务”);
int index=selectProjectBox.FindString(“所有任务”);
selectProjectBox.SelectedIndex=索引;
对于(int i=0;i
显示用户界面

//creates list of task's project names, sets buttons accordingly 
for (int i = lowestIndex, k = 0; i < currentTimesheet.tasks.Count(); i++, k++)
{

    taskProjectNames[i] = currentTimesheet.tasks.ElementAt(i).projectName;

    //usingProject is set when a project is created, even with this removed there's an issue, but instead of being gray, they're transparent.         
    if (!usingProject)
    {
        projectColors[i] = Color.Gray;
    }
    else
    {
        try
        {
            projectColors[i] = currentTimesheet.projects[i].projectColor;
        }
        catch (ArgumentOutOfRangeException e)
        {
            //sends error to the log as well as printing it to the console
            logDetails(e.ToString());
        }
    }

    if (i > highestIndex)
    {
        //ends the loop if the index is higher than a page can show
        break;
    }

    taskProjectButtons[k].Text = taskProjectNames[i];
    taskProjectButtons[k].BackColor = projectColors[i];

}
//创建任务的项目名称列表,并相应地设置按钮
对于(int i=lowestinex,k=0;i高指数)
{
//如果索引高于页面可以显示的值,则结束循环
打破
}
taskProjectButtons[k].Text=taskProjectNames[i];
taskProjectButtons[k]。BackColor=ProjectColor[i];
}


如果这是一个糟糕的问题,我很抱歉,这真的很令人沮丧,我还没有解决它,我已经为此努力了一个半月。

我的问题是项目正确保存颜色,但不要加载它们。
你的帖子包含了许多不必要的逻辑。如何张贴一个最低限度的代码显示这一点?SaveColors和LoadColors方法就足够了。因为它似乎归结为
if(!usingProject)
语句,该语句似乎返回true,您需要对此进行研究。@Flak,如果我在
if(!usingProject)
语句中正确地阅读了您的注释,那么它似乎是false,但是当您删除它时,它是透明的(因此,报告了一个错误,你能显示报告的错误吗?)。你有多确定i索引在循环中是准确的(因为你似乎在很多事情上都使用它)。你确定语句
projectColors[i]=currentTimesheet.projects[i].projectColor
是正确的,正如我所说的,i变量似乎用于那里的几乎所有索引?你能显示生成的xml吗?@codemonkey我同意,但在前面的语句中被忽略了:)我通过添加第二个
for
循环并从原始循环中删除颜色设置,解决了这个问题。谢谢你的帮助。