Windows Forms动态按钮每个按钮的自定义操作

时间:2019-05-12 18:23:09

标签: c# windows forms

我已经编写了一个AddItem函数,可以将一个项目添加到列表视图中。我还创建了创建动态按钮的功能。但是一旦创建了动态按钮,我希望它在按下按钮时能够使AddItem功能起作用。

我不知道如何解决此问题,因为对于C#和Windows窗体而言,它相对较新。

def chooseBlock():
    global player, copylist, colorList, colorChoice, blocksLeft
    #running list of blocks and their positions once placed
    copylist.append(player.copy())
    #running list of those blocks' colors
    colorList.append(colorChoice)
    #resetting player position
    player.y = 50 
    #choosing the color of the next block randomly from a list
    colorChoice = random.choice(colors)
    #subtracting 1 from the count of the blocks left to place after one is placed
    blocksLeft = blocksLeft - 1

    while True:
        adjoining = set()
        for i in range(len(copylist)):
            for j in range(len(copylist)):
                if i == j or colorList[i] != colorList[j]:
                    continue
                if (copylist[i][0] == copylist[j][0] and abs(copylist[i][1]-copylist[j][1]) == height) or \
                   (copylist[i][1] == copylist[j][1] and abs(copylist[i][0]-copylist[j][0]) == width):
                    adjoining.add(i)
                    adjoining.add(j)

        if not adjoining:
            break

        copylist = [copylist[i] for i in range(len(copylist)) if i not in adjoining]
        colorList = [colorList[i] for i in range(len(colorList)) if i not in adjoining]

    testmovedown = True
    while testmovedown:
        testmovedown = False
        for i in range(len(copylist)):
            if copylist[i][1] >= 390:
                continue
            if not any([copylist[j] for j in range(len(copylist)) \
                        if i != j and copylist[i][0] == copylist[j][0] and copylist[i][1] + height == copylist[j][1]]):
                copylist[i][1] += height
                testmovedown = True

如您所见,AddButton函数带有一个价格和商品名称,一旦单击按钮,我希望additem函数以相同的价格和商品名称运行。

谢谢!

2 个答案:

答案 0 :(得分:0)

您可以为按钮的Tag分配一个元组。

private void AddButton(string Name, string Text, int Posx, int Posy, double Price, string ItemName)
{
    ...

    NewButton.Tag = (Price, ItemName);
}

然后,您可以从事件中的sender获取该元组的值,该键实际上是按钮。

private void NewButton_Click(object sender, EventArgs e)
{
    double price = (((double Price, string ItemName))((Button)sender).Tag).Price;
    string itemName = (((double Price, string ItemName))((Button)sender).Tag).ItemName;

    ...
}

答案 1 :(得分:0)

据我了解,您需要一个为每个动态按钮使用一些特定值的处理机。您可以使用的方法很少。

  1. 使用标签属性并以某种格式保存值

    import requests
    
    def get_response(url, params):
        resp = requests.get(url, params)
        return resp.json()
    
    
    url = 'https://en.wikipedia.org/w/api.php?action=query&list=allpages&format=json&aplimit=500'
    
    params = {}
    while True:
        json_resp = get_response(url, params)
        params = json_resp["continue"]
        ...
    

    然后在处理程序中

    newBut.Tag = $"{Price},{ItemName}";
    
  2. 创建字典,每次添加新按钮时,将其添加为字典的键,然后在处理程序中获取数据(但这是最糟糕的方法之一,因此请避免使用它);

  3. 使用“命令”模式。制作一个特殊的类来执行您的操作。看起来应该像这样

    //always check for null
    Button button = sender as Button;
    string data = button.Tag as string;
    //do staff with your data
    

这有点棘手,但引入了以后可以使用的良好模式。

相关问题