Solved

Enable button based on event

Posted on 2013-11-24
8
277 Views
Last Modified: 2014-01-04
Hi

I need some help as I am a new user to vb.net with enabling a button based on an event. The scenario is I have a listview which populates 100 records at a time based on a access query.

I then have 2 buttons, 'btnNextPostings' and 'btnPreviousPostings' which are disabled at form_load. The idea is when the list is populated the next button becomes active which shows the next 100 postings. Think of it as navigating backwards and forwards 100 records at a time. You will see from my code, that I enable the next button 'btnNextPostings' when I populate the list.

I would be grateful if someone could help with this or point me in the direction for further help. Many thanks


Sub filllistview()
        Try
            'creatconn()
            oledbCnn.ConnectionString = My.Settings.storageConnectionString
            oledbCnn.Open()
            Dim oledbCmd As OleDbCommand = New OleDbCommand("Select TOP 100 * from Postings WHERE Customer = '" & newvar & "' ORDER BY Date DESC", oledbCnn)
            dr = oledbCmd.ExecuteReader()

            Dim tmpColor As Color = Color.Green

            'clear items in the list before populating with new values
            ListView1.Items.Clear()

            While dr.Read()

                ListView1.Items.Add(CDate(dr(4)).ToShortDateString()).UseItemStyleForSubItems = False
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(1).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(11).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(7).ToString())
                With ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(CDbl(dr(5)).ToString("C"))
                    If CDbl(dr(5)) < 0 Then
                        .ForeColor = Color.Red
                        .BackColor = Color.Gainsboro
                        '.Font = New Font(Font.FontFamily, Font.Size, FontStyle.Bold)
                    Else
                        .ForeColor = tmpColor
                    End If
                End With
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(14).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(3).ToString())


            End While

            'autosize each column in listview
            For Each col As ColumnHeader In ListView1.Columns
                col.Width = -2
            Next col

            'refresh the list with new data
            ListView1.Refresh()

            'Enable the posting previous button
            btnNextPostings.Enabled = True

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            dr.Close()
            oledbCnn.Close()
        End Try
    End Sub

Open in new window

0
Comment
Question by:peter-cooper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39672798
So you need to do 2 things

1) Enable Next button but only if there are more than 100 records in database. This means that you need to run a query first to get the count of total rows. Store this count in a class level variable and then when you populate the list, if this count is greater than 100 then enable the next button. You then also need to maintain the current page number count. When user presses the Next button, you load next 100 rows and then check if you still need to enable the Next button by checking if PageNumber*100 is less than row count.

2) You need to enable the Previous button if current page number is not 1.
0
 

Author Comment

by:peter-cooper
ID: 39672876
That is correct Codecruiser. Thanks
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39672993
Have you made a start on 1?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:peter-cooper
ID: 39673155
This is all I have so far. I am new user to vb.net. I use this after try statement. The biggest problem I have is finding a place to sart. Should it be a class, a module, or inline. So any guidance would be great. Thanks

btnNextPostings.Enabled = ListView1.Items.Count > 0
        If (ListView1.Items.Count < 10) Then
            btnNextPostings.Enabled = False
        End If

Open in new window

0
 

Author Comment

by:peter-cooper
ID: 39675811
Any further help with this please. Thanks
0
 

Author Comment

by:peter-cooper
ID: 39677316
I have tried this code but throws an error of:

The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.

   
Dim Row_Per_Page As Integer = 4
    Dim TotRows As Integer = 17
    Dim Page_Number As Integer = 2

    Dim oledbCmd As OleDbCommand = New OleDbCommand("Select TOP '" & Row_Per_Page & "' *, Count(*) As '" & TotRows & "' From [Select Top('" & TotRows & "' - (('" & Page_Number & "' - 1) * '" & Row_Per_Page & "'))From Postings Order By [Date] DESC] Order By [Date] ASC", oledbCnn)

Open in new window


I have checked and double checked but cannot find the error. can you help. Thanks
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39724699
You cannot use * with any aggregate function such as Count without using Group By clause.
0
 

Author Closing Comment

by:peter-cooper
ID: 39756555
Thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question