Member Avatar for Micheal87

Hi, I'm searching for a way that let run a event without need, that specific form to be open, and also save thing.
I explain better, the performclick will only fire if that specific form it's open, I'm searching a way to do this on background.
This is the code of where performclick will fire (thanks to a timer)

 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Try
            Dim value As Integer = maxVal
            If Not IsNumeric(Label10.Text) Then
                Label10.Text = maxVal.ToString
            End If
            Integer.TryParse(Label10.Text, value)
            If value >= 1 And value <= maxVal Then
                value -= 1
                Label10.Text = value.ToString
            Else
                If value <= 0 Then
                    Form11.Button5.PerformClick()
                    Timer1.Stop()
                    Label10.Text = maxVal.ToString
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

This is the form11's button_5 code

  Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Try
            For i As Integer = 0 To ComboBox3.Items.Count - 1
                ComboBox2.SelectedIndex = i
                Dim d As DateTime = DateTime.Now
                Dim parsedate As DateTime = ComboBox2.Items(i).ToString
                Dim d2 As DateTime = DateTime.ParseExact(parsedate, "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture)
                Dim ts As New TimeSpan((d - d2).Ticks)
                Dim dysdiff As Integer = ts.TotalDays
                Dim cbNum As Integer = 365 - dysdiff
                ComboBox3.Items(i) = cbNum.ToString
                Dim lstname As String
                lstname = ListBox1.Items(i).ToString()
                If cbNum <= 10 AndAlso cbNum >= 1 Then
                    NotifyIcon1.ShowBalloonTip(3000, "test", lstname & " have " & cbNum & " days left before will get delete", ToolTipIcon.None)
                End If
            Next
        Catch ex As Exception

        End Try
    End Sub

OK, what if you redesign this to require NO FORMs? If a form isn't open, then it doesn't exist yet so no way to process the events.

To me you are describing the old service type app. No forms required. Then again this feels like I'm one of the blind men being shown an elephant. I can't guess what your app does yet or why it needs forms.

Member Avatar for Micheal87

It's more a like to have a way that when the startup form it's open notifications can be show without problem, or user need to open that specific window (form11) before time run out to update values and old post.
I was also thinking at least, to auto open-close that form, to achieve this, only if the statement is true (maybe will change the range of the if statement to a more strict one like 5 to 1)

Sorry I wish to clarify my comment about events. An app with no forms can process events, pop up notifications and more. But there are no "form" events.

Since I only know what you reveal and not what this app needs to do, any guess may be a bad one.

Member Avatar for Micheal87

Thank you, also I'll try to explain better myself.
This program need to send notification about if a "literally" stored value that user inserted previously get old with time passing by, if no notification are sent the user can't know when a stored value satisty the if statement.
The problem there is that the form where is the stored values, notification and if statement, are in one only form. (Form 11)
The startup form is where timer begin to tick, when reach 0, the optimal way to save all changed values, is to do this, from the startup form, because the only way to store value changed are only when the other form (form11) is in the closing part.
If some other info are needed. I'm here.

"only way to store value changed are only when the other form (form11) is in the closing part."

It's your code and design. You can add code to store values when you wish. It's your design and code.

For example, the values could exist in some array and without using any forms, the timer can fire, code runs to look at the values and take action as you see fit.

I can imagine if a new programmer only worked with forms, they may only think in ways that forms work and not as an app that doesn't use forms.

One way to share data between, for example, two forms can be writing and reading a file in disk. Here is a tutorial if needed.

commented: Next thing you know we'll talk about SQL, etc. Good direction. +16

For example, to write a text file just do:

    Sub WriteFile()
        Dim file As System.IO.StreamWriter
        file = My.Computer.FileSystem.OpenTextFileWriter("c:\test.txt", True)
        file.WriteLine("Here is the first string.")
        file.Close()
    End Sub

So, writing to a text file in Button5_Click could be:

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Try
            Dim file As System.IO.StreamWriter
            file = My.Computer.FileSystem.OpenTextFileWriter("c:\test.txt", True)
            For i As Integer = 0 To ComboBox3.Items.Count - 1
                ComboBox2.SelectedIndex = i
                Dim d As DateTime = DateTime.Now
                Dim parsedate As DateTime = ComboBox2.Items(i).ToString
                Dim d2 As DateTime = DateTime.ParseExact(parsedate, "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture)
                Dim ts As New TimeSpan((d - d2).Ticks)
                Dim dysdiff As Integer = ts.TotalDays
                Dim cbNum As Integer = 365 - dysdiff
                ComboBox3.Items(i) = cbNum.ToString
                Dim lstname As String
                lstname = ListBox1.Items(i).ToString()
                If cbNum <= 10 AndAlso cbNum >= 1 Then
                    file.WriteLine("test", lstname & " have " & cbNum & " days left before will get delete", ToolTipIcon.None)
                End If
            Next
            file.Close()
        Catch ex As Exception

        End Try
        Exit Sub
    End Sub

Once a form has saved the text to a file, another form can read the data.

A better aproach would be:

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Try
            Dim file As System.IO.StreamWriter
            Dim path As String = Application.StartupPath + "\test.txt"
            file = My.Computer.FileSystem.OpenTextFileWriter(path, False)
            For i As Integer = 0 To ComboBox3.Items.Count - 1
                ComboBox2.SelectedIndex = i
                Dim d As DateTime = DateTime.Now
                Dim parsedate As DateTime = ComboBox2.Items(i).ToString
                Dim d2 As DateTime = DateTime.ParseExact(parsedate, "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture)
                Dim ts As New TimeSpan((d - d2).Ticks)
                Dim dysdiff As Integer = ts.TotalDays
                Dim cbNum As Integer = 365 - dysdiff
                ComboBox3.Items(i) = cbNum.ToString
                Dim lstname As String
                lstname = ListBox1.Items(i).ToString()
                If cbNum <= 10 AndAlso cbNum >= 1 Then
                    file.WriteLine("test" + lstname + " have " + cbNum.ToString + " days left before will get delete")
                End If
            Next
            file.Close()
        Catch ex As Exception

        End Try
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.