6 Replies - 3490 Views - Last Post: 27 August 2012 - 04:32 PM Rate Topic: -----

#1 jgrotter   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-September 10

Change radiobutton text based on file contents

Posted 23 August 2012 - 09:10 PM

Hello everyone,

Brand new to VB.net and to programming. I am doing this for homework, and am stuck on the idea of working it out. I am getting no error messages yet because this code hasn't been run - it's incomplete, and the "LoanTypeBox" radio button group box in the line "For Each RadioButton In LoanTypeBox" isn't useful (just a placeholder for the idea). ("LoanTypeBox" is a Radiobutton group box on my form, but this word in my code has a squiggly, blue line under it, and message says "Expression is of type 'System.Windows.Forms.GroupBox', which is not a collection type.)

I'm doing the ever-present "amortization calculator," and I'm attempting to perform part of this assignment, setting the text of a radio button from an external file, with the following code.

The idea of the code I wrote is to open a text file I've saved named "LoanTerms.txt" and read each of the three comma-delimited values there (they consist of a period and a rate, such as "7,5.35"). Then I want to change the Text value of the first three radio buttons ("RB_Loan1" - "RB_Loan3") to reflect the period and rate of each line - the fourth and final radio button ("RB_Loan4") has a text value "Customize my terms", I want to keep that.

My question is, what wording might I use to loop through the three radio buttons within my form - specifically within this groupbox - to change the Text property of each button? Could someone point an absolute beginner towards an answer (and maybe a bit of an explanation on it)?

    Private Function GetTerms(ByVal RBtext As String) As String
        ' Declare the streamreader variable.
        Dim Ratetext As String
        Dim Periodtext As String
        Dim rbName As RadioButton
        Dim rb As Decimal
        For Each RadioButton In LoanTypeBox
            If Not RadioButton = "RB_Loan4" Then
                ' Open the input file and display the first record.
                Try
                    TermStreamReader = New StreamReader("LoanTerms.txt")
                    ' Read and display the next record.
                    If TermStreamReader.Peek >= 0 Then
                        Periodtext = TermStreamReader.ReadLine()
                        Ratetext = TermStreamReader.ReadLine()
                        RadioButton.Equals = Periodtext & " years at " & Ratetext & "%"
                    End If
                Catch ex As Exception
                    MessageBox.Show("File not found or is invalid.", "Data Error")
                End Try
            End If
        Next
        ' Close file.
        If Not TermStreamReader Is Nothing Then
            ' Close the file, only if it is open.
            TermStreamReader.Close()
        End If
    End Function



Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Change radiobutton text based on file contents

#2 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Change radiobutton text based on file contents

Posted 23 August 2012 - 09:27 PM

Make sure your comparing the right data types.
For Each ctr As Control In {control collection}
  If TypeOf ctr Is RadioButton Then
    ' it is
  End If
Next


Your trying to compare a RadioButton to text.
Was This Post Helpful? 2
  • +
  • -

#3 jgrotter   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-September 10

Re: Change radiobutton text based on file contents

Posted 24 August 2012 - 11:54 AM

View Post_HAWK_, on 23 August 2012 - 09:27 PM, said:

Make sure your comparing the right data types.
For Each ctr As Control In {control collection}
  If TypeOf ctr Is RadioButton Then
    ' it is
  End If
Next


Your trying to compare a RadioButton to text.


Thank you, Hawk - this helps me get towards the right direction. I have something I think I can test now, and I changed the code some to look like the following:

Private Function GetTermLabels()
        ' Declare the streamreader variable.
        Dim ctrl1 As Control, ctrl2 As Control
        Dim Ratetext As String
        Dim Periodtext As String
        'Dim rbName As RadioButton
        'Dim rb As Decimal
        For Each ctrl1 In Me.Controls
            If ctrl1.Name = "LoanTypeBox" Then
                For Each ctrl2 In ctrl1.Controls
                    If Not ctrl2.Name = "RB_Loan4" Then
                        ' Open the input file and display the first record.
                        Try
                            TermStreamReader = New StreamReader("LoanTerms.txt")
                            ' Read and display the next record.
                            If TermStreamReader.Peek >= 0 Then
                                Periodtext = TermStreamReader.ReadLine()
                                Ratetext = TermStreamReader.ReadLine()
                                ctrl2.Text = Ratetext & " years at " & Periodtext & "%"
                            End If
                        Catch ex As Exception
                            MessageBox.Show("File not found or is invalid.", "Data Error")
                        End Try
                    End If
                Next
            End If
        Next
        ' Close file.
        If Not TermStreamReader Is Nothing Then
            ' Close the file, only if it is open.
            TermStreamReader.Close()
        End If
    End Function



Would this be more correct for what I'm trying?

I haven't really tested this yet, either: the error-thing now is a squiggly-line under "End Function" saying that my, "Function 'GetTermLabels' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used." I understand I'm getting this because I don't have a return path, but putting "Return" before "End Function" is not the correct answer. What is it I don't understand about return paths in functions (I tried MSDN, and a couple other things, but not so useful answers to me). Is it because I don't have a way out of my "For Each" loop's "IF" statement?
Was This Post Helpful? 0
  • +
  • -

#4 jgrotter   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-September 10

Re: Change radiobutton text based on file contents

Posted 24 August 2012 - 01:59 PM

Hi Hawk, and all.

I've been working on this some more, and cleared up a few things by reading related posts/textbook, searching, etc. The posts on this site are awesome, btw.

I've changed my code and cleared up the squiggly-line errors (I still think I'm doing something wrong - as in "best practices" - but here's what I've got), and I'm wondering if there is a more efficient way of doing this now?

Thank you again for your help, Hawk - I couldn't have gotten here without it, and your posts in other questions.

    Private Function GetTermLabels()
        ' Declare the "Control" variables for finding the radio button group and radio buttons.
        Dim ctrl1 As Control, ctrl2 As Control
        Dim Ratetext As String
        Dim Periodtext As String
        For Each ctrl1 In Me.Controls
            If ctrl1.Name = "LoanTypeBox" Then
                ' Open the input file if it exists, error if not.
                Try
                    TermStreamReader = New StreamReader("LoanTerms.txt")
                Catch ex As Exception
                    MessageBox.Show("File not found or is invalid.", "Data Error")
                End Try
                For Each ctrl2 In ctrl1.Controls
                    If TypeOf (ctrl2) Is RadioButton And Not ctrl2.Name = "RB_Loan4" Then
                        ' Read and display the next record.
                        If TermStreamReader.Peek >= 0 Then
                            Dim TermStr() As String = TermStreamReader.ReadLine.Split(","c)
                            Periodtext = TermStr(0)
                            Ratetext = TermStr(1)
                            ctrl2.Text = Periodtext & " years at " & Ratetext & "%"
                        Else
                            MessageBox.Show("No more records to display.", "End of File")
                        End If
                    End If
                Next
            End If
        Next
        ' Close file.
        If Not TermStreamReader Is Nothing Then
            ' Close the file, only if it is open.
            TermStreamReader.Close()
        End If
        Return Nothing
    End Function



So - that is my bulky way of handling re-naming the three (of four) radio buttons based on my separate text file. I moved the lines to open the file outside the radio button "IF" statement but inside the radio button group's "IF" statement, so that it only opens the file once and does so when it finds the correct button group (I was having a problem with it starting from the beginning of the file each time it got to the next radio button).

I'm thinking of changing this, though, so that the loan terms (year/rate) are all set up in an array for use later, as well. That way I can use the stored values after the file is closed, and I only have to open the file once.

Thanks for the answers so far, and if anyone sees a more efficient way, please, please feel free to say so.
Was This Post Helpful? 0
  • +
  • -

#5 jgrotter   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-September 10

Re: Change radiobutton text based on file contents

Posted 24 August 2012 - 07:18 PM

Last update (unless someone comments/critiques). I was able to use an array, and ended up with the following code. If anyone sees a more efficient way, or can comment on "best practices" instead of what I have, please do (I'm trying to learn).

#Region "GetTermOptions Function"
    Private Function GetTermOptions()
        ' Open the input file if it exists, error if not.
        Try
            TermStreamReader = New StreamReader("LoanTerms.txt")
            ' Read each record, set Array "TermArray(2, 1)" vals and read next record. 
            ' Get bounds of the array.
            Dim bound0 As Integer = TermArray.GetUpperBound(0)
            ' Loop over all elements.
            For i As Integer = 0 To bound0
                Dim TermStr() As String = TermStreamReader.ReadLine.Split(","c)
                TermArray(i, 0) = TermStr(0)
                TermArray(i, 1) = TermStr(1)
            Next
        Catch ex As Exception 'If the file isn't in the right spot, or doesn't exist
            ' Tell the user there's a problem with the file
            MessageBox.Show("File not found or is invalid.", "Data Error")
            ' Tell the user who to contact, and the program will now close.
            MessageBox.Show("Without the file ""\bin\Debug\LoanTerms.txt"", the program cannot run." & Chr(13) & _
                            "Please contact jgrotter~snip~ for assistance." & Chr(13) & _
                            "Closing Program.", "Data Error")
            'Exit the program
            Me.Close()
        End Try
        ' Close file.
        If Not TermStreamReader Is Nothing Then
            ' Close the file, only if it is open.
            TermStreamReader.Close()
        End If
        Return TermArray
    End Function
#End Region 'End of "GetTermOptions Function" region

#Region "SetTermLabels Function"
    ' <Summary>
    ' This function sets the labels for the 3 "Standard Term" loan option Radio Buttons on the form.
    ' One odd bit of behavior: It will locate RB_Loan4 - with a TabIndex "6" - and go through to RB_Loan1 - with TabIndex "3"
    '       i.e. it goes from the last Radio Button in the Radio Button Group to the first. I haven't figured out why: so I cycle backwards
    '       through the group of Radio Buttons using an "i" counter counting down instead of up.
    ' </Summary>
    Private Function SetTermLabels()
        ' Declare the variables to find RadioButton Group and RadioButtons.
        Dim ctrl1 As Control, ctrl2 As Control
        Dim Ratetext As Double
        Dim Periodtext As Double
        Dim i As Integer = 2
        For Each ctrl1 In Me.Controls
            If ctrl1.Name = "LoanTypeBox" Then
                For Each ctrl2 In ctrl1.Controls
                    If TypeOf (ctrl2) Is RadioButton And Not ctrl2.Name = "RB_Loan4" Then
                        Periodtext = TermArray(i, 0)
                        Ratetext = TermArray(i, 1)
                        ctrl2.Text = Periodtext & " years at " & Ratetext & "%"
                        i = i - 1
                    End If
                Next
            End If
        Next
        Return Nothing
    End Function
#End Region 'End of "SetTermLabels Function" region



_HAWK_ - Couldn't have gotten here without your direction, thank you.
Was This Post Helpful? 0
  • +
  • -

#6 lucky3   User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 233
  • View blog
  • Posts: 770
  • Joined: 19-October 11

Re: Change radiobutton text based on file contents

Posted 24 August 2012 - 10:56 PM

Maybe you would want to read about methods in vb.net. Functions are used to get some return value of desired type. When you don't need a method to return something to you, and it needs just to execute some code, you use Sub method. That way you won't need to have Return Nothing at the end of function :smile2:

Oh, and congratulations on your effort. Keep up with digging the code, and if you are interested in programming more seriously, you could go through the beginners (and later advanced) course book, to really understand what is going on.

This post has been edited by lucky3: 24 August 2012 - 10:58 PM

Was This Post Helpful? 1
  • +
  • -

#7 jgrotter   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-September 10

Re: Change radiobutton text based on file contents

Posted 27 August 2012 - 04:32 PM

View Postlucky3, on 24 August 2012 - 10:56 PM, said:

Maybe you would want to read about methods in vb.net. Functions are used to get some return value of desired type. When you don't need a method to return something to you, and it needs just to execute some code, you use Sub method. That way you won't need to have Return Nothing at the end of function :smile2:

Oh, and congratulations on your effort. Keep up with digging the code, and if you are interested in programming more seriously, you could go through the beginners (and later advanced) course book, to really understand what is going on.


Hey luck3, thank you for that. I did read about that in my textbook (after you mentioned it) and changed the method to a sub. I thought functions were methods, and there were either subs or functions/methods. I didn't realize that methods were the group of code to call from other areas and could be either subs or functions.

I'm glad you mentioned this, as I've been trying to learn this as the course goes quickly by, so have been cherry-picking from the book and reading in more detail as I got time. Our textbook is Programming_in_Visual_Basic (so, a bit dated for VB.Net), and I do plan on getting another book to learn more.

You all here are awesome - partly because you take time to give a good learning experience instead of an answer - and yet again I want to take a moment to say, "Thank YOU!"
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1