在列表框中存储和访问值

时间:2019-02-08 23:52:38

标签: c# winforms

对于正在开发的游戏应用程序,我具有以下Winform:

My Winform

我想要做的是在Beam Arcs和Beam Settings下输入所需的值,在listBox1中选择武器名称,然后按向右箭头按钮将这些设置存储在listBox2中。我希望能够存储多种武器类型,当我在listBox2上单击它们之间时,“弧线”和“设置”值会适当更新。

这是我当前的代码:

public class beamWeaponClass
{
    public string beamValue, beamActiveValue, beamArcsValue,
    beamBonusValue, beamCostValue, beamDamageValue, beamLoadValue,
    beamLockValue, beamNameValue, beamRangeValue, beamRecycleValue,
    listNameLabel;

    public override string ToString()
    {
        return listNameLabel;
    }
}

public void addBeamWeaponButton_Click(object sender, EventArgs e)
{
    beamWeaponClass newBeamWeapon = new beamWeaponClass();
    newBeamWeapon.beamValue = "0 0 1 1 " + addedBeamWeaponsList.Items.Count.ToString(); // Beam
    newBeamWeapon.beamActiveValue = "0"; // Beam Active
    newBeamWeapon.beamArcsValue = calculateArcs(); // Beam Arcs
    newBeamWeapon.beamBonusValue = beamBonus.Text; // Beam Bonus
    newBeamWeapon.beamCostValue = beamCost.Text; // Beam Cost
    newBeamWeapon.beamDamageValue = "1"; // Beam Damage
    newBeamWeapon.beamLoadValue = "0"; // Beam Load
    newBeamWeapon.beamLockValue = "0"; // Beam Lock
    newBeamWeapon.beamNameValue = beamWeaponList.GetItemText(beamWeaponList.SelectedItem); // Beam Name
    newBeamWeapon.beamRangeValue = beamRange.Text; // Beam Range
    newBeamWeapon.beamRecycleValue = beamRecycle.Text; // Beam Recycle
    newBeamWeapon.listNameLabel = beamWeaponList.GetItemText(beamWeaponList.SelectedItem);

    addedBeamWeaponsList.Items.Add(newBeamWeapon);
}

private void selectedBeamWeapons_SelectedIndexChanged(object sender, EventArgs e)
{
    //beamBonus.Text = newBeamWeapon.beamBonusValue;
    //beamCost.Text = newBeamWeapon.beamCost.Text;
    //beamRange.Text = newBeamWeapon.beamRange.Text; // Beam Range
    //beamRecycle.Text = newBeamWeapon.beamRecycle.Text;
}

private void removeBeamWeaponButton_Click(object sender, EventArgs e)
    {

    }

如您所见,当我单击向右箭头按钮时,我正在尝试使用一个类来存储这些值。然后,我一直在尝试查看是否使用selectedBeamWeapons_SelectedIndexChanged()方法存储了这些值,但是由于出现了The name 'newBeamWeapon' does not exist in the current context错误,我将该代码注释掉了。该按钮本身至少在一定程度上起作用。从一个listBox1到listBox2的值发送正常,我只是不知道这些值是否与之匹配。

My Winform after button click

右侧的按钮将重新排序,并选择一个按钮,然后修改其设置并保存。我现在不那么担心,我认为如果我可以保存和访问,那么更改应该很容易。

我不知道我是否正确地存储了值,更不用说访问它们了。如果有人可以浏览我所做的事情并让我知道如何访问这些存储的设置,我将非常感激。

我希望一切都有意义。

1 个答案:

答案 0 :(得分:1)

您在newBeamWeapon中将addBeamWeaponButton_Click声明为局部变量。因此,它仅在此处可见。但是在selectedBeamWeapons_SelectedIndexChanged中,最好使用实际的选择,因为它也可能通过其他用户交互(当用户单击列表时)发生了变化。

var beamWeapon = addedBeamWeaponsList.SelectedItem as beamWeaponClass;
if (beamWeapon != null) {
    beamBonus.Text = beamWeapon.beamBonusValue;
    beamCost.Text = beamWeapon.beamCost.Text;
    beamRange.Text = beamWeapon.beamRange.Text; // Beam Range
    beamRecycle.Text = beamWeapon.beamRecycle.Text;
}

beamWeaponClass很奇怪。您是否真的要将所有这些值存储为字符串?您想如何对这些值进行算术运算?