1

I'm trying to create a to-do list program using Windows Forms with C# (I'm using Visual Studio 2010, if that's relevant), where the user can add a new task and set the title and its description, as well as deleting and editing the tasks.

I've got it working by putting everything into one form class and by putting in the same delete and edit code for each task's delete and edit button (I've set it so up to 5 tasks can be added), but it is very messy and I want to put all the code to set the task's title, description along with editing and deleting in a separate class and simply call each method where it's needed.

I've spent the last 2 weeks searching all over the internet trying to find something that will help, but I'm new to coding and a lot of the stuff i find isn't relative or i just don't understand. I've used form f1 = new form1(); and used f1.AddTask(); and I get no errors, but when I run the program and click on the buttons nothing happens.

I'm new to this site and not too sure what is needed, but I'm willing to do anything to get this to work. Any help would be immensely appreciated.

Dan J
  • 16,319
  • 7
  • 50
  • 82
Raker
  • 13
  • 1
  • 6

2 Answers2

0

see the following link Similar with samples to call methods from one to another, and/or setting values too. These were based on WinForms. If you are using WPF and doing Windows instead of forms, just change the context of whatever Form or Window.

Community
  • 1
  • 1
DRapp
  • 47,638
  • 12
  • 72
  • 142
0

Try to build an additional class where to put all the code that repeats itself.

public class CommonMethods() {

    public static void AddTask(Control c, Task t) {  /* code to add a task */ }
    public static void EditTask(Control c, Task t) {  /* code to edit a task */ }
    public static void DeleteTask(Control c, Task t) {  /* code to delete a task */ }

/*
etc... other methods here
*/
}

and then call those methods inside your form and pass to the method only the controls where u need to add/edit/remove tasks.

Zelter Ady
  • 6,266
  • 12
  • 48
  • 75
  • I've tried what you suggested and it gives me the error "Error 1 The type or namespace name 'Control' could not be found (are you missing a using directive or an assembly reference?" and the same for "Task", and they don't go blue like with yours. What does the "Control" and "Task" represent? – Raker Apr 12 '12 at 13:24
  • Add the missing reference. The code i gave you is only to indicate you a direction to go, not the real code for your case. I cannot give you a working code as long I don't have your full code. The point is to use and external class where to handle all those operations and to call the class methods from both forms. The solution is simple... try to think a bit and you'll solve the problem. – Zelter Ady Apr 12 '12 at 14:44
  • I think I understand, does the "control" and "Task" refer to the two forms e.g. "form1" and "form2"? Also I'm having trouble calling the method, I have tried CommonMethods.AddTask();, and it comes up with the error, "No overload method for "AddTask" with 0 arguments", but I'm not sure what I need to put into the brackets. I tried putting "ToDoList t, CommonMethods c", along with "ToDoList t = new ToDoList();" and "CommonMethods c = new CommonMethods();" outside the brackets or is it something along the lines of ((form1)this.CommonMethods.CommonMethods).AddTask(); P.S. Thanks for your help :) – Raker Apr 13 '12 at 14:33