Print Rows and Columns

webguy55

Member
Joined
Feb 14, 2010
Messages
12
Programming Experience
Beginner
Hello, I am not asking for someone to complete this for me..I am just asking for advice and or guidance on how to get this to work.

I am stuck on how I go about using the nested for loops.

Thanks in advance for any help.

Here is what I am trying to accomplish:

Step 4: Add code in the Draw button’s Click event to create the number of rows and columns selected, using the character selected
Use nested For loops to create the rectangle of characters based on the user selections. The outer loop will control the number of rows. Use the value selected in the Rows combo to determine the stopping value of the outer loop. Use the value selected in the Columns combo to determine the stopping value of the inner loop, which controls the number of characters per line.


Here Is my Code:

VB.NET:
Public Class MainForm

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        cb_Rows.Items.Add(1)
        cb_Rows.Items.Add(2)
        cb_Rows.Items.Add(3)
        cb_Rows.Items.Add(4)
        cb_Rows.Items.Add(5)
        cb_Rows.Items.Add(6)
        cb_Rows.Items.Add(7)
        cb_Rows.Items.Add(8)
        cb_Rows.Items.Add(9)

        cb_Columns.Items.Add(1)
        cb_Columns.Items.Add(2)
        cb_Columns.Items.Add(3)
        cb_Columns.Items.Add(4)
        cb_Columns.Items.Add(5)
        cb_Columns.Items.Add(6)
        cb_Columns.Items.Add(7)
        cb_Columns.Items.Add(8)
        cb_Columns.Items.Add(9)

        cb_Character.Items.Add("*")
        cb_Character.Items.Add("$")
        cb_Character.Items.Add("@")
        cb_Character.Items.Add("%")

    End Sub

    Private Sub btn_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Exit.Click

        Me.Close()

    End Sub

    Private Sub btn_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Clear.Click

        txt_TextBox.Clear()
        cb_Character.Text = ""
        cb_Columns.Text = ""
        cb_Rows.Text = ""

    End Sub

    Private Sub btn_Draw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Draw.Click

        Dim rows As Integer
        Dim columns As Integer
        Dim character As String

        rows = cb_Rows.Text
        columns = cb_Columns.Text
        character = cb_Character.Text



    End Sub
End Class
 
You need to set up 2 loops that perform the actions that you need to do, action 1) Create a Row , action 2) Create a Column containing the special character, action 3) Repeat action 1 & 2 for however many iterations needed.

Let's examine some psuedo code.
First lets get our information -- For example: TotalRows 6, TotalColumns 5, SpecialCharacter '$'

How do you create the first row? Isn't that basically a loop thorugh the number of columns?

VB.NET:
'Makes 1 row
For ColumnCounter < TotalColumns -- action 1
     'Make Column -- action 2
     Print SpecialCharacter
Next

Well now that we have determined how to make a single row, isn't it just a small step to add the loop for all the rows?

VB.NET:
For RowCounter < TotalRows -- action 3
     For ColumnCounter < TotalColumns -- action 1
          'Make Column -- action 2
          Print SpecialCharacter
     Next
Next
 
Thank you for replying. I have setup my for loops in a nested style but for some reason i can't get it to make more than 1 row and print more than 1 character. I even tried a workaround...this is a pretty ghetto workaround. So this is what i have so far (just going to post my draw button event):

VB.NET:
Private Sub btn_Draw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Draw.Click

        Dim rows As Integer
        Dim columns As Integer
        Dim character As String
        Dim i As Integer
        Dim strTempRow As String

        strTempRow = ""

        rows = cb_Rows.Text
        columns = cb_Columns.Text
        character = cb_Character.Text

        For i = 0 To rows - 1

            strTempRow = strTempRow.PadLeft(columns, character)
            txt_TextBox.Text = strTempRow

        Next i



        'For i = 0 To rows - 1

        'For icol = 0 To columns - 1

        'txt_TextBox.Text = character

        'Next icol

        'Next i

    End Sub

Now my workaround actually somewhat works...but it only prints 1 row...:(
 
You are assigning the output to a textbox, which by default is not multilined -- so only one line will show. A richtext box would be a better choice, however if you have to use a textbox then change the multiline property.

Also you will need to concatenate your resulting values rather than assign them to the .Text property. Which also brings up another issue you may run into, you may want to... Environment Members (System)

And actually your workaround, imho, is the more preferred way of doing it, however, if this is an assignment then you should probably do what the directions say.
 
Thanks again :)...

I checked my text box and it has been set to multiline. I put a test label in my project just to see what was happening and if the for loops were even working and here is an example of what i get. Below this will be my current code.

I added a ";" and a vbCrLF after the line that generates the characters and here is my result using 5 rows 4 cols and $ as character:

$$$$;
;
;
;
;

Its like the character generation only works once and that's it :( ... I guess in a way my code is working but its just the characters don't want to print more than one line.

here is current code:

VB.NET:
 Private Sub btn_Draw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Draw.Click

        Dim rows As Integer
        Dim columns As Integer
        Dim character As String
        Dim i As Integer
        Dim strTempRow As String

        strTempRow = ""

        rows = cb_Rows.Text
        columns = cb_Columns.Text
        character = cb_Character.Text

        For i = 0 To rows - 1

            strTempRow = strTempRow.PadLeft(columns, character) & ";" & vbCrLf
            txt_TextBox.Text = strTempRow
            test_lbl.Text = strTempRow

        Next i



        'For i = 0 To rows - 1

        'For icol = 0 To columns - 1

        'txt_TextBox.Text = character

        'Next icol

        'Next i

    End Sub
 
Try changing the textbox.Text and label.Text = symbol to &=. This will concatenate the new text on to the previous text.

Example:
txt_TextBox.Text &= strTempRow
 
That did it!!! Thanks!!

Here is final code (in case someone else has these problems):

VB.NET:
Private Sub btn_Draw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Draw.Click

        Dim rows As Integer
        Dim columns As Integer
        Dim character As String
        Dim i As Integer
        Dim strTempRow As String

        strTempRow = ""

        rows = cb_Rows.Text
        columns = cb_Columns.Text
        character = cb_Character.Text

        For i = 0 To rows - 1

            strTempRow = strTempRow.PadLeft(columns, character)
            txt_TextBox.Text &= strTempRow & vbCrLf

        Next i

    End Sub
 
Back
Top