0

我正在尝试创建一个自动执行所有操作的函数。这是我当前的代码:

Public Sub IncrementValueBeta(SlideDescription As String, SlideNumber As Integer, FormName As String)
    ChangeSlide (SlideNumber)
    MsgBox ("Test: " + SlideDescription)
    AddClicks = FormName.ClickedTimes.Text + 1
    FormName.ClickedTimes.Text = AddClicks
End Sub

这将为该值增加一个数字,但我试图使代码更简洁,而不必执行if FormName = "Slide1"1000 次,因为它是一个巨大的问卷。

用户将键入"FormName"例如,"Form1"。在代码中,它会像FormName.ClickedTimes.Caption = AddClicks在幻灯片中那样使用它,如果幻灯片是 25 次点击,它将是 26 次,它已经在工作,但只有当我不这样做时 Slide3FormName有没有办法可以做到这一点?如果你知道怎么做,你能帮助我吗,因为如果我必须做If bah = "bah" then elseif bah = "bah" then1000 次,那将是一个真正的痛苦。

这将基本上将表单标签的文本更改为例如,用户单击是,我喜欢运动,代码将加载模块/类并将 label1.text 更改为 +1 点击,并将 label2.text 更改为“问题2",但它不知道它是什么形式,因此它将使用 FormName 中的文本,该文本与参数一起提供,以找出它正在编辑的形式。这就是我想在这里完成的

简而言之,我只想让它从 FormName 和 View FormName.Label1.text = ""as中找到表单Form1.Label1.text = ""

4

2 回答 2

2

*假设只有一个所需表单的实例,请尝试以下操作:

Public Sub IncrementValueBeta(SlideDescription As String, SlideNumber As Integer, FormName As String)
    ChangeSlide(SlideNumber)

    Dim frmTarget As Form = Nothing
    For Each frm As Form In Application.OpenForms
        If frm.Name.ToUpper = FormName.ToUpper Then
            frmTarget = frm
            Exit For
        End If
    Next

    If Not IsNothing(frmTarget) Then
        Dim matches() As Control = frmTarget.Controls.Find("ClickedTimes", True)
        If matches.Length > 0 Then
            AddClicks = matches(0).Text + 1
            matches(0).Text = AddClicks
        End If
    End If
End Sub
于 2013-11-03T17:09:29.593 回答
1

我也会选择使用字典的方式。

Public Class Form1
    Private slides As New Dictionary(Of Integer, String)

    Private currentSlideNumber As Integer
    Private currentSlideDescription As String

    Public Sub IncrementByClick()
        currentSlideDescription = slides(currentSlideNumber + 1)
        currentSlideNumber += 1

        ChangeSlide(currentSlideDescription)
    End Sub

    Public Sub ChangeSlide(ByVal slideDescription As String)
        currentSlideNumber = SelectSlideNumberByDesc(slideDescription)
        currentSlideDescription = slides(currentSlideNumber)
        MessageBox.Show("Successfully changed the slide to slide no. " & currentSlideNumber)
    End Sub

    Private Function SelectSlideNumberByDesc(ByVal slideDesc As String)
        Dim slideNumber As Integer
        For Each slide In slides
            If slide.Value = slideDesc Then
                slideNumber = slide.Key
                Exit For
            End If
        Next
        Return slideNumber
    End Function

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        slides.Add(1, "SLIDE_ONE")
        slides.Add(2, "SLIDE_TWO")
        slides.Add(3, "SLIDE_THREE")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ChangeSlide(TextBox1.Text)
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        IncrementByClick()
    End Sub
End Class

通过单击 button1,幻灯片将根据用户在文本框中输入的幻灯片描述进行更改。

通过单击 btnNext,下一张幻灯片将相应加载。

OO 方式要好得多,但这应该足以展示它。

于 2013-11-03T17:11:35.087 回答