Windows Develop Bookmark and Share   
 index > Windows Forms General > PROGRAMMING STUDENT NEEDING URGENT HELP FOR ASSIGNMENT
 

PROGRAMMING STUDENT NEEDING URGENT HELP FOR ASSIGNMENT

If anyone would be so kind as to help.

I need to write the code of a button which will display the factors of a number (as entered in textbox by user) and display them in a listbox.  Also need to code a button that will display prime factos of same number and display in a separate listbox.

Help required asap.  Thank you.
MigrationUser 1  Thursday, August 28, 2003 7:28 AM
You're going to be hard-pressed getting someone here to do your homework for you.  But, we'll be glad to help.  First off, what pieces in particular are you having problems with?
MigrationUser 1  Thursday, August 28, 2003 11:44 AM
Hi

Thank you very much for your offer of assistance.

The program I have been told to write requires a user to input any number in the textbox, should the user then click on the factors button, the factors of the said number should be displayed in a listbox.

So far, I have tried using a For/Next loop following my guidance notes.  However I am receiving an error.

My code looks like this:

Private Sub btnFactors_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFactors.Click
        'Calculate the factors of the number entered by the user

        Dim intCount As Integer
        Dim intNumber As Integer
        Dim intSum As Integer
        Dim intArrayFactors As Integer
        intNumber = CInt(txtNumber.Text)
        Dim intFactors As Integer
        Dim intPrimeFactors As Integer
        intFactors = lstFactors.SelectedIndex
        intPrimeFactors = lstPrimeFactors.SelectedIndex

        If txtNumber.Text = "" Then 'Nothing was entered in the user input textbox
            MessageBox.Show("Input in text box required", "Input a number", _
            MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

        For intCount = 1 To Int(Math.Sqrt(intNumber))
            If intNumber Mod intCount = 0 Then
                intSum += 1
                intArrayFactors(intSum) = intNumber / intCount

            End If
        Next

    End Sub 

I have spent absolute hours on this, reading the textbook, etc and am still stuck - any help would be greatly appreciated.

Many thanks, look forward to hearing from you.
MigrationUser 1  Thursday, August 28, 2003 2:35 PM
It looks like you have the basics down. I believe your problem lies in one of your variable definitions.

Dim intArrayFactors As Integer

This is declaring an integer variable.  Later in your code you use the variable:

intArrayFactors(intSum) = intNumber / intCount

It is now being used as an array of integers.  You will need to change your declartion. I am a C# coder so forgive me if my syntax is wrong:

Dim intArrayFactors As Integer()

Arrays are not dynamic in .Net unless you use the collection classes. When you allocate the array you will need to know how many integers it will need to hold. 

intArrayFactors = new Integer(countOfInts)

You can also skip the array storage an go straight into the list box.

myListBox.Items.Add(CStr(intNumber / intCount ))


Hope this helps



MigrationUser 1  Thursday, August 28, 2003 4:47 PM
As Jacob said, I didn't do your homework for you but I did clean up your code to get rid of all the errors. It still doesn't do anything. I'll leave that to you. Good luck!    Private Sub btnFactors_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFactors.Click

        Dim intCount As Integer
        Dim intNumber As Integer
        Dim intSum As Integer
        Dim intArrayFactors() As Integer

        If IsNumeric(txtNumber.Text) Then
            intNumber = CInt(txtNumber.Text)
        Else
            txtNumber.Text = String.Empty
            MessageBox.Show("Enter numbers only silly!", "Numbers only", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        ReDim intArrayFactors(CInt(Math.Sqrt(intNumber)))

        If txtNumber.Text.Length = 0 Then
            MessageBox.Show("Input in text box required", "Input a number", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

        For intCount = 1 To CInt(Math.Sqrt(intNumber))
            If intNumber Mod intCount = 0 Then
                intSum += 1
                intArrayFactors(intSum) = CInt(intNumber / intCount)
            End If
        Next

    End Sub
MigrationUser 1  Thursday, August 28, 2003 11:41 PM
Thank you very much for your help, will give a go!!!
MigrationUser 1  Friday, August 29, 2003 4:11 AM

You can use google to search for other answers

Custom Search

More Threads

• How to view data in a grid view nested within a winform
• how to create a instance of this functionality
• Windows Drage and drop
• Tabpage Size issue
• Windows Service Application - Timer Issue
• open file & mycomputer
• MSFlexgrd unicode problem
• is it possible to control vertical scroll datagrid with two push buttons??????
• DataGridView Row Copying problem.
• ValidateNames property of SaveFileDialog