vb.net 如果复选框被选中,运行“某事”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/864767/
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-09 14:13:24  来源:igfitidea点击:

If checkbox is checked, run "something"

vb.net

提问by Kenny Bones

I've got a simple Visual Basic 2008 Express Edition form which looks like this: [link Screenshot of simple form][1]

我有一个简单的 Visual Basic 2008 Express Edition 表单,如下所示:[link Screenshot of simple form][1]

I need some help with a skeleton script, which checks to see if each checkbox is checked or not. I've got a set of Word templates which all contain a macro. And I want to run the macro of each template, ifthe template exists.

我需要一些有关骨架脚本的帮助,该脚本会检查是否选中了每个复选框。我有一组 Word 模板,它们都包含一个宏。如果模板存在,我想运行每个模板的宏。

So basically something like the following (including loads of errors, probably):

所以基本上类似于以下内容(可能包括大量错误):

    Dim strFile1
    Dim strFile2
    Dim strFile3
    Dim strFile4
    Dim strFile5
    Dim strFile6

if checkbox1.Checked Then
try to run command (Winword.exe c:\temp\document.doc /mNameOfMacro)
On error MsgBox ("could not find file from checkbox1")

    if checkbox2.Checked Then
try to run command (Winword.exe c:\temp\document2.doc /mNameOfMacro)
On error MsgBox ("could not find file from checkbox2")

    if checkbox3.Checked Then
try to run command (Winword.exe c:\temp\document3.doc /mNameOfMacro)
On error MsgBox ("could not find file from checkbox3")

    if checkbox4.Checked Then
try to run command (Winword.exe c:\temp\document4.doc /mNameOfMacro)
On error MsgBox ("could not find file from checkbox4")


    if checkbox5.Checked Then
try to run command (Winword.exe c:\temp\document5.doc /mNameOfMacro)
On error MsgBox ("could not find file from checkbox5")

    if checkbox5.Checked Then
try to run command (Winword.exe c:\temp\document5.doc /mNameOfMacro)
On error MsgBox ("could not find file from checkbox5")

I know this pseudocode isn't correct at all, because I'm kind of a beginner, and designer over a programmer. But I've just started learning and I know this is pretty basic. It's just getting an overview of the logics in programming. And I think that getting to learn how to do this will help me with other things as well.

我知道这个伪代码根本不正确,因为我是一个初学者,设计师而不是程序员。但我刚刚开始学习,我知道这是非常基本的。它只是对编程逻辑的概述。而且我认为开始学习如何做到这一点也会帮助我处理其他事情。

回答by Kenny Bones

Ok, just found the answer by doing this:

好的,刚刚通过这样做找到了答案:

 Dim strFile1 = ("c:\temp\file3.txt")
 Dim strFile2 = ("c:\temp\file4.txt")

    If chkbxRapport.Checked Then
        If My.Computer.FileSystem.FileExists(strFile1) Then
            System.Diagnostics.Process.Start(strFile1)
        Else : MsgBox("Can't find the file" & " " & strFile1)
        End If
    End If

    If chkbxRapport_EN.Checked Then
        If My.Computer.FileSystem.FileExists(strFile2) Then
            System.Diagnostics.Process.Start(strFile2)
        Else : MsgBox("Can't find the file" & " " & strFile2)
        End If
    End If

回答by Serapth

This blog post should explain it sufficiently. Running a process in VB

这篇博文应该充分解释它。 在VB中运行一个进程

Its System.Diagnostics.Processthat you are most interested in.

它是您最感兴趣的System.Diagnostics.Process