DataGridView throwing null reference error after code update - vb.net

As a fairly new user to VB.NET, I thought it was about time I tried to get to grips with DataGridView after learning the fundamentals of ListView and other basic knowledge. However, I have an error of 'Object reference not set to an instance of an object'. Let me try to explain how I think I have created this error but cannot seem to find a way out.
Initially, I had the code in the 1 winform, DGVtest.vb and upon pressing the 'fill grid' button, it filled the grid nulls and all. What I thought I would do to keep the code cleaner and in the long term, have more control, is to move the code out of DGVtest.vb and create a module, DGVmod.vb and put the code in there. However, being a new user, I now find myself totally out of my depth and looking for assistance as to how to move forward with this.
So, to outline, the DGV is in DGVtest.vb with a 'fill grid' button which calls 'DGVmod.fillgrid()' to fill the grid. If anyone can offer any assistance, I will be very grateful. Many thanks
Imports System
Imports System.Data
Imports System.Data.OleDb
Module DGVmod
Private da As OleDbDataAdapter
Private ds As DataSet
Public dtSource As DataTable
Private PageCount As Integer
Private maxRec As Integer
Private pageSize As Integer
Private currentPage As Integer
Private recNo As Integer
Sub fillPostings()
Dim conn As OleDbConnection = New OleDbConnection(My.Settings.storageConnectionString)
'Set the DataAdapter's query.
da = New OleDbDataAdapter("select * from Postings", conn)
ds = New DataSet()
' Fill the DataSet.
da.FillSchema(ds, SchemaType.Source, "Postings")
da.Fill(ds, "Postings")
' Set the source table.
dtSource = ds.Tables("Postings")
End Sub
Sub btnprevious()
If Not CheckFillButton() Then Return
If currentPage = PageCount Then
recNo = pageSize * (currentPage - 2)
End If
currentPage = currentPage - 1
'Check if you are already at the first page.
If currentPage < 1 Then
MessageBox.Show("You are at the First Page!")
currentPage = 1
Return
Else
recNo = pageSize * (currentPage - 1)
End If
loadpages()
End Sub
Sub btnnext()
'If the user did not click the "Fill Grid" button then Return
If Not CheckFillButton() Then Return
'Check if the user clicked the "Fill Grid" button.
If pageSize = 0 Then
MessageBox.Show("Set the Page Size, and then click the ""Fill Grid"" button!")
Return
End If
currentPage = currentPage + 1
If currentPage > PageCount Then
currentPage = PageCount
'Check if you are already at the last page.
If recNo = maxRec Then
MessageBox.Show("You are at the Last Page!")
Return
End If
End If
loadpages()
End Sub
Sub btnlast()
If Not CheckFillButton() Then Return
' Check if you are already at the last page.
If recNo = maxRec Then
MessageBox.Show("You are at the Last Page!")
Return
End If
currentPage = PageCount
recNo = pageSize * (currentPage - 1)
loadpages()
End Sub
Private Sub DisplayPageInfo()
DGVtest.txtDisplayPageNo.Text = "Page " & currentPage.ToString & "/ " & PageCount.ToString
End Sub
Sub fillgrid()
Try
'Set the start and max records.
pageSize = CInt(DGVtest.cmbPageSize.Text)
maxRec = DGVtest.DGV.Rows.Count
PageCount = maxRec \ pageSize
' Adjust the page number if the last page contains a partial page.
If (maxRec Mod pageSize) > 0 Then
PageCount = PageCount + 1
End If
'Initial seeings
currentPage = 1
recNo = 0
' Display the content of the current page.
loadpages()
Catch ex As Exception
MessageBox.Show(ex.InnerException.ToString)
End Try
End Sub
Private Sub loadpages()
Dim i As Integer
Dim startRec As Integer
Dim endRec As Integer
Dim dtTemp As DataTable
'Dim dr As DataRow
'Duplicate or clone the source table to create the temporary table.
dtTemp = dtSource.Clone <--- ERROR IS HERE
If currentPage = PageCount Then
endRec = maxRec
Else
endRec = pageSize * currentPage
End If
startRec = recNo
'Copy the rows from the source table to fill the temporary table.
For i = startRec To endRec - 1
dtTemp.ImportRow(dtSource.Rows(i))
recNo = recNo + 1
Next
DGVtest.DGV.DataSource = dtTemp
DisplayPageInfo()
End Sub
Sub btnfirst()
If Not CheckFillButton() Then Return
' Check if you are already at the first page.
If currentPage = 1 Then
MessageBox.Show("You are at the First Page!")
Return
End If
currentPage = 1
recNo = 0
loadpages()
End Sub
Private Function CheckFillButton() As Boolean
'Check if the user clicks the "Fill Grid" button.
If pageSize = 0 Then
MessageBox.Show("Set the Page Size, and then click the ""Fill Grid"" button!")
CheckFillButton = False
Else
CheckFillButton = True
End If
End Function
Sub cmbpage()
'Set the start and max records.
pageSize = CInt(DGVtest.cmbPageSize.Text)
maxRec = dtSource.Rows.Count
PageCount = maxRec \ pageSize
' Adjust the page number if the last page contains a partial page.
If (maxRec Mod pageSize) > 0 Then
PageCount = PageCount + 1
End If
'Initial seeings
currentPage = 1
recNo = 0
' Display the content of the current page.
loadpages()
End Sub
End Module

Related

Print multiple pages with Foreach (e.HasMorepages)

I have a question and i hope somebody can help me.
I would like to print a page, but if the page is longer than the first page than i would like to print multiple pages.
I've seen many code examples using e.HasMorePages.
UPDATE
This is my current code, it shows a second page but it's blank.
If it would be great if somebody can help me with this.
Private Sub PrintDocument_BO_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument_BO.PrintPage
Static page As Integer = 1
Dim startPosition As Integer = (page - 1) * PrintDocument_BO.DefaultPageSettings.Bounds.Height
Static maxPages As Integer = 0
If page = 1 Then
For Each ctrl1 As Control In PrintBackorder.PrintBO_panel.Controls
If TypeOf ctrl1 Is TextBox Or TypeOf ctrl1 Is Label Or TypeOf ctrl1 Is PictureBox Then
ctrl1.Tag = Int((ctrl1.Top + ctrl1.Height) / PrintDocument_BO.DefaultPageSettings.Bounds.Height) + 1
If CInt(ctrl1.Tag) > maxPages Then maxPages = CInt(ctrl1.Tag)
End If
Next
For Each ctrl2 As Control In PrintBackorder.BOLayoutPanel.Controls
If TypeOf ctrl2 Is TextBox Or TypeOf ctrl2 Is Label Or TypeOf ctrl2 Is PictureBox Then
ctrl2.Tag = Int((ctrl2.Top + ctrl2.Height) / PrintDocument_BO.DefaultPageSettings.Bounds.Height) + 1
If CInt(ctrl2.Tag) > maxPages Then maxPages = CInt(ctrl2.Tag)
End If
Next
End If
Dim sf = New StringFormat()
For Each ctrl1 As Control In PrintBackorder.PrintBO_panel.Controls
If CInt(ctrl1.Tag) = page Then
If TypeOf ctrl1 Is TextBox Or TypeOf ctrl1 Is Label Then
e.Graphics.DrawString(ctrl1.Text, ctrl1.Font, Brushes.Black, ctrl1.Bounds.Location)
ElseIf TypeOf ctrl1 Is PictureBox Then
'e.Graphics.DrawImage(DirectCast(ctrl, PictureBox).Image, New PointF(ctrl.Left, ctrl.Top - startPosition))
Dim myBitmap1 = New Bitmap(PrintBackorder.picBarcode.Width, PrintBackorder.picBarcode.Height - 5)
'Dim myBitmap1 As Bitmap = New Bitmap(PrintBackorder.picBarcode.Width, PrintBackorder.picBarcode.Height)
PrintBackorder.picBarcode.DrawToBitmap(myBitmap1, New Rectangle(0, 0, PrintBackorder.picBarcode.Width, PrintBackorder.picBarcode.Height))
e.Graphics.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality
'e.Graphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor
e.Graphics.CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality
e.Graphics.DrawImage(myBitmap1, 625, 50)
End If
End If
Next
For Each ctrl2 As Control In PrintBackorder.BOLayoutPanel.Controls 'PrintBackorder.PrintBO_panel.Controls
If CInt(ctrl2.Tag) = page Then
If TypeOf ctrl2 Is TextBox Or TypeOf ctrl2 Is Label Then
sf.Alignment = If(PrintBackorder.BOLayoutPanel.GetColumn(ctrl2) < 2, StringAlignment.Near, StringAlignment.Far)
e.Graphics.DrawString(ctrl2.Text, ctrl2.Font, Brushes.Black, PrintBackorder.PrintBO_panel.PointToClient(PrintBackorder.BOLayoutPanel.PointToScreen(ctrl2.Bounds.Location)), sf)
End If
End If
Next
page += 1
If page > maxPages Then
e.HasMorePages = False
page = 1
maxPages = 0
MsgBox("done")
Else
e.HasMorePages = True
End If
End Sub
I know i shouldn't use a For Each loop but i don't know how to use a While loop in this case.
Here is a general example of the principle provided in the comments:
'The data to be printed.
Private records As New List(Of String)
'The index of the next record to be printed.
Private recordIndex As Integer
Private Sub Button1_Click(...) Handles Button1.Click
'Start printing from the first record.
recordIndex = 0
PrintDocument1.Print()
End Sub
Private PrintDocument1_PrintPage(...) Handles PrintDocument1.PrintPage
Dim startRecordIndex = recordIndex
'Print 10 records or, if there are not 10 records left, to the end of the list.
Dim endRecordIndex = Math.Min(recordIndex + 9, records.Count - 1)
For startRecordIndex To endRecordIndex
Dim record = records(recordIndex)
e.Graphics.DrawString(record, ...)
Next
'The next page will start at the next unprinted record if there is one.
recordIndex = endRecordIndex + 1
e.HasMorePages = recordIndex < records.Count
End Sub

How to freeze merged columns in data grid view when scrolling vertically?

I have a data grid view where I need the columns to be frozen or fixed when scrolling vertically.
I have a data grid view control in vb.net windows application which displays the data in a parent-child hierarchy(as shown below). The first column displays the parent data and the second column displays all its child data. The child data in the second column can be as much as 100 rows or even more. So when scrolling down through the grid, the value in the first column does not remain there as it is while the values in the second column(i.e. the child data) scrolls down. So if the user wants to check to which parent, the current child info belongs to, then again he will have to scroll up to the starting of the column to find the name of the parent. I want the values in the first column to be displayed or frozen till it reaches the end of the list of its child values in the grid or at least till the next row where the next parent data starts. I have suggested the client to go with a tree view but they are not agreeing and need it in a data grid view itself. Is there anyway to achieve this in a data grid view?
Thanks in advance.
You can't freeze a row (in runtime, on dgv scrolling) with index greater than zero because all those before are frozen and at that point you can't scroll your datagridview.
If I understood correctly what you want I wrote this class quickly (probably should be optimized). Usage is simple.
1 - First create your own datagridview.
2 - then add your columns and rows (IMPORTANT: Put a “X” in the Tag in each row is a Parent or is considered as title for other rows as you seen in TestPopulate method) .
3 - Call the class I made by passing the datagridview (you created first) as a parameter. At this point this control takes its size, placement and REPLACE YOUR DATAGRIDVIEW .
Private Class CustomDgv
Inherits Panel
Dim WithEvents TopDgv As DataGridView = New DataGridView
Dim WithEvents DownDgv As DataGridView = New DataGridView
Dim Cols As Integer
' This variable is in case you have more rows as "headrow"
' In TestPopulate you can see how to get those
Dim listOfOwnerRows As List(Of Integer) = New List(Of Integer)
Dim currentTopRow As Integer = -1
Protected Overloads Property Height As Integer
Get
Return MyBase.Height
End Get
Set(value As Integer)
MyBase.Height = value
TopDgv.Height = TopDgv.RowTemplate.Height - 1
DownDgv.Height = value - TopDgv.Height - 1
End Set
End Property
Protected Overloads Property Width As Integer
Get
Return MyBase.Width
End Get
Set(value As Integer)
MyBase.Width = value
TopDgv.Width = value - 1
DownDgv.Width = value - 1
End Set
End Property
Sub New(dgvOriginal As DataGridView)
DownDgv = dgvOriginal
Dim parentCtrl As Control = dgvOriginal.Parent
parentCtrl.Controls.Remove(dgvOriginal)
parentCtrl.Controls.Add(Me)
Me.Location = DownDgv.Location
Me.Size = DownDgv.Size
Me.BorderStyle = DownDgv.BorderStyle
TopDgv.Width = Width - 2 - SystemInformation.VerticalScrollBarWidth
TopDgv.Height = TopDgv.RowTemplate.Height
TopDgv.ScrollBars = ScrollBars.None
TopDgv.ColumnHeadersVisible = False
TopDgv.BorderStyle = BorderStyle.None
DownDgv.ColumnHeadersVisible = False
DownDgv.BorderStyle = BorderStyle.None
TopDgv.Left = 0
DownDgv.Left = 0
DownDgv.Width = Width - 2
DownDgv.Height = Height - 2
For Each Col As DataGridViewColumn In DownDgv.Columns
Dim cIndex As Integer = TopDgv.Columns.Add(Col.Clone)
If Col.Frozen Then
TopDgv.Columns(cIndex).Frozen = True
End If
Cols += 1
Next
DownDgv.Top = 0
Me.Controls.Add(TopDgv)
Me.Controls.Add(DownDgv)
If DownDgv.Rows.Count > 0 Then
listOfOwnerRows = (From R As DataGridViewRow In DownDgv.Rows
Where R.Tag = "X"
Select R.Index).ToList
If listOfOwnerRows.Count > 0 Then
SetFrosenRow(listOfOwnerRows(0))
End If
End If
End Sub
Protected Sub SetFrosenRow(index As Integer)
If DownDgv.Rows.Count > index Then
TopDgv.Rows.Clear()
TopDgv.Rows.Add()
Dim currentRIndex As Integer = DownDgv.FirstDisplayedScrollingRowIndex
'If you want onlly the base row
For i As Integer = 0 To Cols - 1
TopDgv.Rows(0).Cells(i).Value = DownDgv.Rows(index).Cells(i).Value
Next
'Or else get the diplayed on top row
TopDgv.Rows(0).DefaultCellStyle = New DataGridViewCellStyle With {
.BackColor = Color.Bisque
}
currentTopRow = index
End If
End Sub
Protected Sub SetChildValuesInTopRow(index As Integer)
For i As Integer = 1 To Cols - 1
TopDgv.Rows(0).Cells(i).Value = DownDgv.Rows(index).Cells(i).Value
Next
End Sub
Private Sub DownDgv_Scroll(sender As Object, e As ScrollEventArgs) Handles DownDgv.Scroll
Try
If e.ScrollOrientation = ScrollOrientation.VerticalScroll Then
Dim topR As Integer = DownDgv.FirstDisplayedScrollingRowIndex
'If you want in top row the current value that is in the top uncomment this
SetChildValuesInTopRow(topR)
If listOfOwnerRows.Count > 0 Then
Dim rToSetAsOwner As Integer = listOfOwnerRows(listOfOwnerRows.Count - 1)
For i As Integer = listOfOwnerRows.Count - 1 To 0 Step -1
If listOfOwnerRows(i) <= topR Then
rToSetAsOwner = listOfOwnerRows(i)
Exit For
End If
Next
If rToSetAsOwner <> currentTopRow Then
SetFrosenRow(rToSetAsOwner)
End If
Console.WriteLine("rToSetAsOwner: " & rToSetAsOwner)
End If
Else
TopDgv.HorizontalScrollingOffset = DownDgv.HorizontalScrollingOffset
End If
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
End Sub
End Class
Usage:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
' first populate you grid putting a tag in each row which is a header/parent/title for other rows
TestPopulate()
Dim customControl As Control = New CustomDgv(DataGridView1)
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
End Sub
Sub TestPopulate()
For i As Integer = 0 To 100
DataGridView1.Rows.Add()
If i = 0 Then
DataGridView1.Rows.Item(0).Cells(0).Value = "Owner 0"
DataGridView1.Rows(0).Tag = "X"
End If
If i = 50 Then
DataGridView1.Rows.Item(50).Cells(0).Value = "Owner 50"
DataGridView1.Rows(50).Tag = "X"
End If
If i = 70 Then
DataGridView1.Rows.Item(70).Cells(0).Value = "Owner 70"
DataGridView1.Rows(70).Tag = "X"
End If
DataGridView1.Rows.Item(i).Cells(1).Value = "child_" & i.ToString & "_1"
DataGridView1.Rows.Item(i).Cells(2).Value = "child_" & i.ToString & "_2"
Next
End Sub
I hope I have been helpful

Highlight specific text while user is typing

I am writing a code that highlight the duplicate words in a text. The code is working well when I add a button and the user have to press on the button to check for duplicates.
But I want to make an auto-checking code. I set my code in a subroutine that Handles RichTextBox.TextChanged. The problem is the code selects the target word and highlight it but the selection remains so when a new letter is typed, it clear what has been highlighted.
Here is my code:
Private Sub RichTextBox_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox.TextChanged
Try
Call duplicate_check()
Catch ex As Exception
MessageBox.Show("error in RichTextBox.TextChanged")
End Try
End Sub
duplicate check function:
Private Sub duplicate_check()
Try
' read line by line and get input
Dim LineByLineInput() As String = RichTextBox.Lines
Dim selectionStart, selectionLength As Integer
Dim i, j As Integer
For lineNumber = 0 To UBound(LineByLineInput)
selectionStart = 0
selectionLength = 0
'get index of first char index in the current line
Dim count As Integer = lineNumber
While count <> 0
selectionStart += RichTextBox.Lines(count - 1).Length + 1
count -= 1
End While
' get line as string
Dim line As String = RichTextBox.Lines(lineNumber)
' split line into array of strings
Dim input() As String = line.Split(" ")
'check for duplicates
i = 0
For j = i + 1 To UBound(input)
If input(i) = input(j) Then 'compare each 2 consecutive words if they are the same
selectionStart += input(i).Length + 1
selectionLength = input(i).Length
RichTextBox.SelectionStart = selectionStart
RichTextBox.SelectionLength = selectionLength
RichTextBox.SelectionBackColor = Color.Yellow
Else
selectionStart += input(i).Length + 1
End If
i += 1
Next
Next
Catch ex As Exception
MessageBox.Show("error duplicate_check()")
End Try
End Sub
After your duplicate_check call, have you tried to set the selection of the RichTextBox back to the old position ?
See below :
Private Sub RichTextBox1_TextChanged(sender As Object, e As System.EventArgs) Handles RichTextBox1.TextChanged
Try
' Get current position
Dim cur_pos As Integer = Me.RichTextBox.SelectionStart
Call duplicate_check()
' Set back to old position
Me.RichTextBox.SelectionStart = cur_pos
' Unselect what your sub duplicate_check has selected
Me.RichTextBox1.SelectionLength = 0
Catch ex As Exception
MessageBox.Show("error in RichTextBox.TextChanged")
End Try
End Sub
If this solution is good for you, you should change your duplicate_check Sub to make this change and not in the RichTextBox1_TextChanged Sub

For loop producing IndexOutOfRange error

I keep getting the following error and unsure has to how fix it. As a fairly new user to VB.NET, I think it is saying that there are no rows at that position? To compensate for this, I included an If statement to check the row count, but it is still producing this error. In fact, the messagebox is not firing at all. Can someone please advise as to how I can correct this error. Thanks
Link where code obtained: http://support.microsoft.com/kb/305271/en-us
There is no row at position 1.
Private Sub loadpages()
Dim i As Integer
Dim startRec As Integer
Dim endRec As Integer
Dim dtTemp As DataTable
'Dim dr As DataRow
'Duplicate or clone the source table to create the temporary table.
dtTemp = dtSource.Clone
If currentPage = PageCount Then
endRec = maxRec
Else
endRec = pageSize * currentPage
End If
startRec = recNo
'Copy the rows from the source table to fill the temporary table.
If dtSource.Rows.Count <> 0 Then
For i = startRec To endRec - 1
dtTemp.ImportRow(dtSource.Rows(i)) <--- ERROR HERE
recNo = recNo + 1
Next
Else
MessageBox.Show(dtSource.Rows.Count.ToString())
End If
frmMain.DGV.DataSource = dtTemp
DisplayPageInfo()
'fillPostings()
End Sub
combobox sub to change pagesize
Sub cmbpage()
'Set the start and max records.
pageSize = CInt(frmMain.cmbPageSize.Text)
maxRec = dtSource.Rows.Count
PageCount = maxRec \ pageSize
MessageBox.Show(CStr(maxRec))
' Adjust the page number if the last page contains a partial page.
If (maxRec Mod pageSize) > 0 Then
PageCount = PageCount + 1
End If
'Initial seeings
currentPage = 1
recNo = 0
' Display the content of the current page.
UDGfillPostings()
loadpages()
End Sub
you have assigned startRec to 1,so it is throwing error when dtSource.Rows(1),as there is only one element in the array
you can rectify this by using dtSource.Rows(i-1)
You probably want this looping.
For i = 0 To dtSource.Rows.Count-1
If you just want to copy one DataTable to another then you can just use DataTable.Copy method.
Dim dtTemp As DataTable
dtTemp = dtSource.Copy()

Trouble with Timer_tick not stopping

I'm very new to programming and vb.net, trying to self teach more so as a hobby, as I have an idea for a program that I would find useful, but I am having trouble getting past this issue and I believe it is to do with the timer.
I have a form of size.(600,600) with one button of size.(450,150) that is set location(100,50) on the form. When clicked I want to move down it's own height, then add a new button in it's place. The code included below works as desired for the first two clicks, but on the third click the button keeps moving and the autoscroll bar extends. I initially thought it was the autoscroll function or the location property, but realised that as the button keeps moving, the timer hasn't stopped. I am aware that the code is probably very clunky in terms of achieving the outcome, and that there are a few lines/variables that are currently skipped over by the compiler (these are from older attempts to figure this out).
I have looked around and can't find the cause of my problem. Any help would be greatly appreciated. Apologies if the code block looks messy - first go.
Public Class frmOpenScreen
Dim intWButtons, intCreateButtonY, intCreateButtonX 'intTimerTick As Integer
Dim arrWNames() As String
Dim ctrlWButtons As Control
Dim blnAddingW As Boolean
Private Sub btnCreateW_Click(sender As System.Object, e As System.EventArgs) Handles btnCreateW.Click
'Creates new Button details including handler
Dim strWName, strWShort As String
Dim intCreateButtonY2 As Integer
Static intNumW As Integer
Dim B As New Button
strWName = InputBox("Please enter the name name of the button you are creating. Please ensure the spelling is correct.", "Create W")
If strWName = "" Then
MsgBox("Nothing Entered.")
Exit Sub
End If
strWShort = strWName.Replace(" ", "")
B.Text = strWName
B.Width = 400
B.Height = 150
B.Font = New System.Drawing.Font("Arial Narrow", 21.75)
B.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink
B.Anchor = AnchorStyles.Top
B.Margin = New Windows.Forms.Padding(0, 0, 0, 0)
'Updates Crucial Data (w name array, number of w buttons inc Create New)
If intNumW = 0 Then
ReDim arrWNames(0)
Else
intNumW = UBound(arrWNames) + 1
ReDim Preserve arrWNames(intNumW)
End If
arrWNames(intNumW) = strWShort
intNumW = intNumW + 1
intWButtons = WButtonCount(intWButtons) + 1
'updates form with new button and rearranges existing buttons
intCreateButtonY = btnCreateW.Location.Y
intCreateButtonX = btnCreateW.Location.X
‘intTimerTick = 0
tmrButtonMove.Enabled = True
‘Do While intTimerTick < 16
‘ 'blank to do nothing
‘Loop
'btnCreateW.Location = New Point(intCreateButtonX, intCreateButtonY + 150)
B.Location = New Point(intCreateButtonX, intCreateButtonY)
Me.Controls.Add(B)
B.Name = "btn" & strWShort
intCreateButtonY2 = btnCreateW.Location.Y
If intCreateButtonY2 > Me.Location.Y Then
Me.AutoScroll = False
Me.AutoScroll = True
Else
Me.AutoScroll = False
End If
'MsgBox(intCreateButtonY)
End Sub
Function WButtonCount(ByRef buttoncount As Integer) As Integer
buttoncount = intWButtons
If buttoncount = 0 Then
Return 1
End If
Return buttoncount
End Function
Public Sub tmrButtonMove_Tick(sender As System.Object, e As System.EventArgs) Handles tmrButtonMove.Tick
Dim intTimerTick As Integer
If intTimerTick > 14 Then
intTimerTick = 0
End If
If btnCreateW.Location.Y <= intCreateButtonY + 150 Then
btnCreateW.Top = btnCreateW.Top + 10
End If
intTimerTick += 1
If intTimerTick = 15 Then
tmrButtonMove.Enabled = False
End If
End Sub
End Class
So my current understanding is that the tick event handler should be increasing the timertick variable every time it fires, and that once it has hits 15 it should diable the timer and stop the button moving, but it is not doing so.
Thanks in advance.
IntTimerTick is initialized to 0 at the beginning of every Tick event. This won't happen if you declare it to be static:
Static Dim intTimerTick As Integer