Results 1 to 6 of 6

Thread: Contains (similar function exists?)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2010
    Posts
    23

    Contains (similar function exists?)

    Okay, so i have a listbox a textbox and a mediaplayer

    A timer with the interval of "1" changes the textbox to the current positon (1.????)

    BUT the listbox contains 1.????????? (differnet length of textbox), anyway to add/shorten the string, substring doesn't work.

    Also, when i check with the timer, even if it does match it deosnt tell me it matches

    Code:
     If ListBox2.Items.Contains(AxWindowsMediaPlayer1.Ctlcontrols.currentPosition) Then
                MsgBox("hello")
            End If
    EX.
    Listbox item : 1.010000
    media player : 1.01

    i need to make the media player one match
    Also whent it matches it doesnt tell me, any help?

    btw pic attached
    Attached Images Attached Images  
    Last edited by aaro4130; Jan 27th, 2012 at 08:05 PM.

  2. #2
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Contains (similar function exists?)

    anyway to add/shorten the string, substring doesn't work.
    Use the Math.Round() method to shorten a "string" (really a number value as far as I can tell, but after rounding you can parse or cast it to a string which gets appended to the textbox afterwards.

    I am confused on your question about matching positions and trying to match the position itself with some variable you have visible inside a listbox control though...

    What exactly is this application designed to do?
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Contains (similar function exists?)

    try this:

    vb Code:
    1. If ListBox2.Items.Contains(AxWindowsMediaPlayer1.Ctlcontrols.currentPosition.ToString("f6")) Then
    2.     MsgBox("hello")
    3. End If

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2010
    Posts
    23

    Re: Contains (similar function exists?)

    Quote Originally Posted by .paul. View Post
    try this:

    vb Code:
    1. If ListBox2.Items.Contains(AxWindowsMediaPlayer1.Ctlcontrols.currentPosition.ToString("f6")) Then
    2.     MsgBox("hello")
    3. End If
    Hmm that covers the string length but when it matches it doesnt run the code :/

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Contains (similar function exists?)

    One idea is to populate your Listbox so that the long format is display with the short format stored but not shown by using a DataTable as the DataSource to a BindingSource which is the DataSource for the Listbox. When you need to locate a item i.e. 3.25 use the BindingSource Find method to set the Position in the ListBox

    Code:
    Public Class frmDemo1
        WithEvents bsSomeValues As New BindingSource
        Private Sub frmDemo1_Load( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim SomeValues As String() = _
            { _
             "1.010000", _
             "3.254367", _
             "4.000000" _
            }
    
            Dim dtValues As New DataTable()
            dtValues.Columns.Add("Display", GetType(String))
            dtValues.Columns.Add("Value", GetType(String))
    
            For Each item In SomeValues
                dtValues.Rows.Add(item, CDec(item).ToString("f2"))
                ListBox2.Items.Add(CDec(item).ToString("f2"))
            Next
    
            bsSomeValues.DataSource = dtValues
            ListBox1.DisplayMember = "Display"
            ListBox1.ValueMember = "Value"
            ListBox1.DataSource = bsSomeValues
    
            ListBox2.SelectedIndex = 1
    
        End Sub
        Private Sub Button1_Click( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles cmdTest1.Click
    
            ChangePosition()
    
        End Sub
        Private Sub ListBox2_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox2.MouseDoubleClick
            ChangePosition()
        End Sub
        Private Sub ChangePosition()
            Dim Postion = bsSomeValues.Find("Value", ListBox2.Text)
            If Postion <> -1 Then
                bsSomeValues.Position = Postion
            End If
    
        End Sub
    End Class
    This example finds a match in ListBox1 each time the position changes in ListBox2 (seebsData).
    Code:
    Public Class frmDemo1
        WithEvents bsSomeValues As New BindingSource
        WithEvents bsData As New BindingSource
        Private Sub frmDemo1_Load( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim ListValues As New List(Of String)
    
            Dim SomeValues As String() = _
            { _
             "1.010000", _
             "3.254367", _
             "4.000000" _
            }
    
            Dim dtValues As New DataTable()
            dtValues.Columns.Add("Display", GetType(String))
            dtValues.Columns.Add("Value", GetType(String))
    
            For Each item In SomeValues
                dtValues.Rows.Add(item, CDec(item).ToString("f2"))
                ListValues.Add(CDec(item).ToString("f2"))
            Next
    
            bsSomeValues.DataSource = dtValues
            ListBox1.DisplayMember = "Display"
            ListBox1.ValueMember = "Value"
            ListBox1.DataSource = bsSomeValues
    
            bsData.DataSource = ListValues
    
            ListBox2.DataSource = bsData
            ActiveControl = ListBox2
    
        End Sub
        Private Sub bsData_PositionChanged( _
            ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles bsData.PositionChanged
    
            Dim Postion = bsSomeValues.Find("Value", CStr(bsData.Current))
            If Postion <> -1 Then
                bsSomeValues.Position = Postion
            End If
        End Sub
    End Class

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2010
    Posts
    23

    Re: Contains (similar function exists?)

    GAH! None of this works D:

    The 00:01 will work, but the 0.0001 wont work

    And i made a new program and added my own items to a listbox and converted them both to integeres and it didnt work, can anybody think of a solution? :S

    also dont think im a n00b, i attached a screenshot of a previous proggy i worked on lol
    Attached Images Attached Images  

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