Solved

Enable button based on event

Posted on 2013-11-24
8
280 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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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