Solved

Enable button based on event

Posted on 2013-11-24
8
272 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
  • 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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now