【问题标题】:Modify array with user input?用用户输入修改数组?
【发布时间】:2014-11-19 16:28:48
【问题描述】:

单击“添加”按钮时,我正在使用此代码将用户输入添加到我的数组中。

int newcompartment;
string newproductcode, newname, newaddress;
float newweight;

newcompartment = int.Parse(txtcompartment.Text);
newproductcode = txtproduct.Text;
newname = txtname.Text;
newaddress = txtaddress.Text;
newweight = float.Parse(txtqty.Text);

if ((newcompartment > 0) && (newcompartment < productcode.Length) && (rbtnoccupied.Checked == true))
{
      if (productcode[newcompartment - 1] != "")
      {
           MessageBox.Show("Compartment is occupied !");
      }   
      else
      {
           MessageBox.Show("You have successfully added a new array data!");
           compartmentno[newcompartment - 1] = newcompartment;
           productcode[newcompartment - 1] = newproductcode;
           name[newcompartment - 1] = newname;
           weight[newcompartment - 1] = newweight;
           address[newcompartment - 1] = newaddress;   
      }
}                    

我想知道是否有可能有一个 "MODIFY" 按钮,用户可以在其中输入新的输入来替换数组中以前添加的部分或全部数据?

以防万一,如果需要,我的 GUI 包含 5 个文本框和 2 个单选按钮(compartment noproduct codenameweightaddress)和(Occupied /Empty )。

【问题讨论】:

  • 你想要一个可以输入文本的按钮吗?
  • 您的问题到底是什么?你想获得一个索引来修改你的数组数据吗?
  • 如果您使用 List 而不是字符串数组,您可以使用它的 .Add 方法向其中添加项目。
  • 我想让用户在文本框和单选按钮中输入他们的数据,当点击“修改”按钮时,输入的新数据将替换旧的数组数据。

标签: c# arrays


【解决方案1】:

如果您删除嵌套的 if else,相同的逻辑会不会起作用?因此,您可以将其用作添加/修改按钮。如果它是一个新的隔间编号,它将被添加。否则,如果它是旧的,那么它将被更新。对于删除隔间,您只能接受隔间编号。如果存在,则从所有数组中删除该条目。

if ((newcompartment > 0) && (newcompartment < productcode.Length) ) 

//removed this condition
//&& (rbtnoccupied.Checked == true))

{
      //Remove this if else condition. Always update the values.
      //if (productcode[newcompartment - 1] != "")
      //{...}
      //else
      //{...}

      MessageBox.Show("You have successfully added/modified an array data!");
      compartmentno[newcompartment - 1] = newcompartment;
      productcode[newcompartment - 1] = newproductcode;
      name[newcompartment - 1] = newname;
      weight[newcompartment - 1] = newweight;
      address[newcompartment - 1] = newaddress;   
}

编辑:

更好的方法是创建一个名为 Compartment 的类型并使用您可以修改的集合,而不是处理多个数组。

 public class Compartment
    {
        private int compartmentID;
        private string address;
        private string weight;
        private string name;

        public int CompartmentID
        {
            get { return compartmentID; }
            internal set { compartmentID = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Weight
        {
            get { return weight; }
            set { weight = value; }
        }           

        public string Address
        {
            get { return address; }
            set { address = value; }
        }
    }



 public class Form1
    {
        private List<Compartment> compartments;

        public void Form_Load()
        {
            compartments = new List<Compartment>();
        }

        private void AddCompartment(string name, string weight, string address)
        {
            Compartment newCompartment = new Compartment() { CompartmentID = compartments.Count + 1, Address = address, Name = name, Weight = weight };

            compartments.Add(newCompartment);
        }

        private void UpdateCompartment(int id, string name, string weight, string address)
        {
            //Update based on id
        }

        private void RemoveCompartment(int id)
        {
            compartments.Remove(id);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    相关资源
    最近更新 更多