2016-08-21 53 views
0

我有2個按鈕和一個DataGridView與2列(0 & 1)。替代過程

  • 1st按鈕將隨機化的單元格從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 
    
  • 第二屆按鈕,不過,應該是比較,如果其他文本框的文本具有相同的字符串變量(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

正常的,現在......這是要聽起來不可思議!但是,按照Olivier先生的建議調查「調試」代碼,我敲打了我想與括號進行比較的字符串,並意識到它已經在折線空間後輸出了!所以,我使用以下函數從兩個比較字符串中刪除「空格」!它血腥的工作!雖然這次肯定。^_^

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

如果RemoveWhitespace(tbxSubmit.Text)= RemoveWhitespace(GetCell(X,0))然後

+1

如果需要,只除去開頭和結尾的空格,只需要使用'myString.Trim()'。 –

+0

太棒了!是的,那會做到的。^_^ – Sky7ure

回答

1

轉動局部變量成類字段。

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 

這些字段現在可以從每個子,功能和屬性來訪問。

當然Button3_Click必須在Button2_Click之前調用,因爲這些字段是在第一個方法中初始化的。如果情況並非如此,那麼你應該考慮另一種方法。

爲小區接入

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

+1

調試你的代碼。在比較線上設置一個斷點。運行代碼並檢查變量,在快速監視窗口等中執行方法。請參閱:https://www.google.ch/search?q=visual+studio+C%23+debugging+introduction –

+0

謝謝,奧利維爾先生。你是一位真正的主人。^_ ^ – Sky7ure