将数据文本文件加载到多维数组中,然后将内容复制到文本框中 [英] Load a data text file into a multidimentional array then copie the content into textboxes

查看:111
本文介绍了将数据文本文件加载到多维数组中,然后将内容复制到文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的代码项目成员,
我是Visual C#的初学者,我希望您能解决这个问题,这对于像您这样的高级社区来说太简单了,但对我来说却很困难.

问题:
我有一个文本文件,其中包含14列和3500多行整数.我需要通过单击按钮来读取该文件,然后将全部内容加载到多维数组中.从该数组中将加载14个文本框.然后,我需要该内容每50毫秒更改一次.我将使用此应用程序创建一个套接字,通过该套接字将数据发送到控制其轨迹的机器人.

最好的问候

Dear code project members,
I''m a very beginer in Visual C# and I would like you to help me with this problem, which is too simple for a high level community like you but very difficult for me.

The problem:
I have a text file which contains 14 columns and more than 3500 rows of integers. I need to read that file by the click of a button and then load the whole contents into a multi dimensional array. From this array 14 textboxes will be loaded. Then I need that content to change every 50ms. I''ll use this application to create a socket over which I''ll send the data to a robot controlling a its trajectory.

Best regards

推荐答案

如果您需要有关创建,处理数组的信息: System.IO.File [^ ]
如果您不满意文件类: System.IO.TextReader [ ^ ]
如果需要通用计时器: System.Timers.Timer [ ^ ]
计时器类的替代方法有使用线程和使用thread.sleep来管理计时检出系统. Threading.Thread [^ ]在CodeProject中也有很多文章和问答.例如如何将ref参数传递给线程 [ ^ ].
祝你好运.
If you need info about creating, handling arrays: Array tutorial[^]
If you need info about general file reading stuff like ReadAllText ReadAllLines etc:System.IO.File[^]
If you arent happy with just File class: System.IO.TextReader[^]
If you want a general purpose timer:System.Timers.Timer[^]
There are alternatives to timer class like using threads and use thread.sleep to manage timing checkout System.Threading.Thread[^] also there are many articles and Q&A in CodeProject as well. For instance How to pass ref parameter to the thread[^].
Good luck.


使用系统;
使用System.IO;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;

命名空间WindowsFormsApplication1
{
公共局部类Form1:Form
{

#region私有变量

私有常量字符串_fileName = @"c:\ 1.txt";

私有列表< string> _lineItems = new List< string>();

私人StreamReader _sr;

私人字串[,] _coords;

私有Int32 _loopIndex = 0;

私人Int32 _rowCount = 0;

#endregion

公共Form1()
{
InitializeComponent();
}

私有void btnLoadFile_Click(对象发送者,EventArgs e)
{
光标= Cursors.WaitCursor;

ReadFile();

MessageBox.Show(完成导入数据!");

btnStart.Enabled = true;
btnLoadFile.Enabled = false;

Cursor = Cursors.Default;

}

私有void ReadFile()
{
字符串_newLine =";
_sr = new StreamReader(_fileName);

而(!_sr.EndOfStream)
{

_newLine = _sr.ReadLine();

如果(_newLine.Length> 1)
{
_lineItems.Add(_newLine);
}
}

_coords =新字符串[_lineItems.Count,14];

_rowCount = _lineItems.Count;

for(Int16 _i = 0; _i< _lineItems.Count; _i ++)
{

如果(_lineItems [_i] .ToString().Trim().长度> 1)
Split(_i,_lineItems [_i]);
}

}

私人void Split(Int16 _index,字符串_lineItem)
{
string [] _splitItems = _lineItem.Split(new char [1] {''``}});

for(Int16 _i = 0; _i< 14; _i ++)
{
_coords [_index,_i] = _splitItems [_i];
}


}

私有void btnStart_Click(对象发送者,EventArgs e)
{
tmrLoop.Enabled = true;
}

private void tmrLoop_Tick(对象发送者,EventArgs e)
{
LoopData();
}

私有void LoopData()
{

textBox1.Text = _coords [_loopIndex,0];
textBox2.Text = _coords [_loopIndex,1];
textBox3.Text = _coords [_loopIndex,2];
textBox4.Text = _coords [_loopIndex,3];
textBox5.Text = _coords [_loopIndex,4];
textBox6.Text = _coords [_loopIndex,5];
textBox7.Text = _coords [_loopIndex,6];
textBox8.Text = _coords [_loopIndex,7];
textBox9.Text = _coords [_loopIndex,8];
textBox10.Text = _coords [_loopIndex,9];
textBox11.Text = _coords [_loopIndex,10];
textBox12.Text = _coords [_loopIndex,11];
textBox13.Text = _coords [_loopIndex,12];
textBox14.Text = _coords [_loopIndex,13];

如果(_loopIndex == _rowCount -1)
{
_loopIndex = 0;
}
其他
{
_loopIndex ++;
}

}

}
}
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

#region Private Variables

private const string _fileName = @"c:\1.txt";

private List<string> _lineItems = new List<string>();

private StreamReader _sr;

private string[,] _coords;

private Int32 _loopIndex = 0;

private Int32 _rowCount = 0;

#endregion

public Form1()
{
InitializeComponent();
}

private void btnLoadFile_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;

ReadFile();

MessageBox.Show("Done importing data !");

btnStart.Enabled = true;
btnLoadFile.Enabled = false;

Cursor = Cursors.Default;

}

private void ReadFile()
{
string _newLine = "";
_sr = new StreamReader(_fileName);

while (!_sr.EndOfStream)
{

_newLine = _sr.ReadLine();

if (_newLine.Length > 1)
{
_lineItems.Add(_newLine);
}
}

_coords = new string[_lineItems.Count, 14];

_rowCount = _lineItems.Count;

for (Int16 _i = 0; _i < _lineItems.Count; _i++)
{

if (_lineItems[_i].ToString().Trim().Length > 1)
Split(_i, _lineItems[_i]);
}

}

private void Split(Int16 _index, string _lineItem)
{
string[] _splitItems = _lineItem.Split(new char[1]{'' ''});

for (Int16 _i = 0; _i < 14; _i++)
{
_coords[_index, _i] = _splitItems[_i];
}


}

private void btnStart_Click(object sender, EventArgs e)
{
tmrLoop.Enabled = true;
}

private void tmrLoop_Tick(object sender, EventArgs e)
{
LoopData();
}

private void LoopData()
{

textBox1.Text = _coords[_loopIndex, 0];
textBox2.Text = _coords[_loopIndex, 1];
textBox3.Text = _coords[_loopIndex, 2];
textBox4.Text = _coords[_loopIndex, 3];
textBox5.Text = _coords[_loopIndex, 4];
textBox6.Text = _coords[_loopIndex, 5];
textBox7.Text = _coords[_loopIndex, 6];
textBox8.Text = _coords[_loopIndex, 7];
textBox9.Text = _coords[_loopIndex, 8];
textBox10.Text = _coords[_loopIndex, 9];
textBox11.Text = _coords[_loopIndex, 10];
textBox12.Text = _coords[_loopIndex, 11];
textBox13.Text = _coords[_loopIndex, 12];
textBox14.Text = _coords[_loopIndex, 13];

if (_loopIndex == _rowCount -1)
{
_loopIndex = 0;
}
else
{
_loopIndex++;
}

}

}
}


这篇关于将数据文本文件加载到多维数组中,然后将内容复制到文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆