替代流程

时间:2016-08-21 18:45:41

标签: vb.net variables datagridview textbox

我有2个按钮和一个带2列(0& 1)的DataGridView。

  • 第一个按钮将随机单元格从Column(1)传输到TextBox。然后,它将Cell存储在变量(a)中,加上在变量(b)中将其对立的单元格。

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim rnd As New Random
        Dim x As Integer = rnd.Next(0, Form1.DataGridView1.Rows.Count)
        Dim y As Integer = 1
        Dim a As String = Form1.DataGridView1.Rows(x).Cells(y).Value
        Dim b As String = Form1.DataGridView1.Rows(x).Cells(y - 1).Value
        TextBox3.Text = a
    End Sub
    
  • 然而,第二个按钮应该比较另一个TextBox的文本是否具有与字符串相同的字符串变量(b)。现在,如果是这样,那么它必须显示某个消息,依此类推......

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If TextBox4.Text = b Then '<<< ISSUE HERE!
            MsgBox("Correct! ^_^")
        ElseIf TextBox4.Text = "" Then
            MsgBox("You have to enter something first! O_o")
        Else
            MsgBox("Wrong! >,<")
        End If
    End Sub
    

问题是变量(b)肯定不会在两个“私有”潜艇上共享。所以,没有什么可以比较在第二个按钮的子!我认为这里的解决方案是将“随机化过程”拆分为一个单独的函数,然后在第一个按钮被激活时直接执行它。此外,该函数的变量必须以某种方式共享,我当然不知道如何!

感谢Olivier先生,代码得到了显着改善!然而,我仍然遇到一个“错误的”比较问题,不知何故!

Dim RND As New Random Dim x As Integer

Private Function GetCell(ByVal rowIndex As Integer, ByVal cellIndex As Integer) As String
    Return Form1.DataGridView1.Rows(rowIndex).Cells(cellIndex).Value
End Function

Private Sub btnRoll_Click(sender As Object, e As EventArgs) Handles btnRoll.Click
    x = RND.Next(0, Form1.DataGridView1.Rows.Count)
    tbxRoll.Text = GetCell(x, 1)
End Sub

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    If tbxSubmit.Text = GetCell(x, 0) Then
        MsgBox("Correct! ^_^")
    ElseIf tbxSubmit.Text = "" Then
        MsgBox("You have to enter something first! O_o")
    Else
        MsgBox("Wrong! >,<")
    End If
End Sub</code>

Well, unbelievably, I read a guide about "comparison operations" in VB.net and tried out the first yet the most primal method to compare equality - which was to use .Equals() command - and worked like a charm! Thank God, everything works just fine now. ^_^

If tbxSubmit.Text.Equals(GetCell(x, 0)) Then

Alright now... This is going to sound weird! But, following Mr. Olivier's advise to investigate "debug" the code, I rapped the string I'm trying to compare with brackets and realized that it's been outputted after a break-line space! So, I used the following function to remove the "white-space" from both of the comparison strings! And it bloody worked! This time for sure, though. ^_^

Function RemoveWhitespace(fullString As String) As String Return New String(fullString.Where(Function(x) Not Char.IsWhiteSpace(x)).ToArray()) End Function

Private Function GetCell(ByVal rowIndex As Integer, ByVal cellIndex As Integer) As String Return Form1.DataGridView1.Rows(rowIndex).Cells(cellIndex).Value End Function Private Sub btnRoll_Click(sender As Object, e As EventArgs) Handles btnRoll.Click x = RND.Next(0, Form1.DataGridView1.Rows.Count) tbxRoll.Text = GetCell(x, 1) End Sub Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click If tbxSubmit.Text = GetCell(x, 0) Then MsgBox("Correct! ^_^") ElseIf tbxSubmit.Text = "" Then MsgBox("You have to enter something first! O_o") Else MsgBox("Wrong! >,<") End If End Sub</code>

1 个答案:

答案 0 :(得分:0)

将局部变量转换为类字段。

Dim rnd As New Random
Dim x As Integer
Dim y As Integer
Dim a As String
Dim b As String

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    x = rnd.Next(0, Form1.DataGridView1.Rows.Count)
    y = 1
    a = Form1.DataGridView1.Rows(x).Cells(y).Value
    b = Form1.DataGridView1.Rows(x).Cells(y - 1).Value
    TextBox3.Text = a
End Sub

现在可以从每个Sub,Function和Property访问这些字段。

当然Button3_Click必须在Button2_Click之前调用,因为字段是在第一种方法中初始化的。如果不是这种情况,那么你应该考虑另一种方法。

为Cell访问创建一个函数

Private Function GetCell(ByVal rowIndex As Integer, ByVal cellIndex As Integer) _
                                                                          As String
    Return Form1.DataGridView1.Rows(rowIndex).Cells(cellIndex).Value
End Function

然后比较

If TextBox4.Text = GetCell(x, y - 1) Then 
    ...

并且不再将值存储在ab中。如果y始终为1,请直接使用这些数字。

If TextBox4.Text = GetCell(x, 0) Then 
    ...

还有一件事:在创建Click事件处理程序(例如btnRandomize)之前,在属性网格中为您的按钮指定发言名称。然后你也会得到这些例程的说话名称(例如btnRandomize_Click)。

请参阅:
- VB.NET Class Examples
- Visual Basic .NET/Classes: Fields