Results 1 to 3 of 3

Thread: timer and buttons problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2010
    Posts
    18

    timer and buttons problem

    hi, I've two questions.. first I've a timer in my wpf program it work fine but I cann't stop it. here is the code:

    vb Code:
    1. Public Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button3.Click
    2.         Button2.Visibility = Windows.Visibility.Hidden
    3.         Button3.Visibility = Windows.Visibility.Hidden
    4.         ListBox2.Visibility = Windows.Visibility.Hidden
    5.         ListBox3.Visibility = Windows.Visibility.Hidden
    6.         Label1.Visibility = Windows.Visibility.Hidden
    7.         ListBox1.Visibility = Windows.Visibility.Hidden
    8.         Button5.Visibility = Windows.Visibility.Visible
    9.         'obter nome imagens
    10.         Dim Sqlimg As String = "select id,CodProd,Imagem from imagem"
    11.         'obter caracteristicas do produto da imagem
    12.         Dim sqlprod As String = "select CodProd,descricao1,TipoProd,familia,coleccao from produto where CodProd= "
    13.         Dim conexaoBD As OleDbConnection
    14.         Dim dsdados As DataSet
    15.         Dim ligar As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\shroom.mdb"
    16.         Dim dt As DataTable
    17.         conexaoBD = New OleDbConnection(ligar)
    18.         Dim adaptadorImg As OleDbDataAdapter = New OleDbDataAdapter(Sqlimg, conexaoBD)
    19.         dsdados = New DataSet
    20.         adaptadorImg.Fill(dsdados, "imagem")
    21.         dt = dsdados.Tables("imagem")
    22.  
    23.  
    24.  
    25.         Dim NumImg As Integer = dt.Rows.Count
    26.         ReDim ArrayImg(NumImg)
    27.  
    28.         Dim linha As DataRow
    29.         Dim x As Integer = 0
    30.  
    31.         For Each linha In dt.Rows
    32.             ArrayImg(x) = dt.Rows(x).Item(2).ToString
    33.             x = x + 1
    34.         Next
    35.  
    36.         ' listar imagens
    37.  
    38.         If NumImg > 0 Then
    39.             Dim Timer = New DispatcherTimer()
    40.             Timer.Interval = New TimeSpan(0, 0, 2)
    41.             Timer.IsEnabled = True
    42.             Timer.Start()
    43.             AddHandler Timer.tick, AddressOf timer_tick
    44.            
    45.         Else
    46.             MsgBox("Não tem imagens")
    47.         End If
    48.        
    49.     End Sub
    50.     Public Sub timer_tick(ByVal send As System.Object, ByVal e As System.EventArgs)
    51.         Try
    52.             Dim imgBrush As New ImageBrush()
    53.             Dim appstart As String = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)
    54.             imgBrush.ImageSource = New BitmapImage(New Uri(appstart & "\img\" & ArrayImg(ArrayNum)))
    55.             Button4.Background = imgBrush
    56.  
    57.             Button4.Visibility = Windows.Visibility.Visible
    58.             ArrayNum = ArrayNum + 1
    59.             If ArrayNum = UBound(ArrayImg) Then
    60.                 ArrayNum = 0
    61.             End If
    62.  
    63.         Catch
    64.  
    65.  
    66.  
    67.         End Try
    68.     End Sub


    the second problem is that I'm creating button by code, to show image, but I cann't make them disapear...

    vb Code:
    1. Private Sub ListBox4_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ListBox4.SelectionChanged
    2.  
    3.  
    4.         Try
    5.  
    6.             Dim sqlser As String = "select distinct descricao1,codProd from produto where codProd in (select codProd from produto where modelo= '" & ListBox4.SelectedValue.ToString & "')"
    7.             Dim conexaoBD As OleDbConnection
    8.             Dim dsdados As DataSet
    9.             Dim ligar As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\shroom.mdb"
    10.             Dim dt As DataTable
    11.             conexaoBD = New OleDbConnection(ligar)
    12.  
    13.             Dim adaptadorPro As OleDbDataAdapter = New OleDbDataAdapter(sqlser, conexaoBD)
    14.             dsdados = New DataSet
    15.             adaptadorPro.Fill(dsdados, "produto")
    16.             dt = dsdados.Tables("produto")
    17.             Label2.Content = "Descrição: " & dt.Rows(0).Item(0).ToString
    18.             Label3.Content = "Tipo: " & ListBox1.SelectedValue.ToString
    19.             Label4.Content = "Formato: " & ListBox2.SelectedValue.ToString
    20.             Label5.Content = "Serie: " & ListBox3.SelectedValue.ToString
    21.  
    22.  
    23.             Dim sqlimg As String = "select distinct Imagem from imagem where codProd='" & dt.Rows(0).Item(1).ToString & "'"
    24.             Dim dsdadosImg As DataSet
    25.             Dim dtImg As DataTable
    26.             conexaoBD = New OleDbConnection(ligar)
    27.  
    28.             Dim adaptadorImg As OleDbDataAdapter = New OleDbDataAdapter(sqlimg, conexaoBD)
    29.             dsdadosImg = New DataSet
    30.             adaptadorImg.Fill(dsdadosImg, "imagem")
    31.             Dim linhas As DataRow
    32.             dtImg = dsdadosImg.Tables("imagem")
    33.             Dim x As Integer = 0
    34.             For Each linhas In dtImg.Rows
    35.                 Dim Btimg As New Button
    36.                 Dim imgBrush As New ImageBrush()
    37.                 Dim appstart As String = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)
    38.                 imgBrush.ImageSource = New BitmapImage(New Uri(appstart & "\img\" & dtImg.Rows(x).Item(0)))
    39.                 Btimg.Background = imgBrush
    40.                 Btimg.Width = 100
    41.                 Btimg.Height = 100
    42.                 Btimg.Visibility = Windows.Visibility.Visible
    43.                 StackPanelimg.Children.Add(Btimg)
    44.                 x = x + 1
    45.  
    46.             Next
    47.  
    48.         Catch ex As Exception
    49.  
    50.         End Try
    51.     End Sub

    anyone ????

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: timer and buttons problem

    Well you need to have some way of referencing the timer from wherever you want to stop it from. The easiest way would be to just declare the timer at class level (you can also then declare it WithEvents which means you dont have to manually hook up the event handler for the Tick event) then you can just call the Stop method from wherever you want.

    By the way there is a much easier way to get the directory that your app is running from instead of doing this:
    Code:
    Dim appstart As String = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)
    just do this:
    Code:
    Dim appStart As String = My.Application.Info.DirectoryPath
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2010
    Posts
    18

    Re: timer and buttons problem

    can you give me an example ?

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