簡體   English   中英

C#使用多個文本框數組

[英]C# working with multiple arrays of textboxes

我的程序是什么樣子,請在閱讀之前先看一下。

我正在Winforms中創建一個項目,該項目應該從t數組中獲取值,並將它們與尚未聲明的C變量內的值相加,並在按FIFO按鈕時在tf文本框數組中顯示結果。 我的問題是我似乎無法正確執行此操作。 我一直在嘗試定期添加內容,以確保ti或t的內容顯示在tf上,但似乎沒有任何作用。 我的主要問題是該程序僅采用數組的第一個值,而不是全部取值。 我將在下面發布我的代碼。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        foreach (Control c in this.Controls)
        {
            if (c.GetType() == typeof(TextBox))
            {
                ((TextBox)(c)).Text = "0";
            }
        }
    }

    private void fifo_Click(object sender, EventArgs e)
    {
        int c = 0;

        int[] ti = { 0, 1, 2, 3, 4, 5 };
        ti[0] = Convert.ToInt32(tiA.Text);
        ti[1] = Convert.ToInt32(tiB.Text);
        ti[2] = Convert.ToInt32(tiC.Text);
        ti[3] = Convert.ToInt32(tiD.Text);
        ti[4] = Convert.ToInt32(tiE.Text);
        ti[5] = Convert.ToInt32(tiF.Text);

        int[] t = { 0, 1, 2, 3, 4, 5 };
        t[0] = Convert.ToInt32(ta.Text);
        t[1] = Convert.ToInt32(tb.Text);
        t[2] = Convert.ToInt32(tc.Text);
        t[3] = Convert.ToInt32(td.Text);
        t[4] = Convert.ToInt32(te.Text);
        t[5] = Convert.ToInt32(tf.Text);

        int[] tf1 = { 0, 1, 2, 3, 4, 5 };
        tf1[0] = Convert.ToInt32(tfA.Text);
        tf1[1] = Convert.ToInt32(tfB.Text);
        tf1[2] = Convert.ToInt32(tfC.Text);
        tf1[3] = Convert.ToInt32(tfD.Text);
        tf1[4] = Convert.ToInt32(tfE.Text);
        tf1[5] = Convert.ToInt32(tfF.Text);

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

            ti[i] = tf1[i] + 5;
        }

    }
}

由於您始終使用6個元素,因此可以將數組創建更改為:

const int size = 6;
int[] ti = new int[size];
int[] t = new int[size];
int[] tf1 = new int[size];

循環變為

for (int i = 0; i < size; i++)
{
    ti[i] = tf1[i] + 5;
}

順便說一句, +5表示什么意思?

我不知道您認為您在fifo_click()中正在做什么,但是由於您正在對函數范圍內的變量進行操作,因此它似乎無能為力。 我不想為您工作,所以這只會給您一個正確的方向(?),但如果沒有足夠的呼聲。

如果定義了多個相同的對象,那么我認為您應該考慮一個類。 遵循以下原則:

class Element
{
  public int mValue;
  public TextBox mControl;
  public Element (TextBox control) {mControl = control;}
};
class FIFO
{
  public FIFO (Element[] elements) { ... }
  public void SetElement(int index, String value) {...}
  public Element[] mElements;
};

您想將一組FIFO實例存儲為類成員,然后它們將在兩次調用之間保留其數據。

哦,注釋一下您的代碼,因為它使理解WTF變得更加容易:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM