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:
"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
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
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
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