单击 excel vba 用户窗体图片更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26994547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:17:26  来源:igfitidea点击:

excel vba userform picture change on click

excelimagevbaexcel-vbauserform

提问by der de

I made a userform with 10 * 4 spaces for pictures("set11", "set12", ...). At the beginning they are empty. When I click on one of the pictures they should be reset by a random picture(function getRandomPath).

我为图片制作了一个带有 10 * 4 个空格的用户表单(“set11”,“set12”,...)。一开始它们是空的。当我点击其中一张图片时,它们应该被随机图片重置(函数 getRandomPath)。

The sub "clicked" works if I use a Button(start) to click. If I click a picture nothing happens. When I now click again on the Button(start), the before clicked pictures don't change no more.

如果我使用 Button(start) 单击,子“单击”就可以工作。如果我点击图片没有任何反应。当我现在再次点击按钮(开始)时,之前点击的图片不再改变。

Here is the relevant code:

这是相关的代码:

Private Sub set11_Click() 
    Call clicked("1", "1") '*doesn't work*
End Sub 

Private Sub set12_Click() 
    Call clicked("1", "2") '*doesn't work*
End Sub 

Private Sub set13_Click() 
    Call clicked("1", "3") '*doesn't work*
End Sub 

Private Sub set14_Click() 
    Call clicked("1", "4") '*doesn't work*
End Sub 

Private Sub clicked(row As String, column As String) 
    Controls("set" & row & column).Picture = LoadPicture(getRandomPfad()) 
End Sub

Private Sub start_Click() 
    Call clearpictures 
    set11.Picture = LoadPicture(getRandomPfad()) '*works*
    Controls("set12").Picture = LoadPicture(getRandomPfad()) '*works*
    Call clicked("1", "3") '*works*
End Sub

Private Function getRandomPfad()
    Dim random As Integer
    random = Int(6 * Rnd + 1)
    Select Case random
        Case Is = 1
            getRandomPfad = "U:\MMpic\Green.jpg"
        Case Is = 2
            getRandomPfad = "U:\MMpic\Blue.jpg"
        Case Is = 3
            getRandomPfad = "U:\MMpic\Yellow.jpg"
        Case Is = 4
            getRandomPfad = "U:\MMpic\Pink.jpg"
        Case Is = 5
            getRandomPfad = "U:\MMpic\Orange.jpg"
        Case Is = 6
            getRandomPfad = "U:\MMpic\Red.jpg"
    End Select
End Function

I hope you can help me.

我希望你能帮助我。

回答by bp_

Your code must exist inside the userform. If the code is within a Module, it will not work.
enter image description here

您的代码必须存在于用户表单中。如果代码在模块内,它将不起作用。
在此处输入图片说明

Also, ensure you have named the image controls to match the names given in your code: "set11"

此外,请确保您已命名图像控件以匹配代码中给出的名称:“set11”

enter image description here

在此处输入图片说明

回答by der de

I found the solution, I should have repainted:

我找到了解决方案,我应该重新粉刷:

Private Sub clicked(row As String, column As String) 
    Controls("set" & row & column).Picture = LoadPicture(getRandomPfad())
    Repaint
End Sub  

But thank you for your help!

但是谢谢你的帮助!