Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to show data by indexing from page to page (I mean like reading next previous in a single page) the correct ways?

Currently let's say I have promotions data. I store all of the promotions data in arraylist let's say, 21 promotions data. The code is on "What I have Tried" section.

The sequence is:-
1. Count how many page index will be from this total data
2. Assign all image from index 0 to max number of display on each index pages.
3. Next button will continue shows from next index of last pages max number.
4. Previous button will shows number from first continuous number of previous index page


I need to know if there is better ways than this? Currently this method works but takes too much lines of codes.

Any C# snippet or tricks will be accepted even though I wrote in VB since most of the code in my program are translated from C# for some function.

What I have tried:

VB
"Private Sub PageCount()
    If arr_promotions.Count <= 3 Then
        lngTotPage = 1
    ElseIf arr_promotions.Count > 3 And arr_promotions.Count <= 6 Then
        lngTotPage = 2
    ElseIf arr_promotions.Count > 6 And arr_promotions.Count <= 9 Then
        lngTotPage = 3
    ElseIf arr_promotions.Count > 9 And arr_promotions.Count <= 12 Then
        lngTotPage = 4
    ElseIf arr_promotions.Count > 12 And arr_promotions.Count <= 15 Then
        lngTotPage = 5
    ElseIf arr_promotions.Count > 15 And arr_promotions.Count <= 18 Then
        lngTotPage = 6
    ElseIf arr_promotions.Count > 18 And arr_promotions.Count <= 21 Then
        lngTotPage = 7
    ElseIf arr_promotions.Count > 21 And arr_promotions.Count <= 24 Then
        lngTotPage = 8
    ElseIf arr_promotions.Count > 24 And arr_promotions.Count <= 27 Then
        lngTotPage = 9
    ElseIf arr_promotions.Count > 27 And arr_promotions.Count <= 30 Then
        lngTotPage = 10
    ElseIf arr_promotions.Count > 30 And arr_promotions.Count <= 33 Then
        lngTotPage = 11
    ElseIf arr_promotions.Count > 33 And arr_promotions.Count <= 36 Then
        lngTotPage = 12
    ElseIf arr_promotions.Count > 36 And arr_promotions.Count <= 39 Then
        lngTotPage = 13
    ElseIf arr_promotions.Count > 39 And arr_promotions.Count <= 42 Then
        lngTotPage = 14
    ElseIf arr_promotions.Count > 42 And arr_promotions.Count <= 45 Then
        lngTotPage = 15
    ElseIf arr_promotions.Count > 45 And arr_promotions.Count <= 48 Then
        lngTotPage = 16
    ElseIf arr_promotions.Count > 48 And arr_promotions.Count <= 51 Then
        lngTotPage = 17
    ElseIf arr_promotions.Count > 51 And arr_promotions.Count <= 54 Then
        lngTotPage = 18
    ElseIf arr_promotions.Count > 54 And arr_promotions.Count <= 57 Then
        lngTotPage = 19
    ElseIf arr_promotions.Count > 57 And arr_promotions.Count <= 60 Then
        lngTotPage = 20
    End If
End Sub

//MAX PROMO IS 3 WHICH MEAN 3 IMAGES IN EACH PAGES
VB
Private Sub UpdatePromotionsInfo()
	Dim i As Integer = 0
	Dim intInfoCnt As Integer = 0
	Dim tmpIdx As Integer = 0
	Dim x As Integer = 0
	Try
		intInfoCnt = arr_promotions.Count
		HideAllBox()
		If intInfoCnt <= (intMaxPromo + 1) Then
			For i = 0 To intInfoCnt - 1
				x = i + 1
				Select Case x
					Case 1
						FillContent(i, id_1, img1)
					Case 2
						FillContent(i, id_2, img2)
					Case 3
						FillContent(i, id_3, img3)
				End Select
				tmpIdx = tmpIdx + 1
			Next
			intStartPageIdx = 0
			intLastPageIdx = intMaxPromo
			intCurrntPageIdx = intLastPageIdx
			navi_next.Visibility = Visibility.Collapsed
			navi_previous.Visibility = Visibility.Collapsed
		Else
			tmpIdx = 1
			For i = 0 To intMaxPromo
				Select Case tmpIdx
					Case 1
						FillContent(i, id_1, img1)
					Case 2
						FillContent(i, id_2, img2)
					Case 3
						FillContent(i, id_3, img3)
				End Select
				tmpIdx = tmpIdx + 1
			Next
			intStartPageIdx = 0
			intLastPageIdx = intMaxPromo
			intCurrntPageIdx = intLastPageIdx
			navi_next.Visibility = Visibility.Visible
			navi_previous.Visibility = Visibility.Collapsed
		End If
		tb_page.Text = lngCurrPage.ToString & " / " & lngTotPage.ToString
	Catch ex As Exception
		strMsg = "[" & strlogpage & "] Error in UpdateDirInfo." & ex.Message
		App.LogEvents(strMsg, EventLogEntryType.Error)
	End Try
End Sub

VB
Private Sub FillContent(ByVal i As Integer, ByVal tbid As TextBlock, ByVal my_img As Image)
    Try
        tbid.Text = arr_promotions.Item(i).ID
        my_img.Source = obj_core.ReturnSpecificImage(arr_promotions.Item(i).strimgpath, 350)
        my_img.Visibility = Visibility.Visible
    Catch ex As Exception
        strMsg = "[" & strlogpage & "] Error at fill content to view. Ex:" & ex.Message
        App.LogEvents(strMsg, EventLogEntryType.Error)
        EnableTouch()
        ScreenTimer.Start()
    End Try
End Sub


VB.NET
Private Sub navi_next_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles navi_next.Click
    Dim i As Integer
    Dim intTmpVal As Integer = 1
    Try
        DisableTouch()
        ScreenTimer.Stop()
        intStartPageIdx = intStartPageIdx + (intMaxPromo + 1)
        intLastPageIdx = intStartPageIdx + (intMaxPromo + 1)
        navi_next.Visibility = Visibility.Collapsed
        navi_previous.Visibility = Visibility.Collapsed
        lngCurrPage = lngCurrPage + 1
        PageCount()
        tb_page.Text = lngCurrPage.ToString & " / " & lngTotPage.ToString
        HideAllBox()
        If intLastPageIdx >= arr_promotions.Count Then
            intCurrntPageIdx = intLastPageIdx - arr_promotions.Count
            intLastPageIdx = intLastPageIdx - intCurrntPageIdx
            For i = intStartPageIdx To (intLastPageIdx - 1)
                Select Case intTmpVal
                    Case 1
                        FillContent(i, id_1, img1)
                    Case 2
                        FillContent(i, id_2, img2)
                    Case 3
                        FillContent(i, id_3, img3)
                End Select
                intTmpVal = intTmpVal + 1
            Next
            navi_next.Visibility = Visibility.Collapsed
            navi_previous.Visibility = Visibility.Visible
        ElseIf intLastPageIdx < arr_promotions.Count Then
            For i = intStartPageIdx To (intLastPageIdx)
                Select Case intTmpVal
                    Case 1
                        FillContent(i, id_1, img1)
                    Case 2
                        FillContent(i, id_2, img2)
                    Case 3
                        FillContent(i, id_3, img3)
                End Select
                intTmpVal = intTmpVal + 1
            Next
            If intStartPageIdx <> 0 Then
                navi_next.Visibility = Visibility.Visible
                navi_previous.Visibility = Visibility.Visible
            Else
                navi_next.Visibility = Visibility.Visible
            End If
        End If
        EnableTouch()
        ScreenTimer.Start()
    Catch ex As Exception
        strMsg = "[" & strlogpage & "] Error in next button in promotion page." & ex.Message
        App.LogEvents(strMsg, EventLogEntryType.Error)
        EnableTouch()
        ScreenTimer.Start()
    End Try
End Sub
Private Sub navi_previous_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles navi_previous.Click
    Dim i As Integer
    Dim intTmpVal As Integer = 1
    Try
        DisableTouch()
        ScreenTimer.Stop()
        intStartPageIdx = intStartPageIdx - (intMaxPromo + 1)
        intLastPageIdx = intStartPageIdx + (intMaxPromo + 1)
        navi_next.Visibility = Visibility.Collapsed
        navi_previous.Visibility = Visibility.Collapsed
        lngCurrPage = lngCurrPage - 1
        PageCount()
        tb_page.Text = lngCurrPage.ToString & " / " & lngTotPage.ToString
        HideAllBox()
        If intLastPageIdx >= arr_promotions.Count Then
            intCurrntPageIdx = intLastPageIdx - arr_promotions.Count
            intLastPageIdx = intLastPageIdx - intCurrntPageIdx
            For i = intStartPageIdx To (intLastPageIdx - 1)
                Select Case intTmpVal
                    Case 1
                        FillContent(i, id_1, img1)
                    Case 2
                        FillContent(i, id_2, img2)
                    Case 3
                        FillContent(i, id_3, img3)
                End Select
                intTmpVal = intTmpVal + 1
            Next
            navi_next.Visibility = Visibility.Collapsed
            navi_previous.Visibility = Visibility.Visible
        ElseIf intLastPageIdx < arr_promotions.Count Then
            For i = intStartPageIdx To (intLastPageIdx)
                Select Case intTmpVal
                    Case 1
                        FillContent(i, id_1, img1)
                    Case 2
                        FillContent(i, id_2, img2)
                    Case 3
                        FillContent(i, id_3, img3)
                End Select
                intTmpVal = intTmpVal + 1
            Next
            If intStartPageIdx <> 0 Then
                navi_next.Visibility = Visibility.Visible
                navi_previous.Visibility = Visibility.Visible
            Else
                navi_next.Visibility = Visibility.Visible
            End If
        End If
        EnableTouch()
        ScreenTimer.Start()
    Catch ex As Exception
        strMsg = "[" & strlogpage & "] Error in previous button in promotions page." & ex.Message
        App.LogEvents(strMsg, EventLogEntryType.Error)
        EnableTouch()
        ScreenTimer.Start()
    End Try
End Sub
Posted
Comments
Michael_Davies 26-Dec-16 12:12pm    
For your PageCount simply Mod the arr_promotions.Count with 3 to find out how many sets of 3, for example;

lngTotPage = arr_promotions.Count Mod 3
If lngTotPage * 3 <> arr_promotions.Count then
lngTotPage += 1
Endif
Luiey Ichigo 26-Dec-16 12:35pm    
If the total promotion's data is 10:
lngTotPage = arr_promotions.Count Mod 3 'Equal to 1
If lngTotPage * 3 <> arr_promotions.Count Then
    lngTotPage += 1 'Equal to 2
End If


But in my long lines code, it shows on 4 pages it will be.
Michael_Davies 26-Dec-16 12:45pm    
Apologise;

lngTotPage = arr_promotions.Count / 3
If lngTotPage * 3 <> arr_promotions.Count Then
lngTotPage += 1
End If
Luiey Ichigo 26-Dec-16 13:00pm    
:)
That one works! Thanks. What's left is how to show the data in "UpdatePromotionsInfo" in the correct and dynamic ways because I have some other pages which need to show 30 data's on it. So, it will made me 30 repeated "Case". Some pages show 16 data's.

I'm using WPF Pages (Navigation)
Luiey Ichigo 27-Dec-16 10:05am    
Hi Micheal,

It seems this lines of code works. Have found since the code lines of some count return wrong number. Example total arraylist count is 10 datas, 15 lines to be shown for each page, total page return 1/2. Below code return 1 page:-

lngTotPage = CInt(Fix(Math.Ceiling(CDbl(arr_tenantview.Count) / 15)))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900