伊莉討論區

標題: 厲害的來看一下 到底哪邊錯了Q.Q [打印本頁]

作者: 復仇刺    時間: 2020-4-30 02:25 PM     標題: 厲害的來看一下 到底哪邊錯了Q.Q

厲害的來看一下 到底哪邊錯了Q.Q
為什會跑錯誤

我照書做 為什有錯誤Q.Q

Public Class Timer
    'Timer控制項範例
    Dim min As Integer = 0 : Dim sec As Integer
    Dim a As Integer = 0 : Dim b As Integer = 0

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Timer1.Interval = 10
        b += 1
        If b = 10 Then
            b = 0
            a += 1
            If a = 10 Then
                a = 0
                sec += 1
                TrackBar1.Value += 1
                If TrackBar1.Value = 60 Then
                    TrackBar1.Value = 0
                    If sec = 60 Then
                        sec = 0
                        min += 1
                    End If
                End If
            End If
        End If
        TextBox1.Text = min & "分" & sec & "秒" & a & b

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Enabled = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Timer1.Enabled = False
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        min = 0 : sec = 0 : a = 0 : b = 0 : TrackBar1.Value = 0
        TextBox1.Text = min & "分" & sec & "秒" & a & b
    End Sub
End Class
[attach]131480251[/attach]

[attach]131480252[/attach]

[attach]131480254[/attach]

[attach]131480255[/attach]




作者: tryit244178    時間: 2020-5-1 08:10 AM

我複製你的程式碼來跑,完全沒問題耶…
從你的錯誤訊息來看是TrackBar1.Value加到61了…
作者: 復仇刺    時間: 2020-5-1 12:38 PM

tryit244178 發表於 2020-5-1 08:10 AM
我複製你的程式碼來跑,完全沒問題耶…
從你的錯誤訊息來看是TrackBar1.Value加到61了… ...

你好啊感謝您的回答@@"我註解掉那兩行 還是會在即時運算視窗出現這個警示
可程式會跑了@@"
但會有以下的警示 不知是正常現象@@?
第一個可能發生的例外狀況類型 'System.ArgumentOutOfRangeException' 發生於 System.Windows.Forms.dll
第一個可能發生的例外狀況類型 'System.ArgumentOutOfRangeException' 發生於 System.Windows.Forms.dll




Public Class Timer
    'Timer控制項範例
    Dim min As Integer = 0 : Dim sec As Integer
    Dim a As Integer = 0 : Dim b As Integer = 0

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Timer1.Interval = 10
        b += 1
        If b = 10 Then
            b = 0
            a += 1
            If a = 10 Then
                a = 0
                sec += 1
                'TrackBar1.Value = 1
                If TrackBar1.Value = 60 Then
                    'TrackBar1.Value = 0
                    If sec = 60 Then
                        sec = 0
                        min += 1
                    End If
                End If
            End If
        End If
        TextBox1.Text = min & "分" & sec & "秒" & a & b

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Enabled = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Timer1.Enabled = False
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        min = 0 : sec = 0 : a = 0 : b = 0 : TrackBar1.Value = 0
        TextBox1.Text = min & "分" & sec & "秒" & a & b
    End Sub
End Class


作者: tryit244178    時間: 2020-5-1 02:26 PM

復仇刺 發表於 2020-5-1 12:38 PM
你好啊感謝您的回答@@"我註解掉那兩行 還是會在即時運算視窗出現這個警示
可程式會跑了@@"
但會有以下的 ...

你設定TrackBar1.Value的值時,會觸發 TrackBar.ValueChanged事件
  1. If TrackBar1.Value = 60 Then
  2.    TrackBar1.Value = 0
  3.    If sec = 60 Then
  4.       sec = 0
  5.       min += 1
  6.    End If
  7. End If
複製代碼
把這段程式碼改寫在ValueChanged事件裡試試

補充內容 (2020-5-1 02:27 PM):
不需要把TrackBar1.Value +=1 和 TrackBar1.Value = 0 註解掉
作者: tvmateiii    時間: 2020-5-1 05:37 PM

TrackBar1.Maximum = 60
你應該是上面的值沒有去設定到~
所以才會這樣~
作者: 復仇刺    時間: 2020-5-1 11:21 PM

tryit244178 發表於 2020-5-1 02:26 PM
你設定TrackBar1.Value的值時,會觸發 TrackBar.ValueChanged事件把這段程式碼改寫在ValueChanged事件裡 ...

Public Class Timer
    'Timer控制項範例
    Dim min As Integer = 0 : Dim sec As Integer
    Dim a As Integer = 0 : Dim b As Integer = 0

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Timer1.Interval = 10
        b += 1
        If b = 10 Then
            b = 0
            a += 1
            If a = 10 Then
                a = 0
                sec += 1
                TrackBar1.Value += 1
            End If
        End If
        TextBox1.Text = min & "分" & sec & "秒" & a & b

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Enabled = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Timer1.Enabled = False
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        min = 0 : sec = 0 : a = 0 : b = 0 : TrackBar1.Value = 0
        TextBox1.Text = min & "分" & sec & "秒" & a & b
    End Sub

    Private Sub TrackBar1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar1.ValueChanged
        If TrackBar1.Value = 60 Then
            TrackBar1.Value = 0
            If sec = 60 Then
                sec = 0
                min += 1
            End If
        End If
    End Sub
End Class
作者: tryit244178    時間: 2020-5-2 10:58 AM

復仇刺 發表於 2020-5-1 11:21 PM
Public Class Timer
    'Timer控制項範例
    Dim min As Integer = 0 : Dim sec As Integer

如果還是不行的話,你可以去掉TrackBar1_ValueChanged()
然後把Timer1_Tick()改為
  1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

  2.    Timer1.Interval = 10
  3.    b += 1
  4.    If b = 10 Then
  5.       b = 0
  6.       a += 1
  7.       If a = 10 Then
  8.          a = 0
  9.          sec += 1
  10.          If sec = 60 Then
  11.             sec = 0
  12.             min += 1
  13.          End If
  14.       End If
  15.    End If
  16.    TrackBar1.Value = sec
  17.    TextBox1.Text = min & "分" & sec & "秒" & a & b

  18. End Sub
複製代碼

作者: tyfbbs    時間: 2020-5-2 06:31 PM

TrackBar1的Maximun等於60
修改一下判斷式就可以
TrackBar1.Value += 1
If TrackBar1.Value = 60 Then

改成
If TrackBar1.Value < 60 Then
    TrackBar1.Value += 1
Else
    TrackBar1.Value = 0
    '以下程式碼自行複製
End if
作者: 復仇刺    時間: 2020-5-2 06:43 PM

tryit244178 發表於 2020-5-2 10:58 AM
如果還是不行的話,你可以去掉TrackBar1_ValueChanged()
然後把Timer1_Tick()改為

先感謝師傅0.0.......
為什執行後滑鼠右鍵要改變顏色 不給右鍵....點了沒反應....是系統有問題還是....




Public Class ContextMenu
    '功能:快顯功能表範例
    Private Sub 綠色ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 綠色ToolStripMenuItem.Click
        RichTextBox1.ForeColor = Color.Green '選擇「綠色」前景顏色變更

    End Sub

    Private Sub 黑色ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 黑色ToolStripMenuItem.Click
        RichTextBox1.ForeColor = Color.Black '選擇「黑色」前景顏色變更
    End Sub

    Private Sub 黃色ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 黃色ToolStripMenuItem.Click
        RichTextBox1.ForeColor = Color.Yellow '選擇「黃色」前景顏色變更

    End Sub

    Private Sub 紅色ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 紅色ToolStripMenuItem.Click
        RichTextBox1.ForeColor = Color.Red '選擇「紅色」前景顏色變更
    End Sub
End Class
[attach]131528007[/attach]

[attach]131528009[/attach]

[attach]131528015[/attach]



作者: 復仇刺    時間: 2020-5-2 10:06 PM

tyfbbs 發表於 2020-5-2 06:31 PM
TrackBar1的Maximun等於60
修改一下判斷式就可以
TrackBar1.Value += 1

已改好
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Interval = 10
        b += 1
        If b = 10 Then
            b = 0
            a += 1
            If a = 10 Then
                a = 0
                sec += 1
                If TrackBar1.Value < 60 Then
                    TrackBar1.Value += 1
                Else
                    TrackBar1.Value = 0
                    '以下程式碼自行複製
                End If
                If sec = 60 Then
                    sec = 0
                    min += 1
                End If
            End If
        End If
        TextBox1.Text = min & "分" & sec & "秒" & a & b

    End Sub
作者: tryit244178    時間: 2020-5-2 11:06 PM

本帖最後由 tryit244178 於 2020-5-2 11:07 PM 編輯
復仇刺 發表於 2020-5-2 06:43 PM
先感謝師傅0.0.......
為什執行後滑鼠右鍵要改變顏色 不給右鍵....點了沒反應....是系統有問題還是....

你有把快顯選單設到 Form1.ContextMenuStript 裡嗎?
[attach]131531688[/attach]


作者: 復仇刺    時間: 2020-5-3 12:44 AM

tryit244178 發表於 2020-5-2 11:06 PM
你有把快顯選單設到 Form1.ContextMenuStript 裡嗎?

多謝師傅了@.@
設定後可以正常執行右鍵也可以更改顏色了@@感恩@@
[attach]131532189[/attach]

[attach]131532190[/attach]





作者: tryit244178    時間: 2020-5-3 01:52 AM

復仇刺 發表於 2020-5-3 12:44 AM
多謝師傅了@.@
設定後可以正常執行右鍵也可以更改顏色了@@感恩@@

RichTextBox裡也有那個屬性,設在那就只會出現在RichTextBox之上
作者: tryit244178    時間: 2020-5-4 11:49 PM

本帖最後由 tryit244178 於 2020-5-5 12:40 AM 編輯

閒著沒事,來點別的寫法
  1. Public Class Form1
  2.     Private microSecond As New Limit(10), milliSecond As New Limit(10)
  3.     Private sec As New Limit(60), min As New Limit(60)
  4.     Private carry10 As New Carrier(10)
  5.     Private carry60 As New Carrier(60)

  6.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  7.         milliSecond = 0
  8.         microSecond = 0
  9.         sec = 0
  10.         min = 0
  11.     End Sub

  12.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  13.         Timer1.Enabled = Not Timer1.Enabled
  14.     End Sub

  15.     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  16.         milliSecond += 1
  17.         carry10.CheckCarry(microSecond, milliSecond)
  18.         carry10.CheckCarry(sec, microSecond)
  19.         carry60.CheckCarry(min, sec)
  20.         Label1.Text = CUInt(min) & "分:" & CUInt(sec) & "秒:" & CUInt(microSecond) & CUInt(milliSecond)
  21.     End Sub
  22. End Class

  23. Public Class Carrier
  24.     Private _carry As UInteger

  25.     Public Sub New(ByVal carry As UInteger)
  26.         _carry = carry
  27.     End Sub

  28.     Public Sub CheckCarry(ByRef highOrder As Limit, ByRef lowOrder As Limit)
  29.         If CUInt(lowOrder) = _carry Then
  30.             lowOrder = 0
  31.             highOrder += 1
  32.         End If
  33.     End Sub
  34. End Class

  35. Public Structure Limit
  36.     Private Shared _min As UInteger
  37.     Private Shared _max As UInteger
  38.     Private _value As UInteger

  39.     Public Sub New(ByVal Max As UInteger)
  40.         _min = 0
  41.         _max = Max
  42.     End Sub

  43.     Public Sub New(ByVal Min As UInteger, ByVal Max As UInteger)
  44.         _min = Min
  45.         _max = Max
  46.     End Sub

  47.     Public Shared Widening Operator CType(ByVal l As Limit) As UInteger
  48.         Return l._value
  49.     End Operator

  50.     Public Shared Narrowing Operator CType(ByVal value As UInteger) As Limit
  51.         If value < _min Or value > _max Then
  52.             'Throw New ArgumentOutOfRangeException(value & "超出範圍")
  53.             value = 0
  54.         End If

  55.         Dim result As New Limit(_min, _max)
  56.         result._value = value
  57.         Return result
  58.     End Operator

  59.     Public Shared Operator +(ByVal first As Limit, ByVal second As Limit) As Limit
  60.         Dim result As New Limit(_min, _max)
  61.         result = first._value + second._value
  62.         Return result
  63.     End Operator
  64. End Structure
複製代碼

作者: 復仇刺    時間: 2020-5-6 02:34 PM

本帖最後由 復仇刺 於 2020-5-6 02:38 PM 編輯
tryit244178 發表於 2020-5-3 01:52 AM
RichTextBox裡也有那個屬性,設在那就只會出現在RichTextBox之上

[attach]131595390[/attach]

[attach]131595395[/attach]

[attach]131595408[/attach]
[attach]131595745[/attach]
[attach]131595747[/attach]
[attach]131595753[/attach]

痾老實說我不知道他要寫在哪個事件裡@.@"
一般我都是click兩下去找事件= ="然後寫在裡面= =

然後由於沒有範例檔= =只好自己截圖做範例的圖= =
如果照範例步驟= =感覺.....


    Private Sub ToolStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
        '不知道程式碼不知道要寫在哪個事件

            Select Case ToolBar1.Buttons.indexof(e.Button)  '這行不明= =
            Case 0
                RichTextBox1.Font = New Font("新細明體", 12, FontStyle.Bold)
            Case 1
                RichTextBox1.Font = New Font("新細明體", 12, FontStyle.Italic)
            Case 2
                RichTextBox1.Font = New Font("新細明體", 12, FontStyle.Underline)
        End Select
    End Sub
[attach]131596138[/attach]
[attach]131596139[/attach]
[attach]131596140[/attach]
[attach]131596141[/attach]

[attach]131596143[/attach]
[attach]131596144[/attach]
[attach]131596145[/attach]
[attach]131596146[/attach]
[attach]131596147[/attach]
[attach]131596149[/attach]






作者: tryit244178    時間: 2020-5-6 03:16 PM

復仇刺 發表於 2020-5-6 02:34 PM
痾老實說我不知道他要寫在哪個事件裡@.@"
一般我都是click兩下去找事件= ="然後寫在裡面= =

事件可以從最上面的下拉式選單來選,像這樣
[attach]131596467[/attach]

關於不懂的部份…
Select Case…
http://it-easy.tw/selectcase/
這種教學,比起看微軟的文件,不如去看網友大大們的教學,會比較容易懂

e.Button…
簡單的說被點過的ToolBar會透過變數e傳給ToolStrip1的ItemClicked事件

然後再透過ToolBar1.Buttons.indexof(e.Button)得到被點選的物件的索引編號
如果是0就執行…如果是1就執行…如果是2就執行…


最後…如果貼圖超過10張…看的人好像會被扣點數…
回文的我不知道會不會扣就是…





歡迎光臨 伊莉討論區 (http://a04.eyny.com/) Powered by Discuz!