Member Avatar for Micheal87

Hi, I like to ask for a clarification about it. I have combobox.items(i) that go trough a for next loop for each index I need to check if its content (number) will be >=1 or <=10. Now I tried this one, but I know where the error might be, the + " Days" combination that it's a string and when checking in the if statement the text it give me an error of course. It's there any tip to check only a number in a integer + string combination on an if?
In case I can delete the + " days" and put it in an label so the combobox will be filled only with the integer part, and the if can work (if I'm right)
This is the part of code

   ComboBox3.Items(i) = (365 - dysdiff).ToString + " Days"
            If ComboBox3.Text.ToString >= 1 <= 10 & " Days" Then
                MsgBox("post old", MessageBoxButtons.OK)
            Else
            End If
        Next

I would definitely try putting the "Days" constant string into a Label next to the ComboBox in question. As it is, you'd need to parse the string into the part with the numerical value, and the part with the "Days" substring, and you would need to do it any time you want the numeric value. It can be done, but it isn't really worth the effort, and in any case the Label would probably be clearer for the user.

Not to be too much of a nuisance about it, but it might help if we knew more about the program as a whole, in case there might be a better solution which isn't obvious just from this one small piece of the puzzle.

Member Avatar for Micheal87

Sure, this is the button5_Click

  Public Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        For i = 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 dysdiff As Integer = (d - d2).Days
            ComboBox3.Items(i) = (365 - dysdiff).ToString
            If ComboBox3.Text.ToString >= 1 <= 10 Then
                MsgBox("post old", MessageBoxButtons.OK)
            Else
            End If
        Next
    End Sub

This button 5 is linked with a timer on another form (Also thanks for the help with it)
timer1.tick sub

 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim value = CInt(Label10.Text)
        value = CByte_Saturate_Range(Label10.Text, 0, 30)
        If value > 0 Then
            value = value - 1
        Else
            Form11.Button5.PerformClick()
            Timer1.Stop()
        End If
        Label10.Text = CStr(value)

    End Sub

function of sub

Public Function CByte_Saturate_Range(data As String, min As Byte, max As Byte) As Byte
    Dim value As Byte
    Dim swap As Byte

    If min = max Then
        Return min
    End If

    If min > max Then
        swap = min
        min = max
        max = swap
    End If
    ' convert to a Short first, then force it to a positive absolute value
    ' before converting it to Byte
    value = CByte(Math.Abs(CShort(data)))
    If min > value Then
        value = min
    ElseIf value > max Then
        value = max
    End If
    Return value
End Function
End Class

Also in the end I removed all + " days" combination from the code, created a label and called days, that will be visible when added at least 1 item and invisible when it's 0

Be aware that all string or numeric comparisons must be between the same type:

  1. Number compared to Number or
  2. String compared to String.
    Never Number compared to String, or String compared to Number.

Line# 9:

If ComboBox3.Text.ToString >= 1 <= 10 Then 

compares ComboBox3.Text.ToString, i.e. a String, with numbers (also >= 1 <= 10 is a syntax error and, as ComboBox3.Text is already a String, the trailing .ToString is not necessary).

Therefore consider converting line#9 to a valid comparison:

 Public Sub Button5_Click(sender As Object, e As 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 dysdiff As Integer = (d - d2).Days
                ComboBox3.Items(i) = (365 - dysdiff).ToString
                Dim cbNum As Integer
                Integer.TryParse(ComboBox3.Text, cbNum)
                ' Compare pairs of numbers: '
                ' Numeric cbNum compared to number 1 '
                ' Numeric cbNum compared to number 10 '
                If cbNum >= 1 AndAlso cbNum <= 10 Then
                    MsgBox("post old", MessageBoxButtons.OK)
                End If
            Next
        Catch ex As Exception

        End Try
    End Sub
Member Avatar for Micheal87

Thank you for explanation, I was getting much confusing about it.
I tried your code, seem that value will be updated like regulally, but message box doesn't show up when timer end, also a strange thing happen if I select the only value that will be right for the if statement, and press directly the button, messagebox appear n times for how many items are stored, of couse this is only for testing, for see if msgbox show up, but the problem is why when only selected msgbox appear and when timer that will fire a performclick to this button doesn't showing up?
EDIT: I figured out why msgbox will fire, and also why is repeated n times for how many items are stored, because the if statement will be already trueand cycle continues on. But yea it's just for testing directly clicking to the button.

Perhaps, you may try my guess of what you want:

    Const maxVal As Integer = 30
    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
                    Button5.PerformClick()
                    Timer1.Stop()
                    Label10.Text = maxVal.ToString
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub






    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Try
            For i As Integer = 0 To ComboBox2.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
                If -10 <= cbNum AndAlso cbNum <= -1 Then
                    ComboBox3.Items(i) += " old post"
                End If
            Next
        Catch ex As Exception

        End Try
    End Sub
Member Avatar for Micheal87

I tested, your code, and this is my result in what I noticed:
Program start and timer too, and it's ok also when windows is open, where the button is, timer fire up the for cycle.
Also I changed the line

 For i As Integer = 0 To ComboBox2.Items.Count - 1

with

  For i As Integer = 0 To ComboBox3.Items.Count - 1

Value changed, of course no message box because is not in the code, but if I see correctly this line

  If -10 <= cbNum AndAlso cbNum <= -1 Then
       ComboBox3.Items(i) += " old post"

check if the number on the current for next index of the combobox satisfy the if statement, if true, the combination part add old post to that currently item.
In this part doesn't add the old post string to the combobox item(i). I don't know if I missing something here but it's there a reason why number are negative? the number 10 and 1 doesn't need to be without - ?

Member Avatar for Micheal87

EDIT: I changed the last line of if whit this and it's working

 If cbNum <= 10 AndAlso cbNum >= 1 Then
                    ComboBox3.Items(i) += " old post"
                End If

now if the statement is true then add the old post to the number.
Also thank you for help :)

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.