1

Task List

I have made a simple TO-DOs program that gets input from a text box then place it in another text box. With tick boxes next to it, this is all fine except i Cannot save the list eg. the item and if it's finished or not. Please could anyone help me be able to save this list of items.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TO_DOs
{
    public partial class Form1 : Form
    {
        private bool text1, text2, text3, text4, text5, text6, text7, text8;

        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (text1 == false)
            {
                textBox2.Text = textBox1.Text;
            }
            else if (text2 == false)
            {
                textBox3.Text = textBox1.Text;
            }
            else if (text3 == false)
            {
                textBox4.Text = textBox1.Text;
            }
            else if (text4 == false)
            {
                textBox5.Text = textBox1.Text;
            }
            else if (text5 == false)
            {
                textBox6.Text = textBox1.Text;
            }
            else if (text6 == false)
            {
                textBox7.Text = textBox1.Text;
            }
            else if (text7 == false)
            {
                textBox8.Text = textBox1.Text;
            }
            else if (text8 == false)
            {
                textBox9.Text = textBox1.Text;
            }


        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            text1 = true;

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            text2 = true;
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            text3 = true;
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            text4 = true;
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            text5 = true;
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {
            text6 = true;

        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {
            text7 = true;
        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {
            text8 = true;
        }
    }
}
Bilbo
  • 56
  • 1
  • 9
  • where do you want to save the list? and which list? I don't see any – Mong Zhu Mar 14 '17 at 07:23
  • You might want to consider studying how to use arrays, lists, collections, etc then go from there since based on your code there is no variable to store your to do items. – jegtugado Mar 14 '17 at 07:26
  • When i run the program it will ask for the item then i can tick it, and it all works fine but if i close the program and open it again it will ask for the input all again. So I would like to be able to have it save what i have done when i close the program Eg. Save To A File Somewhere: {1} Shopping - True. Which would mean the program could then reload it back as shopping with a tick for finished . Thanks – Bilbo Mar 14 '17 at 15:48

5 Answers5

1

I would do it like this:

Create a class to store your values in:

public class ListEntry
{
    public string Text { get; set; }

    public bool Finished { get; set; }
}

Then I would create 2 Methods:

public List<ListEntry> UI_To_List(); //Create UI from your saved file

public void List_To_UI(List<ListEntry> entries); //Process your UI 

Now it's your choice on how to store your list.

You could store it as JSON or XML.

A few recommendations:

  1. I would create a UserControl for your TextBox + CheckBox
  2. Display the 'List of UserControls' in a FlowLayoutPanel
  3. => then you can process the FlowLayoutPanel.Controls List.
  4. This will make your List dynamically size to an 'unlimited' amount of items.

Short example:

  1. Create a UserControl (Rightclick project for that): enter image description here
  2. Add these 2 methods to the code of your UserControl (F7 / rightclick => View Code):

    public void SetText(string text)
    {
        //Set the Text of your TextBox in the UserControl:
        textBox1.Text = text;
    }
    
    public void SetFinished(bool finished)
    {
        //Set the Checked of your CheckBox in the UserControl:
        checkBox1.Checked = finished;
    }
    
  3. In your MainForm add an FlowLayoutPanel (from ToolBox).

  4. Add your Data like this (using class from above):

    /// <summary>
    /// 
    /// </summary>
    /// <param name="entries">You will get them from loading your previously saved file</param>
    public void CreateUI(List<ListEntry> entries)
    {
        foreach (ListEntry entry in entries)
        {
            //Create new instance of your UserControl
            TaskView view = new TaskView();
            view.SetFinished(entry.IsFinished);
            view.SetText(entry.Text);
    
            //Add that to your UI:
            this.flowLayoutPanel1.Controls.Add(view);
        }
    }
    

The result will look like this:

enter image description here

Community
  • 1
  • 1
Felix D.
  • 4,811
  • 8
  • 38
  • 72
  • Sorry But i am quite new to c# and don't know how i would do this.Pls could u explain exactly how to to do this. Thank you so mich guys! – Bilbo Mar 15 '17 at 07:13
  • How do i make a user control? – Bilbo Mar 15 '17 at 07:16
  • I will work out an example. Usercontrol can be made by right-clickin your project => Add => UserControl – Felix D. Mar 15 '17 at 07:24
  • @FelixD. Maybe provide him a documentation source so he can study it with more details. – Cataklysim Mar 15 '17 at 09:33
  • @Cataklysim this is only a short mockup. The mainquestions was about saving and I linked two helpful posts for saving objects or xml or json. I guess he will be fine with that. – Felix D. Mar 15 '17 at 10:23
  • SO If i do this will it save when closed the n reopen with the same data in it? – Bilbo Mar 15 '17 at 15:49
  • @Bilbo you will need to implement some saving/storing mechanism but yes it will do. Save on `MainWindow_Closing` and load on `MainWindow_Shown` – Felix D. Mar 17 '17 at 06:40
0

I'm not sure what exactly it is that you want to save in a list... but here's just a tip when checking conditions, instead of using if (text1 == false), simply do if (!text1) as this means "is not true" because by default if (text1) will return true.

private void button1_Click(object sender, EventArgs e)
{
    if (!text1)
    {
        textBox2.Text = textBox1.Text;
    }
    else if (!text2)
    {
       textBox3.Text = textBox1.Text;
    }

   // Left out the rest of the else ifs
}
Christopher Lake
  • 358
  • 4
  • 10
0

You are casting textboxes wrong. For example when you change textBox4, you gave text3 true.

    private void textBox4_TextChanged(object sender, EventArgs e)
    {
        text3 = true;
    }

Then you cast

TextBox4.Text = TextBox1.Text;

It changes TextBox4.Text to TextBox1.Text.

You probably want to save TextBox4.Text here at TextBox1.Text so you sould change all if blocks like that. So you have to give only one "true" function for changed textBox sign and change if blocks

if(text(boolNum))

 TextBox1.Text = TextBox(Number).Text;

Just swap them and try like that.

If you want to save another thing by another way. You have to be more spesific.

ReadyFreddy
  • 898
  • 8
  • 20
  • Textbox 1 is the input box the 2 - 9 are the boxes after it.So i don't need to know if textbox one has chnaged so i just go straight to textbox two. – Bilbo Mar 15 '17 at 06:37
  • Now i understand your problem. You want to save status of controls. I don't know if there is an easy way for it but you can use a textfile to save status of controls. If you want to solve it like that, i can edit my answer and try to tell how to do this or at least logic of this. – ReadyFreddy Mar 15 '17 at 06:55
  • Yes Thats What I meant So It should save that textbox2 has Task1 written in and it's ticked. Thank you – Bilbo Mar 15 '17 at 06:59
  • If i fill it out then tick a few of the boxes and close it that will all go away, i want it to keep the data that it had before when opened again. – Bilbo Mar 15 '17 at 07:02
0

You can use a CheckedListbox to hold all tot actions. You can then tick the itemsand for instance in the OK button you include a save action:

 foreach(var item in MyCheckedListbox.CheckedItems)
    {
    Console,WriteLine(item.Text);
    }
RudolfJan
  • 91
  • 1
  • 11
0

Lets see the answer from Felix D. He tells you exactly how to create a class and save the items into it. But now you only have a List that will be available as long as your software is running. You still need to save it somewhere on your desktop.

Lucky for you, you got a really simple pattern. string; boolean

So how about you make it yourself simple? Just create a textfile and write your entries, as example in a csv marked with a ; for every information?

Example:

class Program
{
 public class tmpClass
    {
        public string Text;
        public bool tick;
    }

    public List<tmpClass> tmpList = new List<tmpClass>();

    static void Main(string[] args)
    {
           //Stuff         
    }

    public void WriteToFile()
    {
        string tmpTextFilePath = @"C:\User\Desktop\SaveText.txt";

        using (StreamWriter tmpWriter = new StreamWriter(tmpTextFilePath))
        {
            string tmpTextToWrite = String.Empty;
            for (int i = 0; i < tmpList.Count; i++)
            {
                tmpClass tmpEntry = tmpList[i];
                tmpTextToWrite += tmpEntry.Text + ";" + tmpEntry.tick;
            }
            tmpWriter.WriteLine(tmpTextToWrite);
        }
        //Now we wrote a text file to you desktop with all Informations
    }

    public void ReadFromFile()
    {
        string tmpTextFilePath = @"C:\User\Desktop\SaveText.txt";
        using (StreamReader tmpReader = new StreamReader(tmpTextFilePath))
        {
            string tmpText = tmpReader.ReadLine();
            string tmpInput = String.Empty;
            tmpClass tmpClass = new tmpClass();
            int i = 0;
            foreach (char item in tmpText)
            {
                if(item.Equals(";".ToCharArray()))
                {
                    if (i == 0)
                    {
                        tmpClass.Text = tmpInput;
                        i = 1;
                        tmpInput = String.Empty;
                    }
                    else
                    {
                        if (tmpInput == "True")
                            tmpClass.tick = true;
                        else
                            tmpClass.tick = false;
                        i = 0;
                        tmpInput = String.Empty;
                        tmpList.Add(tmpClass);
                    }
                }
                tmpInput += item;
            }
        }
    }
 }

This should simply write a txt File to your desktop with your information and read one and save it to your list.

Cataklysim
  • 637
  • 6
  • 21
  • Saving to txt is not best practice ! All that string operations are also quite inefficent. As I provided in my anser json or xml is the best way to go here. Also the OP should do a little self research to maximize learing. Just copy-paste won't help for long. – Felix D. Mar 15 '17 at 10:27