Im trying to make a simple windows form that records the date based on a click on a month calendar, the distance driven and time taken based on text box inputs.

Im ok with arrays in console mode but no idea when it comes to forms/button click events.

this code works on a single button click and will print to a text box using the print method below. What I don't know how to do is make it so that every time i click the save button it increases the index and stores the next record without wiping the 1st record

I know it will need the index i to be declared somewhere and a counter increase used but have no idea where to start

private void btnDate_Click(object sender, EventArgs e)
        {            
              if (IsDataValid()) // error checking 
              {       
                    date[i] = txtDate.Text;
                    distance[i] = decimal.Parse(txtDistance.Text.ToString());
                    time[i] = int.Parse(txtDistance.Text.ToString());
             }            
       }

print method

public void PrintDetails()
        {
            for (int i = 0; i < MAXSIZE; i++)
            {
                textBox1.Text = date[i] + "  Distance : " + distance[i] +
                    " Time: " + time[i] + " \n";
            }
        }
private void btnPrint_Click(object sender, EventArgs e)
        {
            PrintDetails();
        }

any pointers in the right direction would be awesome

Recommended Answers

All 5 Replies

Add a new class named RecordInfo to your project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace daniweb
{
  public class RecordInfo
  {
    public DateTime DriveDate { get; set; }
    public decimal Distance { get; set; }
    public int Time { get; set; }
  }
}

Then the form code:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmInfo : Form
  {
    private List<RecordInfo> lst;

    public frmInfo()
    {
      InitializeComponent();
      lst = new List<RecordInfo>();
    }

    private void buttonSave_Click(object sender, EventArgs e)
    {
      if (!IsDataValid())
        return;

      RecordInfo rec = new RecordInfo();
      rec.Distance = decimal.Parse(txtDistance.Text);
      rec.DriveDate = DateTime.Parse(txtDate.Text);
      rec.Time = int.Parse(txtTime.Text);
      lst.Add(rec);
    }

    private void buttonPrint_Click(object sender, EventArgs e)
    {
      foreach (RecordInfo rec in lst)
      {
        Console.WriteLine(
          string.Format("Distance: {0:F2}, Date {1:g}, Time {2:F2}",
          rec.Distance,
          rec.DriveDate,
          rec.Time));
      }
    }

    private bool IsDataValid()
    {
      //Do your checking
      return true;
    }
  }
}

Make a class with a date a time and a distance as fields.
Add some methods to it to act on these data.
As you don't seem to know in advance how many dates etc. you have it is better to use a List than an Array.
You can define it like this:
List<MyClass> myClassVar= new List<MyClass>();
Use ypour own names for MyClass and myClassVar.

Oops scott.:-O You came before me, but as always with a better explanation. You should perhaps also post in the code snippets, I have seen many exellent little code snippets of you that would fit the qualification;:)

I don't know how I would title this post under code snippets :X

Any advice?

Well... Right, the problem here is a bit hazy and cannot be easily classified under one well defined subject. But I should certainly keep the possibility in mind for the future:idea:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.