如何在文本框中显示字符串数组 [英] How do you display a string array in a text box

查看:66
本文介绍了如何在文本框中显示字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C# 新手,我的第一个 Windows 窗体应用程序有问题.

我有来自串行端口的数据,并且已经找到了一种处理数据并将其存储到 3 个不同的字符串数组中的方法.我无法解决的是如何将数组中的数据显示到各自的文本框中,我只想显示当前索引位置所指向的数组中的数据.

我的 windows 窗体设计中有 3 个文本框,它们已经被命名为:textBoxmagtextBoxlattextBoxlon我想在相应的文本框中显示变量 string[] magstring[] latstring[] lon.>

请帮助我,我正确地坚持了这一点,尽管对更有经验的 C# 程序员来说这似乎很简单.下面是我的代码:

使用系统;使用 System.Collections.Generic;使用 System.ComponentModel;使用 System.Data;使用 System.Drawing;使用 System.Linq;使用 System.Text;使用 System.Windows.Forms;命名空间 WindowsFormsApplication1{公共部分类 Form1 :表单{string RxString;//原始串行数据的存储位置string[] mag = new string[1000];//存储磁数据的地方string[] lat = new string[1000];//存放纬度数据的地方string[] lon = new string[1000];////存放经度数据的地方string ends = "\r\n";//数据句标识符结束string starts = "G";//数据句标识符的开始国际我;//索引纬度数据int j;//索引lon数据int k;//索引mag数据公共 Form1(){初始化组件();}private void buttonStart_Click(object sender, EventArgs e){serialPort1.PortName = "COM5";//定义PIC连接的串口serialPort1.BaudRate = 4800;//设置波特率以匹配PICserialPort1.Open();if (serialPort1.IsOpen)//如果端口是开放的可以按启动键{buttonStart.Enabled = false;//开始按钮禁用buttonStop.Enabled = true;//停止按钮启用textBox1.ReadOnly = false;//允许在文本框中写入}}private void buttondtop_Click(object sender, EventArgs e){如果 (serialPort1.IsOpen){serialPort1.Close();buttonStart.Enabled = true;buttonStop.Enabled = false;textBox1.ReadOnly = true;}}private void DisplayText(对象发送者,EventArgs e){textBox1.AppendText(RxString);//将原始数据字符串中的水添加到文本框中已有的内容中}private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//当端口打开并且缓冲区中有数据时激活{RxString = serialPort1.ReadExisting();//从串口读取原始数据为字符串this.Invoke(new EventHandler(DisplayText));//invoke 允许它调用函数显示文本if (RxString.StartsWith(starts))//如果字符串以G"开头{if ((RxString.Length > 36))//如果至少有一个字符串句子{if (RxString.EndsWith(ends))//如果字符串以\r\n"结尾处理字符串serialPort1.Close();//关闭串口lat[i] = RxString.Split(',')[0].Substring(4);//在字符串数组中提取纬度存储i++;//增加纬度数据数组的索引lon[j] = RxString.Split(',')[2];//提取经度存储在字符串数组中j++;//增加经度数据数组的索引mag[k] = RxString.Split(',')[3].Substring(7).Trim();//提取磁性数据存储在字符串数组中k++;//磁性数据数组的递增索引RxString = null;//重置原始数据字符串serialPort1.Open();//打开串口准备新的字符串语句}}else RxString = null;//如果原始数据字符串语句不是以G"开头则重置数组}private void Form1_FormClosing(对象发送者,FormClosingEventArgs e){if (serialPort1.IsOpen) serialPort1.Close();}private void labellat_Click(对象发送者,EventArgs e){}private void label3_Click(object sender, EventArgs e){}}}

解决方案

您需要将 string[]s 转换为 strings.您可以使用 string.Join() 方法轻松完成此操作:

字符串分隔符 = ", ";string[] mag = new string[] { "hello", "world" };textBoxmag.Text = string.Join(separator, mag);//textBoxmag.Text == "你好,世界";

Hi I am new to C# and I having problems with my first windows form application.

I have data coming in from serial port and have worked out a way of processing and storing the data into 3 different string arrays. What I cant work out is how to display the data in the arrays into their own individual text boxes, I would only want to display the data in the array pointed to by the current index position.

I have 3 text boxes in my windows forms design already they are named: textBoxmag, textBoxlat and textBoxlon I would like to display the variables string[] mag, string[] lat and string[] lon in their corresponding text boxes.

Please help me guys I properly stuck with this even though it may seem simple to you more experienced C# programmers. Below is my code:

using System;
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
    {

        string RxString;// where the raw serial data is stored
        string[] mag = new string[1000];//where magnetic data is stored 
        string[] lat = new string[1000];//where latidude data is stored
        string[] lon = new string[1000];///where longitude data is stored 
        string ends = "\r\n";//end of data sentence identifier
        string starts = "G";//start of data sentence identifier

        int i; //index lat data 
        int j;//index lon data
        int k;//index mag data


        public Form1()
        {
            InitializeComponent();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM5";//define sierial port in which PIC is connected to
            serialPort1.BaudRate = 4800;//set baud rate to match PIC

            serialPort1.Open();
            if (serialPort1.IsOpen)//if the port is open you can press the start button
            {
                buttonStart.Enabled = false;//start button disbaled
                buttonStop.Enabled = true;//stop button enabled
                textBox1.ReadOnly = false;//allow writing in text box
            }
        }

        private void buttondtop_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
                buttonStart.Enabled = true;
                buttonStop.Enabled = false;
                textBox1.ReadOnly = true;
            }
        }



        private void DisplayText(object sender, EventArgs e)
        {
            textBox1.AppendText(RxString);//add watever is in raw data string to what is already in text box

        }


        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//activates when port is open and data in buffer
        {
            RxString = serialPort1.ReadExisting();//read raw data from serial port into string
            this.Invoke(new EventHandler(DisplayText));//invoke allows it to call function diplay text

            if (RxString.StartsWith(starts))//if the string starts with "G"
            {
                if ((RxString.Length > 36))//if there is aleast one string sentence
                {
                    if (RxString.EndsWith(ends))// if the string ends with "\r\n" process the string
                        serialPort1.Close();//close serial port 
                    lat[i] = RxString.Split(',')[0].Substring(4);// extract latitude store in string array
                    i++;//increment index of latitude data array
                    lon[j] = RxString.Split(',')[2];// extract longitude store in string array
                    j++;//increment index of longitude data array
                    mag[k] = RxString.Split(',')[3].Substring(7).Trim();// extract magnetic data store in string array
                    k++;//increment index of magnteric data array
                    RxString = null;//Reset raw data string
                    serialPort1.Open();//open serial port ready for new string sentence
                }
            }
            else RxString = null;// if the raw data string sentence does not start with "G" reset the array
           }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (serialPort1.IsOpen) serialPort1.Close();
        }

        private void labellat_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}

解决方案

You will need to convert your string[]s into strings. You can do this easily with the string.Join() method:

string separator = ", ";
string[] mag = new string[] { "hello", "world" };

textBoxmag.Text = string.Join(separator, mag);
// textBoxmag.Text == "hello, world";

这篇关于如何在文本框中显示字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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