简体   繁体   中英

C# working with multiple arrays of textboxes

我的程序是什么样子,请在阅读之前先看一下。

I'm making a project in Winforms that's supposed to take values from the t Array, add them with the value inside a C variable that I have yet to declare and show the results in the tf textbox array when I press the FIFO button. My problem is that I can't seem to do this properly. I've been trying regular additions and whatnot to make sure that the contents of ti or t show on tf, and yet nothing seems to work at all. My main issue is that the program only takes the first value of the array, instead of taking them all. I'll be posting my code below.

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;
        }

    }
}

Since you always work with 6 elements, you can change your array creation to this:

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

And the loop changes to

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

By the way, what does the +5 expression mean?

I don't know what you think you are doing in fifo_click() but since you are doing it to variables inside the scope of the function it isn't going to appear to do anything. I don't want to do you work for you, so this only gives you a push in the right (?) direction, but if its not enough shout.

If you are define a number of identical objects then I think you ought to think about a class. Something along these lines:

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;
};

You want to store an array of FIFO instances as class members and then they will retain their data between calls.

Oh and comment your code, because it makes it so much easier to understand WTF is going on :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM