Results 1 to 22 of 22

Thread: [RESOLVED] [2008] Timer help!

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Resolved [RESOLVED] [2008] Timer help!

    Hi! I want to want to enable/disable Timer1.Tick when Form1 loads, and i want to to depend to the checkstate of a CheckBox in another form. This is the code I'm using:

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Customize.CheckBox4.CheckState = CheckState.Checked Then
                Timer1.Enabled = False
            ElseIf Customize.CheckBox4.CheckState = CheckState.Unchecked Then
                Timer1.Enabled = True
            End If
        
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If Customize.CheckBox7.CheckState = CheckState.Checked Then
                If Me.Opacity <= My.Settings.DefaultOpacity Then
                    Me.Opacity = Me.Opacity + 0.1
                End If
            ElseIf Customize.CheckBox7.CheckState = CheckState.Unchecked Then
                If Me.Opacity <= 1 Then
                    Me.Opacity = Me.Opacity + 0.1
                End If
            End If
        End Sub
    Timer1.Enabled = True

    I don't know what's wrong, but it doesn't respond to the Checkbox4.CheckedState. What's wrong with the code?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Timer help!

    First up, I'd suggest not using the CheckState property unless your CheckBox is actually using three states, i.e. Checked, Unchecked and Indeterminate. If you're only using two states then just use the Checked property, which will be True or False.

    As for the question, the way you're using Customize there means that you're referring to the default instance of that class. It may well be that the instance you're actually checking and unchecking the box on is not the default instance. Also, even if you do display the default instance, as soon as you dispose it a new default instance will be created, so you're still referring to a different object.

    I would suggest that you don't refer to that CheckBox at all. Declare a global variable and refer to that in Form1. When you display your Customize form you can then edit the value of that global variable. Also, if you use a property instead of a field, you can raise an event when the value changes. That way Form1 can simply handle that event to know when the value has changed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Timer help!

    When you say declare a global variable, do you mean:

    Code:
    Public timer_val as type (string, integer, or ...?)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Timer help!

    You need a variable or property declared somewhere that it can be accessed by both forms. It could be in a module, a Shared member in a class, in the ApplicationEvents.vb file and accessible via My.Application or, if you want the value persisted between sessions, in My.Settings. In all those cases one form can set the value and another can get it without the two ever having to know the other exists.

    And yes, that code snippet you posted is a variable declaration.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Timer help!

    Yes, but the type for the variable should be what? I tried with integer, but it did the same thing >_<.

    I tried this:

    Code:
    Public Sub Form1_load
    If variable = 1 then
    Timer1.Interval = 100
    ElseIf variable = 0 then
    Timer1.Interval = 1
    End If
    It KIND OF works. You can still notice the change in the opacity level of the main form, but not so much.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Timer help!

    The type of the value you want to assign to it dictates the type of the variable. What do you want to assign to it? Either the CheckState or, if you follow my advice, Checked property of the CheckBox on the Customize form. What type is the property you're going to use? That's the type you need to declare the variable as.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Timer help!

    I followed your advice and changed it to Checked and the variable to Boolean, right? But it still does the same thing T_T

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Timer help!

    Can you post the code where you've declared this global variable, where you're setting it in the Customize form and where you're getting it Form1?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Timer help!

    I followed your advice and changed it to Checked, so the variable type is Boolean, right?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Timer help!

    Quote Originally Posted by tassa
    I followed your advice and changed it to Checked, so the variable type is Boolean, right?
    Right.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Timer help!

    I tried that... And it doesn't disable Timer1. It still performs Timer1_tick.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Timer help!

    Please read post #8.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Timer help!

    Oh, sorry. here it is:

    Code:
    Imports System.IO
    Public Class Customize
        Public timer As Integer = 0
        Private Sub Customize_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Label1.Text = Form1.Opacity.ToString
            TrackBar1.Value = Form1.Opacity
        End Sub
    
        Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
            If CheckBox1.Checked = True Then
                Form1.FormBorderStyle = 0
                My.Settings.DefaultCustomBorder = 0
                My.Settings.DefaultCheck1 = True
            ElseIf CheckBox1.Checked = False Then
                Form1.FormBorderStyle = 4
                My.Settings.DefaultCustomBorder = 4
                My.Settings.DefaultCheck1 = False
            End If
        End Sub
    
        Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
            If CheckBox2.Checked = True Then
                PrintSettings.Enabled = False
                My.Settings.DefaultPrinterSettings = False
                My.Settings.DefaultCheck2 = True
            ElseIf CheckBox2.Checked = False Then
                PrintSettings.Enabled = True
                My.Settings.DefaultPrinterSettings = True
                My.Settings.DefaultCheck2 = False
            End If
        End Sub
    
        Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
            If CheckBox3.Checked = True Then
                With Form1
                    .Button1.Visible = False
                    .Button2.Visible = False
                    .Button3.Visible = False
                End With
                With My.Settings
                    .DefaultButton1 = False
                    .DefaultButton2 = False
                    .DefaultButton3 = False
                End With
                My.Settings.DefaultCheck3 = True
            ElseIf CheckBox3.Checked = False Then
                With Form1
                    .Button1.Visible = True
                    .Button2.Visible = True
                    .Button3.Visible = True
                End With
                With My.Settings
                    .DefaultButton1 = True
                    .DefaultButton2 = True
                    .DefaultButton3 = True
                End With
                My.Settings.DefaultCheck3 = False
            End If
        End Sub
    
        Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
            If CheckBox4.Checked = True Then
                timer = 1
                My.Settings.DefaultCheck4 = True
            ElseIf CheckBox4.Checked = False Then
                My.Settings.DefaultCheck4 = False
                timer = 0
            End If
        End Sub
    
        Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
            If CheckBox5.Checked = True Then
                CheckBox6.Checked = False
                CheckBox6.Enabled = False
                Dim key As Microsoft.Win32.RegistryKey
                key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
                key.SetValue("To-Do List", Application.ExecutablePath)
                My.Settings.DefaultCheck5 = True
            ElseIf CheckBox5.Checked = False Then
                CheckBox6.Enabled = True
                My.Settings.StartMinimized = False
                Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue("To-Do List", False)
                My.Settings.DefaultCheck5 = False
            End If
        End Sub
    
        Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
            If CheckBox5.Checked = True Then
                My.Settings.DefaultCheck6 = True
                My.Settings.ShowInTaskbar = True
            ElseIf CheckBox5.Checked = False Then
                If CheckBox6.Checked = True Then
                    My.Settings.DefaultCheck6 = True
                    My.Settings.ShowInTaskbar = True
                ElseIf CheckBox6.Checked = False Then
                    My.Settings.DefaultCheck6 = False
                    My.Settings.ShowInTaskbar = False
                End If
            End If
        End Sub
    
        Private Sub CheckBox7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox7.CheckedChanged
            If CheckBox7.Checked = True Then
                TrackBar1.Enabled = True
                My.Settings.DefaultCheck7 = True
                My.Settings.DefaultOpacity2 = TrackBar1.Value
            ElseIf CheckBox7.Checked = False Then
                TrackBar1.Enabled = False
                My.Settings.DefaultCheck7 = False
                My.Settings.DefaultOpacity = 1
            End If
        End Sub
    
        Private Sub TrackBar1_Scroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
            Me.Opacity = 0.1 + TrackBar1.Value / 100
            Label1.Text = Me.Opacity.ToString
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If CheckBox7.Checked = True Then
                Form1.Opacity = 0.1 + TrackBar1.Value / 100
            ElseIf CheckBox7.Checked = False Then
                Form1.Opacity = 1
            End If
            My.Settings.DefaultOpacity = 0.1 + TrackBar1.Value / 100
            If CheckBox6.Checked = True Then
                Form1.ShowInTaskbar = True
            ElseIf CheckBox6.Checked = False Then
                Form1.ShowInTaskbar = False
            End If
            Form1.Focus()
            Me.Close()
        End Sub
    
    End Class
    Code:
    Imports System.IO
    Imports System.Drawing.Printing
    Imports System.Drawing
    Imports System.Windows.Forms
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If My.Settings.FirstRun = True Then
                MsgBox("Welcome to To-Do List!", MsgBoxStyle.Information, "Welcome")
                My.Settings.FirstRun = False
            End If
            If My.Settings.StartMinimized = True Then
                Me.WindowState = FormWindowState.Minimized
            End If
            Me.ToolTip1.ReshowDelay = 10
            Me.ToolTip1.InitialDelay = 1000
            'Handles fade-in effects
            If Customize.timer = True Then
                Timer1.Enabled = False
            ElseIf Customize.timer = False Then
                Timer1.Interval = 1
            End If
            'Settings from Customize Form
            With Me
                .ListBox1.ForeColor = My.Settings.DefaultColor
                .FormBorderStyle = My.Settings.DefaultCustomBorder
                .Button1.Visible = My.Settings.DefaultButton1
                .Button2.Visible = My.Settings.DefaultButton2
                .Button3.Visible = My.Settings.DefaultButton3
                .Size = My.Settings.DefaultSize
                .ShowInTaskbar = My.Settings.ShowInTaskbar
            End With
            With Customize
                .CheckBox1.Checked = My.Settings.DefaultCheck1
                .CheckBox2.Checked = My.Settings.DefaultCheck2
                .CheckBox3.Checked = My.Settings.DefaultCheck3
                .CheckBox4.Checked = My.Settings.DefaultCheck4
                .CheckBox5.Checked = My.Settings.DefaultCheck5
                .CheckBox6.Checked = My.Settings.DefaultCheck6
                .CheckBox7.Checked = My.Settings.DefaultCheck7
            End With
            PrintSettings.Enabled = My.Settings.DefaultPrinterSettings
            'Settings end here
            TextBox1.Focus()
            ListBox1.Items.Clear()
            Dim i As Integer = 0
            Do
                Dim new_value As String = GetSetting(APP_NAME, _
                    SECTION_NAME, "Item" & i.ToString())
                If new_value.Length = 0 Then Exit Do
                ListBox1.Items.Add(new_value)
                i += 1
            Loop
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If Customize.CheckBox7.Checked = True Then
                If Me.Opacity <= My.Settings.DefaultOpacity Then
                    Me.Opacity = Me.Opacity + 0.1
                End If
            ElseIf Customize.CheckBox7.Checked = False Then
                If Me.Opacity <= 1 Then
                    Me.Opacity = Me.Opacity + 0.1
                End If
            End If
        End Sub
    End Class

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Timer help!

    Didn't post all the code in Form1, because its A LOT and it doesnt really have to do with the timer.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Timer help!

    I'm afraid that that's a complete schamozzle. You're still referring to members of the Customize form directly form Form1, which was the whole point of everything that's gone before. You're supposed NOT to be doing that. Why would you need to set the Checked properties of CheckBoxes on the Customize form in Form1's Load event handler? Why can't the Customize form do that for itself?

    It looks like you're using My.Settings to store these global values. That's fine. Form1 should be referring ONLY to My.Settings. That's where it should get ALL its information from. If the user makes any changes in the Customize form then you push those changes to My.Settings, where Form1 can read them.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Timer help!

    OK, how can i make Form1 read ALL the information? In a simple way.

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Timer help!

    This is from your first post:
    Code:
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If Customize.CheckBox7.CheckState = CheckState.Checked Then
                If Me.Opacity <= My.Settings.DefaultOpacity Then
                    Me.Opacity = Me.Opacity + 0.1
                End If
            ElseIf Customize.CheckBox7.CheckState = CheckState.Unchecked Then
                If Me.Opacity <= 1 Then
                    Me.Opacity = Me.Opacity + 0.1
                End If
            End If
        End Sub
    We then went through a long conversation about global variables, how to declare them and what type they should be. Now this is from your most recent post:
    Code:
        Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If Customize.CheckBox7.Checked = True Then
                If Me.Opacity <= My.Settings.DefaultOpacity Then
                    Me.Opacity = Me.Opacity + 0.1
                End If
            ElseIf Customize.CheckBox7.Checked = False Then
                If Me.Opacity <= 1 Then
                    Me.Opacity = Me.Opacity + 0.1
                End If
            End If
        End Sub
    Nothing's changed. Yes, you're now testing the Checked property of the CheckBoxes instead of the CheckState, but you're still getting the values from the Customize form directly. That's EXACTLY what I've been saying NOT to do. Get those values from your global variables.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Timer help!

    I'm sorry, I just don't understand. I'm learning VB.NET as in right now. So, can you post the "fixed" code, please? I don't really understand what is it that you want me to do.
    Last edited by tassa; Jan 19th, 2009 at 01:39 AM.

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Timer help!

    You're already doing exactly what I'm saying elsewhere, e.g.
    vb Code:
    1. If My.Settings.FirstRun = True Then
    Get your values from your global variables, NOT from the Customize form. Can you see that this code:
    Code:
    If Customize.CheckBox7.Checked = True Then
    Is getting data directly from the Customize form? Do NOT do that that. Get ALL the data from your global variables.

    Obviously that particular global variable is supposed to represent the Checked property of a CheckBox on the Customize form, so you need to set the value of the global variable when the Checked property of that CheckBox changes. You do that in the Customize form itself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Timer help!

    Got it. It works now. Thanks a lot! This is what a have now:

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If My.Settings.FirstRun = True Then
                MsgBox("Welcome to To-Do List!", MsgBoxStyle.Information, "Welcome")
                My.Settings.FirstRun = False
            End If
            Me.ToolTip1.ReshowDelay = 10
            Me.ToolTip1.InitialDelay = 1000
            'Handles fade-in effect
            If My.Settings.DisableFadeIn = True Then
                Timer1.Enabled = False
                Me.Opacity = 100
            ElseIf My.Settings.DisableFadeIn = False Then
                Timer1.Enabled = False
            End If
    End Sub
    Works like a charm!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Timer help!

    Ops, sorry for the double post. Didn't know how to delete this one! Thanks again!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  22. #22
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] [2008] Timer help!

    Excellent. Well done.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width