首页 新闻 会员 周边

C#中怎么将listbox里的一组数据存放到一维数组中??求大神指导

0
悬赏园豆:10 [已解决问题] 解决于 2017-08-10 10:55

例如:listbox里的数据为:-53,-160,-123,-92,-157,54,-5,173....这样的一组数, 想实现的是把-53放在数组第一个位置,-160放在数组第二个位置,-123放在数组第三个位置。。。请问这怎么实现??

lodyuy的主页 lodyuy | 初学一级 | 园豆:106
提问于:2017-06-06 14:12
< >
分享
最佳答案
0

直接new一个数组,把listbox里的数据遍历存进去就行,数组的长度,你可以按listbox的内容数据量定义,如果不可以做排序,就按你存的顺序保存的

收获园豆:5
小闹闹 | 初学一级 |园豆:192 | 2017-06-06 14:50

能不能给一段代码呢?谢谢你

lodyuy | 园豆:106 (初学一级) | 2017-06-06 15:02

@lodyuy: 你想要js的还是cs类里的代码

小闹闹 | 园豆:192 (初学一级) | 2017-06-06 15:05

@小闹闹: cs类的代码

lodyuy | 园豆:106 (初学一级) | 2017-06-06 15:06

@lodyuy: 

int len=listBox1.Items.Count;      

      string[] str = new string[len];        

    for (int i = 0; i < len; i++)         

   {                str[i] = listBox1.Items[i].ToString();            }

小闹闹 | 园豆:192 (初学一级) | 2017-06-06 15:16

@小闹闹: 我验证了下你写的代码,有问题。

      “int len=listBox1.Items.Count;  ”这句里的len 对应的应该是listBox1数据的个数,如果数据有50个,则len应该等于50,但是我用

int len = listBox1.Items.Count;
listBox3.Items.Add(len);运行后listBox3里的内容是1,说明str[i]数组,把listBox1的一组数据整个的给到了str[0],而不是str[0] = listBox1.Items[0],str[1] = listBox1.Items[1].....

不过还是很感谢你提供的代码。

lodyuy | 园豆:106 (初学一级) | 2017-06-06 15:56

@lodyuy: 

你把我代码全放到winform的load事件里试下就知道了,你的验证有问题,你把你全部验证的代码贴出来

小闹闹 | 园豆:192 (初学一级) | 2017-06-06 16:01

@lodyuy: 

你的listBox3里的内容是1,只能证明你的listbox1的内容是一个,不是多个,怎么就证明了是数组赋值错误,多个的话listBox1.Items.Count肯定不是1

小闹闹 | 园豆:192 (初学一级) | 2017-06-06 16:09

@小闹闹: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.IO;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Forms;
using NationalInstruments.VisaNS;
namespace PhaseTest
{
public partial class Form1 : Form
{
wfy wangfenyi;

public Form1()

{

listBox1.HorizontalScrollbar = true;
InitializeComponent();
}

private void readdate()
{
string GetTraceDate;
GetTraceDate = wangfenyi.GetTraceDate();
listBox1.Items.Add(GetTraceDate);
listBox1.SelectedIndex = listBox1.Items.Count - 1;

int len = listBox1.Items.Count;
listBox3.Items.Add(len);
string[] str = new string[len];
for (int i = 0; i < len; i++)
{
str[i] = listBox1.Items[i].ToString();
}

}

 

这是我验证的代码,我listBox1里的内容是接收网分仪返回的GetTraceDate数据,每次收到的数据是一组数,例如-53.62507901,-160.6366603,-123.6787395,-92.14303104,-157.76411,54.36579603。。。。这样的一组数。

lodyuy | 园豆:106 (初学一级) | 2017-06-06 16:41

@lodyuy: 你listbox1就加了一条数据,你自己可以调试看下

小闹闹 | 园豆:192 (初学一级) | 2017-06-06 16:50

@小闹闹: 能不能加个好友,请教你一下??我QQ:1046794535

lodyuy | 园豆:106 (初学一级) | 2017-06-06 16:58
其他回答(6)
0

按顺序加进去

收获园豆:1
~扎克伯格 | 园豆:1923 (小虾三级) | 2017-06-06 14:16

请问怎么按顺序加进去呢?我是一个初学者,不会,能不能写一段程序呢?

支持(0) 反对(0) lodyuy | 园豆:106 (初学一级) | 2017-06-06 14:18
0

a[0]=-53

a[1]=-160

a[2]=-123

...

这样行不?

收获园豆:1
爱编程的大叔 | 园豆:30839 (高人七级) | 2017-06-06 14:17

不行,因为listbox里的内容是一个变化的一组数据,例如listbox里的内容过1分钟会重新写入一组数据,希望在下一组数据到来前,把前面的数据放到数组里,而我们不知道listbox里的数据具体是多少。该怎么实现呢?

支持(0) 反对(0) lodyuy | 园豆:106 (初学一级) | 2017-06-06 14:26
0

var array[items.count];

for(var i=0;i<items;i++)arrya[i]=items[i]

花飘水流兮 | 园豆:13560 (专家六级) | 2017-06-06 15:05

我验证了你的代码,跟楼上的问题一样,不过还是谢谢你

支持(0) 反对(0) lodyuy | 园豆:106 (初学一级) | 2017-06-06 15:57
0

            int[] xx = new int[listBox1.Items.Count];
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                xx[i] = Convert.ToInt32(listBox1.Items[i].ToString());
            }


            //打印数组
            foreach (var i in xx)
            {
                Console.WriteLine(i);
            }

收获园豆:1
兰冰点点 | 园豆:401 (菜鸟二级) | 2017-06-06 16:08

顺便说一句,你的问题可能不在代码这一块

支持(0) 反对(0) 兰冰点点 | 园豆:401 (菜鸟二级) | 2017-06-06 16:11
0

哪儿来的listBox3   ?

收获园豆:1
西漠以西 | 园豆:1675 (小虾三级) | 2017-06-06 16:09

listBox3是加的一个控件。

支持(0) 反对(0) lodyuy | 园豆:106 (初学一级) | 2017-06-06 16:43
0

获取listbox的值

string s=listBox.value;

 

把接受的值用“,”分割,结果用一个数组接收

string[] sArray=s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

 

******

如果要取出数组每一个项的值:

for(int i=0;i<sArray.Length;i++)

{

string s=sArray[i].Tostring();
}

收获园豆:1
盲人点灯,且行且乐 | 园豆:207 (菜鸟二级) | 2017-06-15 09:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册