2013-03-27 117 views
2

嗨,我是C#的新手,我遇到了第一個Windows窗體應用程序的問題。如何在文本框中顯示字符串數組

我有從串口進來的數據,並已經制定了處理和存儲數據到3個不同的字符串數組的方式。我無法解決的是如何將數組中的數據顯示到各自的文本框中,我只想顯示當前索引位置指向的數組中的數據。

我在我的Windows窗體設計3個文本框已經它們被命名爲: textBoxmagtextBoxlattextBoxlon 我想顯示的變量string[] mag,在其相應的文本框中string[] latstring[] lon

請幫助我們,我很好地堅持這一點,即使它可能看起來很簡單,你更有經驗的C#程序員。下面是我的代碼:

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) 
     { 

     } 
    } 
} 

回答

10

您需要將您的string[]秒值進行轉換成string秒。你可以用string.Join() method容易做到這一點:

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

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

我不知道這是否是最好的方式,但在這裏是如何能夠實現method

4

這將顯示字符串數組作爲逗號分隔值:

string strLat = String.Join(", ", lat); 
string strLon = String.Join(", ", lon); 

textBoxlat.Text = strLat; 
textBoxlon.Text = strLon; 
相關問題