0

以下のコードは、簡単な方法があると確信していますが、私はこれが初めてで苦労しています。textBoxLatitude.Text に項目 [0] を表示させるにはどうすればよいですか?

namespace GPSCalculator
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                List<float> inputList = new List<float>();
                TextReader tr = new StreamReader("c:/users/tom/documents/visual studio 2010/Projects/DistanceCalculator3/DistanceCalculator3/TextFile1.txt");
                String input = Convert.ToString(tr.ReadToEnd());
                String[] items = input.Split(',');

            }



            private void buttonNext_Click(object sender, EventArgs e)
            {

                textBoxLatitude.Text = (items[0]);

            }
        }
    }
4

3 に答える 3

1

items は現在ローカル変数です。クラス変数にする必要があります

于 2012-11-28T11:51:42.383 に答える
0

配列を関数の外に移動します。これにより、クラス全体で利用できるようになります。

namespace GPSCalculator
{
    public partial class Form1 : Form
    {
        String[] items;

        public Form1()
        {
            InitializeComponent();
            List<float> inputList = new List<float>();
            TextReader tr = new StreamReader("c:/users/tom/documents/visual studio 2010/Projects/DistanceCalculator3/DistanceCalculator3/TextFile1.txt");
            String input = Convert.ToString(tr.ReadToEnd());
            items = input.Split(',');
        }



        private void buttonNext_Click(object sender, EventArgs e)
        {

            textBoxLatitude.Text = (items[0]);

        }
    }
}
于 2012-11-28T11:52:37.503 に答える
0

items 配列をクラス フィールドに移動します。

public partial class Form1 : Form
{
    private String[] items; // now items available for all class members

    public Form1()
    {
        InitializeComponent();
        List<float> inputList = new List<float>();
        TextReader tr = new StreamReader(path_to_file);
        String input = Convert.ToString(tr.ReadToEnd());
        items = input.Split(',');
    }

    private void buttonNext_Click(object sender, EventArgs e)
    {
        textBoxLatitude.Text = items[0];
    }
}

また、ボタンをクリックするたびに次のアイテムを表示したいと思います。次に、アイテム インデックスのフィールドも必要です。

public partial class Form1 : Form
{
    private String[] items; // now items available for all class members
    private int currentIndex = 0;

    public Form1()
    {
        InitializeComponent();
        // use using statement to close file automatically
        // ReadToEnd() returns string, so you can use it without conversion
        using(TextReader tr = new StreamReader(path_to_file))
              items = tr.ReadToEnd().Split(',');    
    }

    private void buttonNext_Click(object sender, EventArgs e)
    {
        if (currentIndex < items.Length - 1)
        {
            textBoxLatitude.Text = items[currentIndex];
            currentIndex++
        }
    }
}
于 2012-11-28T11:51:29.790 に答える