我希望你能帮助一个程序员。 基本上,我希望将富文本框(taskNameRTB)中的用户输入分配给taskName; 我的类taskStructure字符串变量,如下所示:
public partial class Form1 : Form { public class taskStructure { public string taskName; public string taskDescription; public int Priority; public string dateAndTime; } public List<taskStructure> TasksArray = new List<taskStructure>(); //Declared a list data structure在我的第二种形式中,用户输入与任务相关的所有内容,我想在单击“创建任务”按钮后将此信息发送到列表:
private void createTaskBtn_Click(object sender, EventArgs e) { Form1 welcomeForm = new Form1(); welcomeForm.TasksArray[0].taskName = taskNameRTB.Text; welcomeForm.TasksArray[0].taskDescription = taskDescRTB.Text; }但是,当我这样做时,我得到一个ArgumentOutOfRangeException ,我不明白为什么。 我也试过这些:
welcomeForm.TasksArray[0].Add(taskDescRTB.Text); welcomeForm.TasksArray.Insert(0, taskNameRTB.Text); welcomeForm.TasksArray.Add(taskDescRTB.Text); taskNameRTB.Text = welcomeForm.TasksArray[0].taskName;但运行的那些会出现相同的错误ArgumentOutOfRangeException ,其中一些不起作用,例如:
welcomeForm.TasksArray[0].Add(taskDescRTB.Text);我知道该列表尚未初始化,但如果不允许我使用用户输入初始化它,如何初始化它...
你能解决的任何亮点都会非常有用
亲切的问候,
基兰
I hope you can help out a fellow programmer. Basically, I want the user input from the Rich Text Box (taskNameRTB) to be assigned to the taskName; string variable in my class taskStructure which is in form1 shown below:
public partial class Form1 : Form { public class taskStructure { public string taskName; public string taskDescription; public int Priority; public string dateAndTime; } public List<taskStructure> TasksArray = new List<taskStructure>(); //Declared a list data structureIn my second form which is where the user enters everything related to the task, I want to send this information to the list after the 'Create Task' button has been clicked:
private void createTaskBtn_Click(object sender, EventArgs e) { Form1 welcomeForm = new Form1(); welcomeForm.TasksArray[0].taskName = taskNameRTB.Text; welcomeForm.TasksArray[0].taskDescription = taskDescRTB.Text; }However, when I do this I get a ArgumentOutOfRangeException and I do not understand why. I have also tried these:
welcomeForm.TasksArray[0].Add(taskDescRTB.Text); welcomeForm.TasksArray.Insert(0, taskNameRTB.Text); welcomeForm.TasksArray.Add(taskDescRTB.Text); taskNameRTB.Text = welcomeForm.TasksArray[0].taskName;But the ones that run come up with the same error ArgumentOutOfRangeException and some of them don't work, such as:
welcomeForm.TasksArray[0].Add(taskDescRTB.Text);I'm aware that the list has not been initialized, but how can I initialize it when it doesn't allow me to initialize it with user input...
Any light you can shed on this will be really helpful
Kind Regards,
Kieran
最满意答案
您需要向列表中添加新的taskStructrue 。
welcomeForm.TasksArray.Add(new taskStructure { taskName = taskDescRTB.Text, taskDescription = taskDescRTB.Text });但就个人而言,我会重写该类以遵循命名约定并使用属性而不是公共字段。
public class TaskStructure { public string TaskName { get; set; } public string TaskDescription { get; set; } public int Priority { get; set; } public string DateAndTime { get; set; } }You need to add a new taskStructrue to the list.
welcomeForm.TasksArray.Add(new taskStructure { taskName = taskDescRTB.Text, taskDescription = taskDescRTB.Text });But personally I'd rewrite that class to follow naming conventions and to use properties instead of public fields.
public class TaskStructure { public string TaskName { get; set; } public string TaskDescription { get; set; } public int Priority { get; set; } public string DateAndTime { get; set; } }更多推荐
发布评论