0

我需要构建一个允许用户输入 100 种不同测量值的表单。然后输入 100 个不同的权重。我想使用相同的文本框。一个用于测量,一个用于重量。然后让他们存储该值,然后重置以接受下一个值,依此类推。谁能指出我正确的方向来实现这一目标?我已经搜索了一个多小时,找不到任何东西。预先感谢您的帮助。

4

1 回答 1

3

当用户单击提交按钮时,将两个文本框的值添加到列表或字典中。这样做直到达到想要的 list.Count 数量。如果我没有正确理解您的问题,请告诉我。

在 WPF (C#) 中,它看起来像这样。

// Dictionary to save values
Dictionary<string, int> dict = new Dictionary<string, int>();

// Method that is called on user submit button click
private void HandleSubmit(object sender, EventArgs args) {
    // Add values of both textboxes to dictionary
    dict.Add(textBox1.Text, Int32.Parse(textBox2.Text));

    // Check if all data is entered
    // then activate custom method
    if(dict.Count >= 100) {
        CUSTOMMETHOD(dict);
    }
}

- 编辑 -

@briskovich 据我了解您的评论。您想先保存所有 100 个压力样本,然后输入 100 个重量样本。在这种情况下,不需要使用字典,您可以使用两个 List<int> 来表示压力和重量。在这种情况下,代码看起来像这样:

// Variables to save our data
// numberValues - number of values user needs to enter
// pressureList - list of entered pressure data
// weightList - list of entered weight data
int numberValues = 100;
List<int> pressureList = new List<int>();
List<int> weightList = new List<int>();

// Method that is called on user submit button click
// This method uses only one text box to input data,
// first we input pressure data until limit is reached 
// and then weight data
private void HandleSubmit(object sender, EventArgs args) {
    // Check if we are still entering pressure data, that
    // is until we reach numberValues of pressure data values
    // Same thing goes for weight list
    if (pressureList.Count < numberValues) {
        pressureList.Add(Int32.Parse(textBox1.Text));
    }
    else if (weightList.Count < numberValues) {
        weightList.Add(Int32.Parse(textBox1.Text));
    }
    else {
        // When we have #numberValues of values in both 
        // lists we can call custom method to process data
        CUSTOMMETHOD(pressureList, weightList);
    }
}

// Method for processing data
private void CUSTOMMETHOD(List<int> pressures, List<int> weights) {
    // This loop will go through all values collected and
    // will give you access to both pressure and weight on
    // each iteration
    for (int index = 0; index < numberValues; index++) {
        int currentPressure = pressures.ElementAt(index);
        int currentWeight = weights.ElementAt(index);

        // Do processing here
    }
}
于 2012-08-21T00:19:44.007 回答