激活按钮上的Void按下

时间:2012-09-02 04:27:34

标签: c# button onclick void

非常直截了当的问题,我刚刚忘记了正确的编码。我有一个空白设置,我希望它在单击按钮时运行。

Void我想要执行:

    public void giveWeapon(int clientIndex, string weaponName)
    {

        uint guns = getWeaponId(weaponName);

        XDRPCExecutionOptions options = new XDRPCExecutionOptions(XDRPCMode.Title, 0x822728F8);  //Updated
        XDRPCArgumentInfo<uint> info = new XDRPCArgumentInfo<uint>(getPlayerState(clientIndex));
        XDRPCArgumentInfo<uint> info2 = new XDRPCArgumentInfo<uint>((uint)guns);
        XDRPCArgumentInfo<uint> info3 = new XDRPCArgumentInfo<uint>((uint)0);
        uint errorCode = xbCon.ExecuteRPC<uint>(options, new XDRPCArgumentInfo[] { info, info2, info3 });
        iprintln("gave weapon: " + (guns.ToString()));
        giveAmmo(clientIndex, guns);
        //switchToWeapon(clientIndex, 46);

    }

我只想让它按下按钮点击:

    private void button14_Click(object sender, EventArgs e)
    {
     // Call void here

    }

3 个答案:

答案 0 :(得分:3)

void是一个关键字,表示您的功能 giveWeapon未返回值。所以你的正确问题是:“我怎样才能调用函数?”

答案:

private void button14_Click(object sender, EventArgs e)
{
    int clientIndex = 5; // use correct value
    string weaponName = "Bazooka"; // use correct value
    giveWeapon(clientIndex, weaponName);
}

如果giveWeapon在另一个类中定义,则需要创建一个实例并在该实例上调用该方法,即:

ContainingClass instance = new ContainingClass();
instance.giveWeapon(clientIndex, weaponName);

作为旁注,您的代码可读性将因使用implicitly typed local variables

而受益匪浅
public void giveWeapon(int clientIndex, string weaponName)
{
    uint guns = getWeaponId(weaponName);

    var options = new XDRPCExecutionOptions(XDRPCMode.Title, 0x822728F8);  //Updated
    var info = new XDRPCArgumentInfo<uint>(getPlayerState(clientIndex));
    var info2 = new XDRPCArgumentInfo<uint>(guns); // guns is already uint, why cast?
    var info3 = new XDRPCArgumentInfo<uint>(0); // same goes for 0
    uint errorCode = xbCon.ExecuteRPC<uint>(options, new XDRPCArgumentInfo[] { info, info2, info3 });
    iprintln("gave weapon: " + guns); // ToString is redundant
    giveAmmo(clientIndex, guns);
    //switchToWeapon(clientIndex, 46);
}

答案 1 :(得分:1)

简单地说:

private void button14_Click(object sender, EventArgs e)
{
    giveWeapon(clientIndex, weaponName);
}

只要giveWeaponbutton14属于同一个班级,那么它就能正常运作。

希望这有帮助!

答案 2 :(得分:1)

然后调用它

private void button14_Click(object sender, EventArgs e)
{

   giveWeapon(10, "Armoured Tank");
}