我正在尝试从选定索引处的传递数组中删除一个值。基本上采用显示数组的列表框的选定索引并使用它来删除条目。由于某种原因,当我运行程序时,什么都没有被删除并且保持不变。我正在用 C# 在 Visual Studio 2010 中编程
public double [] redrawArray(double[] ary, int selectedIndex)
{
double[] redoneArray = new double[ary.GetUpperBound(0)];
for (int i = selectedIndex; i < ary.GetUpperBound(0); i++)
{
redoneArray[i] = ary[i + 1];
}
//redoneArray[ary.GetUpperBound(0)] = 0;
return redoneArray;
}
这是代码的删除按钮部分
private void btnDelete_Click(object sender, EventArgs e)
{
int selectedIndex;
if (this.lstClients.SelectedIndex <= 1)
{
return;
}
//else if ((this.lstClients.SelectedIndex - 2) < arrayIndex)
else
{
selectedIndex = this.lstClients.SelectedIndex - 2;
this.lstClients.Items.RemoveAt(this.lstClients.SelectedIndex);
lbSI.Text = (this.lstClients.SelectedIndex - 2).ToString();
redrawArray(mFirstNameArray, selectedIndex);
redrawArray(mLastNameArray, selectedIndex);
redrawArray(mAgeArray, selectedIndex);
redrawArray(mHeightArray, selectedIndex);
redrawArray(mStartWeightArray, selectedIndex);
redrawArray(mGoalWeightArray , selectedIndex);
redrawArray(mTotalWeeksArray, selectedIndex);
//arrayIndex += -1;
lstClients.Items.Clear();
loadListBox();
}
}
这是我将其加载到列表中的主要代码
private void loadListBox()
{
string currentClient;
int lineNumber = 0;
string formattedName;
string strAvgBMI;
string strLowBMI;
string strHghBMI;
double dStartAvgBMI;
double dStartLowBMI;
double dStartHghBMI;
double dEndAvgBMI;
double dEndLowBMI;
double dEndHghBMI;
lstClients.Items.Add(" CLIENT NAME AGE HEIGHT(in) START WEIGHT START BMI GOAL WEIGHT GOAL BMI WEEKS");
lstClients.Items.Add("================= ===== =========== ============== =========== ============= ========== =========");
for (int index = 0; index < arrayIndex; index++)
{
if (mFirstNameArray[index] == null)
{
continue;
}
lineNumber++;
formattedName = mFirstNameArray[index];
formattedName += " ";
formattedName += mLastNameArray[index];
mStartBMI[index] = calcBMI(mHeightArray[index], mStartWeightArray[index]);
mEndBMI[index] = calcBMI(mHeightArray[index], mGoalWeightArray[index]);
currentClient = index.ToString() + " ";
currentClient += formattedName.PadRight(18) + " ";
currentClient += mAgeArray[index].ToString("##").PadLeft(4) + " ";
currentClient += mHeightArray[index].ToString("##.#0").PadRight(4) + " ";
currentClient += mStartWeightArray[index].ToString("###.0").PadRight(4) + " ";
currentClient += mStartBMI[index].ToString("##.#0").PadRight(4) + " ";
currentClient += mGoalWeightArray[index].ToString("###.0").PadRight(4) + " ";
currentClient += mEndBMI[index].ToString("###.#0").PadRight(4) + " ";
currentClient += mTotalWeeksArray[index].ToString("##").PadRight(4);
lstClients.Items.Add(currentClient);
}
dStartAvgBMI = sumAvg(mStartBMI, arrayIndex);
dStartHghBMI = maxArray(mStartBMI, arrayIndex);
dStartLowBMI = minArray(mStartBMI, arrayIndex);
dEndAvgBMI = sumAvg(mEndBMI, arrayIndex);
dEndHghBMI = maxArray(mEndBMI, arrayIndex);
dEndLowBMI = minArray(mEndBMI, arrayIndex);
strAvgBMI = "";
strHghBMI = "";
strLowBMI = "";
strAvgBMI = " Average: " + dStartAvgBMI.ToString("0#.#0") + " " + dEndAvgBMI.ToString("0#.#0");
strHghBMI = " High: " + dStartHghBMI.ToString("0#.#0") + " " + dEndHghBMI.ToString("0#.#0");
strLowBMI = " Low: " + dStartLowBMI.ToString("0#.#0") + " " + dEndLowBMI.ToString("0#.#0");
lstClients.Items.Add(strAvgBMI);
lstClients.Items.Add(strHghBMI);
lstClients.Items.Add(strLowBMI);
}