VB.NET- Changing .Text property of Textbox throws Null Reference Exception - nullreferenceexception

This MUST be trivial. I do this for a living and CAN NOT figure out why I am getting this exception:
System.NullReferenceException was unhandled Message=Object reference
not set to an instance of an object.
Here is the code:
Public Class frmMain
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim counties() As String
counties = {"", "Huntsville, AL", "Madison, AL", "Rural Madison County, AL", "Taft, TN"}
Me.cbCounties.DataSource = counties
Me.lblStatus.Text = "[ Please select a county ]"
Me.lblStatus.Left = Me.ClientSize.Width \ 2 - Me.lblStatus.Width \ 2
End Sub
Private Sub cbCounties_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbCounties.SelectedIndexChanged
Select Case cbCounties.SelectedIndex
Case 1
txtTaxRate.Text = "8.00%" 'Issue is these, when index is changed.
Case 2
txtTaxRate.Text = "8.50%"
Case 3
txtTaxRate.Text = "5.50"
Case 4
txtTaxRate.Text = "9.50%"
Case Else
txtTaxRate.Text = Nothing
End Select
Me.lblStatus.Text = "[ Please enter net amount ]"
Me.lblStatus.Left = Me.ClientSize.Width \ 2 - Me.lblStatus.Width \ 2
End Sub
End Class
Help?

Is this after you changed it a couple of times?
Setting txtTaxRate = Nothing and later trying to set txtTaxRate.Text to something else will cause some problems.
You are setting the textbox object to nothing, and later trying to reference one of its properties.
Try changing
txtTaxRate = Nothing
to
txtTaxRate.Text = ""

Related

Operation is not valid, because of the current state of the object

I have an Infopath 2010 form with a button and the following event code:
Public Sub txtGetCC_Changing(ByVal sender As Object, ByVal e As XmlChangingEventArgs)
' Ensure that the constraint you are enforcing is compatible
' with the default value you set for this XML node.
Dim myNav As XPathNavigator = Me.MainDataSource.CreateNavigator
If myNav.SelectSingleNode("//my:txtGetCC", Me.NamespaceManager).ToString.Length > 0 Then
Dim myIterator As XPathNodeIterator = myNav.Select("/my:myFields/my:group11/my:group12", Me.NamespaceManager)
Dim CCnummer As String = ""
Dim CCsource As DataSource = Me.DataSources("CostCenters")
Dim email As String = ""
While myIterator.MoveNext
CCnummer = myIterator.Current.SelectSingleNode("my:cbRTcc", Me.NamespaceManager).Value
Dim myNavigator As XPathNavigator = Me.DataSources("CostCenters").CreateNavigator
Dim rows As XPathNodeIterator = myNavigator.Select("/dfs:myFields/dfs:dataFields/d:CostCenters", Me.NamespaceManager)
email = ""
While (rows.MoveNext)
Dim ccnumber As String = rows.Current.SelectSingleNode("#CCnumber", Me.NamespaceManager).Value
If ccnumber = CCnummer Then
email = rows.Current.SelectSingleNode("#Email", Me.NamespaceManager).Value
Exit While
End If
End While
myIterator.Current.SelectSingleNode("//my:txtCCowner", Me.NamespaceManager).SetValue(email)
End While
End If
End Sub
The line just before the last END WHILE is not working.
I want to update the column txtCCowner in the repeating table to be updated with the email address found in the external datasource.
Everything works except this line.
The error message I get is: Operation is not valid, because of the current state of the object.
Why do I get this error, what am I doing wrong?
Any help is greatly appreciated.
rg.
Eric

mdi child doesn't show maximized

We're having the following issue in our application: When we open an mdi childform it comes in maximize windowstate as it should be but it is shown in normal state.Tthe background from the parent is still visible on the right and bottom side.
However when i change the childform to normal state and maximize it again it is shown as maximized. But when the application is closed and restarted the form comes again with the issue.
Can anyone help me to solve this problem?
Is this a problem related to windows? or something else?
Private Sub tbrMngr_ToolClick(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinToolbars.ToolClickEventArgs) Handles tbrMngrCeres.ToolClick
Try
'--> Handle click '
If Not e.Tool.SharedProps.Tag Is Nothing Then
Select Case CType(e.Tool.SharedProps.Tag, String).ToUpper
Case "M_MANAGEMENT" : HandleManagementClick(e.Tool.Key)
End Select
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
here is the case selected from what button on th menubar has been clicked. All the problems only come within HandleManagementClick
Private Sub HandleManagementClick(ByVal pstrKey As String)
Try
Dim lblnNonModal As Boolean = True
If Not Me.ifrmActiveForm Is Nothing Then
If Not ifrmActiveForm.Disposing Then
ifrmActiveForm.WindowState = FormWindowState.Normal
ifrmActiveForm.Dispose()
ifrmActiveForm = Nothing
iNotifiers = Nothing
End If
End If
Me.Cursor = Cursors.AppStarting
Select Case pstrKey.ToUpper
Case "RES" : Me.ifrmActiveForm = New frmReservationWizard(Me.iclsApp, "RES")
Case "RCAT" : Me.ifrmActiveForm = New frmFacilityCategory(Me.iclsApp, "RCAT")
Case "RGRP" : Me.ifrmActiveForm = New frmFacilityGroup(Me.iclsApp, "RGRP")
End Select
If Not ifrmActiveForm Is Nothing Then
If pstrKey.ToUpper.Equals("RES") Then
'ifrmActiveForm.ShowInTaskbar = False
ifrmActiveForm.ShowDialog()
' Me.ShowScreen(FormWindowState.Normal, False, lblnNonModal, FormStartPosition.CenterScreen)
ifrmActiveForm.Dispose()
Else
Me.ShowScreen(FormWindowState.Maximized, False, lblnNonModal, FormStartPosition.CenterScreen)
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
As you see the windowstate property is set on maximize at the showscreen sub.
Private Function ShowScreen(ByVal pWindowState As FormWindowState, _
ByVal pblnRegisterChild As Boolean, _
ByVal pblnIsMdiChild As Boolean, _
ByVal pStartPosition As FormStartPosition) As DialogResult
Dim lDialResult As DialogResult = DialogResult.None
Dim lblnOpeningScreen As Boolean
Try
If Not lblnOpeningScreen Then
lblnOpeningScreen = True
Me.iNotifiers = Nothing
If pblnIsMdiChild Then
If pblnRegisterChild Then RegisterChild(AddressOf ifrmActiveForm.ParentNotifier)
ifrmActiveForm.WindowState = pWindowState
ifrmActiveForm.MdiParent = Me
ifrmActiveForm.Show()
Else
lDialResult = ifrmActiveForm.ShowDialog()
End If
End If
Return lDialResult
Catch ex As Exception
MessageBox.Show(ex.Message)
I hope this covers a bit more documentation.
Kind Regards,
Kristof

Visual Basic Deleting Unmodified Files

The following code will extract files from one Folder and Place them another as you can see. But I am trying to delete the files that have not been modified in the past 3 months, and it does not delete any files at all.
Code:
Imports System.IO
Public Class frmExtractionator
Dim txtFiles1 As Control
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Dim sourceDirectory As String = "E:\CopierFolderforTestDriveCapstone"
Dim archiveDirectory As String = "E:\FilesExtracted"
Try
Dim txtFiles = Directory.EnumerateFiles(sourceDirectory)
If (Not System.IO.Directory.Exists(archiveDirectory)) Then
System.IO.Directory.CreateDirectory(archiveDirectory)
End If
For Each currentFileLoc As String In txtFiles
Dim fileName = currentFileLoc.Substring(sourceDirectory.Length + 1)
File.Move(currentFileLoc, Path.Combine(archiveDirectory, fileName))
Try
Dim di As DirectoryInfo = New DirectoryInfo(sourceDirectory)
Dim fi As FileInfo() = di.GetFiles()
For Each currentFile As FileInfo In fi
File.Move(currentFile.FullName, Path.Combine(archiveDirectory, currentFile.Name))
Dim dt As DateTime = currentFile.LastWriteTime
' Add 3 months to the last write and check if it is less than today '
If dt.AddMonths(3) < DateTime.Today Then
File.Delete(currentFile.FullName)
End If
Next
Catch eT As Exception
Console.WriteLine(eT.Message)
End Try
Next
Catch eT As Exception
Console.WriteLine(eT.Message)
End Try
End Sub
End Class

Programmatically adding event handler to array of radio buttons

Trying to add event handlers to radio buttons that are created on user input, looking at this and this as examples.
However I get an error that answerOptions is not an event.
Create the radio buttons
Private answerOptions(n) As RadioButton
...
Private Sub showQuestion(n As Integer)
For i = 0 To answerOptions.Length - 1
answerOptions(i) = New RadioButton
AddHandler answerOptions, AddressOf Me.Radios_Click
With answerOptions(i)
' --------- SET TEXT, LOCATION ETC.
End With
Me.Controls.Add(answerOptions(i))
Next
End Sub
Planning on then handling events with
Private Sub Radios_Click(sender As Object, e As EventArgs) Handles answerOptions.checked
End Sub
I want things to happen when the radios are checked. Don't want to use checkboxes as I want to limit one selection at a time.
Try this in your form or page
Private Sub ClickButton(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim Rbtn As RadioButton
Rbtn = CType(sender, RadioButton)
MsgBox(Rbtn.Text)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
Dim Rbtn As New RadioButton()
For i = 0 To 19
Rbtn = New RadioButton()
With Rbtn
.Name = "RBtn" & i
.Text = .Name
.Checked = False
.Left = 20
.Top = (i * 20)
.Visible = True
Me.Controls.Add(Rbtn)
AddHandler Rbtn.Click, AddressOf ClickButton
End With
Next
End Sub

How to change the location of a picture box in VB 2010 using the arrow or wasd keys?

I only have access to the internet at school, so the filters get in the way of any real research. I'm currently coding an rpg for a school project but it's difficult to get the avatar to move on a map. Here's my pathetic code so far:
Public Class Map1
Private Sub USER_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
User.Top = User.Top - 1
End Sub
Private Sub USER_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
User.Top = User.Bottom + 1
'User.Location.X = 200
End Sub
End Class
I have the following problems with it:
User.location.x = 200 had syntax errors when I deleted the x and when I didn't.
The player also had to continually press the keys to move.
both I do not know how to correct.
Any help at all is greatly appreciated as it's for my final grade.
You can put it in a timer_tick to loop over it, that is what I do.
And the correct version of User.Location.X = 200 is:
User.location = new point(200, User.location.y)
nvm found the solution.
Here it is for anyone else how might need the code in the future.
Private Sub Map_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Up Then
User.Location = New Point(User.Location.X, User.Location.Y - 5)
ElseIf e.KeyCode = Keys.Down Then
User.Location = New Point(User.Location.X, User.Location.Y + 5)
ElseIf e.KeyCode = Keys.Left Then
User.Location = New Point(User.Location.X - 5, User.Location.Y)
ElseIf e.KeyCode = Keys.Right Then
User.Location = New Point(User.Location.X + 5, User.Location.Y)
End If
Module
Public Sub MovePictureBox(ByRef PictureBox As PictureBox)
Tiempo.Tag = PictureBox
AddHandler PictureBox.Parent.KeyDown, AddressOf Parent_KeyDown
AddHandler PictureBox.Parent.KeyUp, AddressOf Parent_KeyUp
AddHandler CType(PictureBox.Parent, Form).Load, AddressOf Parent_Load
End Sub
Private Up, Down, Left, Right As Boolean
WithEvents Tiempo As New Timer() With {.Interval = 1}
Private Sub Tiempo_Tick() Handles Tiempo.Tick
If Up Then Tiempo.Tag.Top -= 2
If Down Then Tiempo.Tag.Top += 2
If Left Then Tiempo.Tag.Left -= 2
If Right Then Tiempo.Tag.Left += 2
End Sub
Private Sub Parent_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.Up Then Up = True
If e.KeyCode = Keys.Down Then Down = True
If e.KeyCode = Keys.Left Then Left = True
If e.KeyCode = Keys.Right Then Right = True
Tiempo.Enabled = True
End Sub
Private Sub Parent_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Tiempo.Enabled = False
Up = False : Down = False : Right = False : Left = False
End Sub
Private Sub Parent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
sender.KeyPreview = True
End Sub
End Module

Resources