C# - 将数据添加到List <class>中的List <class>

时间:2016-03-25 13:28:01

标签: c# list listbox touch mouseevent

我的程序使用全局钩子来捕获和记录鼠标点击。按下一个开始按钮,开始记录这些点击以及录制的日期和时间。按下停止按钮时,也会记录停止时间和日期。

以下显示我的List<RecordingData>,其中我捕获了我需要的数据。我遇到的问题是让List<TouchData> touchPoints填充鼠标点击次数和时间。 (鼠标点击和touchPoints可能会令人困惑,但我希望最终能在触摸屏上显示这一点。)

public class RecordingData
{
    public string _recStartDate { get; set; }
    public string _startRecTime { get; set; }
    public string _recStopDate { get; set; }
    public string _stopRecTime { get; set; }
    public List<TouchData> touchPoints { get; set; }

    public override string ToString()
    {
        return "Recording: " + 
            _recStartDate + " " 
            + _startRecTime + " - " 
            + _recStopDate + " " 
            + _stopRecTime;
    }
} 

public class TouchData
{
    public int _touchX { get; set; }
    public int _touchY { get; set; }
    public string _xyTouchTime { get; set; }

    public override string ToString()
    {
        return "x: " +  _touchX + " y: " + 
            _touchY + " Time: " + _xyTouchTime;
    }
}

在我的表单中,我有OnMouseDown事件来记录鼠标单击数据并将其输入到我的触摸列表中。此外,当Stop_Click被触发时,它会存储并记录所有recordingData

List<RecordingData> recordingData = new List<RecordingData>();
List<TouchData> touchData = new List<TouchData>();

private void OnMouseDown(object sender, MouseEventArgs e)
    {
        xyTouchTime = DateTime.Now.ToString("HH:mm:ss tt");

        Log(string.Format("X: {0} \t Y: {1} \t Date: {2} \t Time: {3} \n {4}", 
            e.X.ToString(), 
            e.Y.ToString(), 
            DateTime.Now.ToString("dd/MM/yyyy"),
            xyTouchTime,
            Environment.NewLine));

        touchX = e.X;
        touchY = e.Y;

        // add these values to our touch List<>
        touchData.Add(new TouchData
            {
                _touchX = touchX,
                _touchY = touchY,
                _xyTouchTime = xyTouchTime
            });
    }

private void Stop_Click(object sender, EventArgs e)
    {
        recStopDate = DateTime.Now.ToString("dd/MM/yy");
        recStopTime = DateTime.Now.ToString("HH:mm:ss tt");
        Unsubscribe();
        Show();
        this.WindowState = FormWindowState.Normal;
        this.ShowInTaskbar = true;
        notifyIcon.Visible = false;

        //  populate List<T> with our data
        recordingData.Add(new RecordingData
        {
            _recStartDate = recStartDate, 
            _startRecTime = recStartTime,
            _recStopDate = recStopDate,
            _stopRecTime = recStopTime
        });

        //  update our listbox
        BindData();
    }

    void BindData()
    {
        listBox1.DataSource = null;
        listBox1.DataSource = recordingData;
        listBox1.SelectedIndex = 0;
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = listBox1.SelectedIndex;

        if (listBox1.SelectedItem == null)
        {
            return;
        }
        else
        {
            RecordingData recData = (RecordingData)listBox1.SelectedItem;

            textBoxDebug.Text = "Start Date: " + recData._recStartDate + Environment.NewLine
            + "Stop Date: " + recData._recStopDate + Environment.NewLine
            + "Start Time " + recData._startRecTime + Environment.NewLine
            + "Stop Time " + recData._stopRecTime + Environment.NewLine
            + String.Join(Environment.NewLine, recData.touchPoints + Environment.NewLine);
        }
    }

简而言之,我有一个录音列表,每个录音都有自己的嵌套点击列表。我的最终目标是在listBox中显示这些内容,但是,我是新手,并且目前在使这个工作时遇到一些困难。建设性的批评是值得赞赏的。

0 个答案:

没有答案