VBA:输入框和取消按钮

时间:2019-04-01 10:44:27

标签: excel vba inputbox

varInput = Application.InputBox("Text", "Title", Type:=2)
If varInput = "False" Then
    Exit Sub
End If

如果按下取消按钮,则返回值为带有“ False”的字符串。 但是在某些使用德语设置的计算机上,我会得到“ Falsch”!

应该如何处理?

2 个答案:

答案 0 :(得分:2)

删除引号:

public class MyApplication {

private Form current;
private Resources theme;

private Transition defaultInTrans = CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 300);
private Transition defaultOutTrans = CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 300);
private Transition popupInTrans = CommonTransitions.createCover(CommonTransitions.SLIDE_VERTICAL, false, 300);
private Transition popupOutTrans = CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, 300);

public void init(Object context) {
    theme = UIManager.initFirstTheme("/theme");
    Toolbar.setGlobalToolbar(true);
}

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    new MainForm().show();
}

public void stop() {
    current = getCurrentForm();
    if (current instanceof Dialog) {
        ((Dialog) current).dispose();
        current = getCurrentForm();
    }
}

public void destroy() {
}

class MainForm extends Form {
    public MainForm() {
        setLayout(BoxLayout.y());

        Button slideBut = new Button("Slide Form");
        Button popBut = new Button("Popup Form");
        add(slideBut).add(popBut);

        slideBut.addActionListener(e -> {
            new SlideForm().show();
        });

        popBut.addActionListener(e -> {
            new PopupForm(this).show();
        });
    }
}

class SlideForm extends Form {
    public SlideForm() {
        Style bg = getContentPane().getUnselectedStyle();
        bg.setBgTransparency(255);
        bg.setBgColor(0x00ff00);
        getToolbar().setBackCommand("", e -> {
            new MainForm().showBack();
        });
        add(new Label("Slide Form content"));
    }
}

class PopupForm extends Form {
    public PopupForm(Form orig) {
        Style bg = getContentPane().getUnselectedStyle();
        bg.setBgTransparency(255);
        bg.setBgColor(0xff0000);

        getToolbar().setBackCommand("", e -> {
            new MainForm().showBack();
            orig.setTransitionInAnimator(defaultInTrans);
            orig.setTransitionOutAnimator(defaultOutTrans);
        });
        add(new Label("This is a popup!"));

        // remove source animation to remain in place
        orig.setTransitionInAnimator(null);
        orig.setTransitionOutAnimator(null);

        // add transition for target popup to appear and vanish from/to the bottom
        setTransitionInAnimator(popupInTrans);
        setTransitionOutAnimator(popupOutTrans);
    }
}

答案 1 :(得分:2)

使用VarType(varInput) = vbBoolean时,还必须始终将变量的类型测试为布尔值:Application.InputBox

Dim varInput As Variant
varInput = Application.InputBox("Text", "Title", Type:=2)

If VarType(varInput) = vbBoolean And varInput = False Then
    Exit Sub
End If

如果仅测试False

If varInput = False Then

...,如果您输入0Falsch(在德语Excel中)或False(在英语Excel中),它也会退出sub。

之所以会发生这种情况,是因为与0相比,字符串"Falsch""False"(或False)会自动转换为布尔值。但是只有 Cancel 按钮会返回一个真正的布尔值。