PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Loop-VBForums
Results 1 to 4 of 4

Thread: Loop

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    3

    Loop

    Can anyone tell me how to write the code for this one. this one use for or Do While loop. It will only use Command button and the loop will be printed in the form itself ones the Command button was pressed. No Textbox or other toolbox menu needed. This loop is in the form of Pyramid not a right triangle.
    1
    123
    12345
    1234567
    123456789

    Thanks for the kindness people. jerald-12 from the Philippines

  2. #2

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    3

    Re: Loop

    That one is in the form of PYRAMID not right triangle. Thanks.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,726

    Re: Loop

    Getting the shape of a pyramid is going to be a bit tough, as the size of the characters is different for different characters in most fonts. Using a font with equal size per character would make it easier.

    How do you intend to display this, and how many rows? There has to be some control that the string of numbers is displayed on, such as a series of labels, or a listbox, or some such.
    My usual boring signature: Nothing

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,968

    Re: Loop

    heres an example i wrote:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim sb As New System.Text.StringBuilder
    4.     Dim newFont As New Font("MS Sans Serif", 10)
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         Dim rangeStart As Integer = 1
    8.         Dim rangeEnd As Integer = 1
    9.         Dim lines(4) As String
    10.  
    11.         For y As Integer = 1 To 5
    12.             For x As Integer = -9 To -2
    13.                 If x >= rangeStart Then
    14.                     lines(y - 1) &= Math.Abs(x).ToString
    15.                 End If
    16.             Next
    17.             For x As Integer = 1 To 9
    18.                 If x <= rangeEnd Then
    19.                     lines(y - 1) &= x.ToString
    20.                 End If
    21.             Next
    22.             If y > 1 Then rangeStart -= 2 Else rangeStart -= 4
    23.             rangeEnd += 2
    24.         Next
    25.  
    26.         Dim newLines As New List(Of String)
    27.  
    28.         For y As Integer = 5 To 1 Step -1
    29.             If y = 5 Then
    30.                 newLines.Add(String.Format("{0,-1}{1}", "", lines(y - 1)))
    31.             Else
    32.                 newLines.Add(String.Format("{0," & -((Math.Abs(y - 5) * 4) + 1) & "}{1}", "", lines(y - 1)))
    33.             End If
    34.         Next
    35.  
    36.         newLines.Reverse()
    37.  
    38.         sb.Append(String.Join(Environment.NewLine, newLines.ToArray))
    39.  
    40.         Dim gr As Graphics = Graphics.FromHwnd(Me.Handle)
    41.         Dim maxWidth As Integer = CInt(gr.MeasureString(newLines(newLines.Count - 1), newFont).Width)
    42.         Dim maxHeight As Integer = CInt(gr.MeasureString(newLines(newLines.Count - 1), newFont).Height) * 5
    43.  
    44.         Me.SetClientSizeCore(maxWidth + 100, maxHeight + 100)
    45.  
    46.         Me.Invalidate()
    47.  
    48.     End Sub
    49.  
    50.     Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    51.         e.Graphics.DrawString(sb.ToString, newFont, Brushes.Black, New Point(50, 50))
    52.     End Sub
    53.  
    54. End Class
    Attached Images Attached Images  
    Last edited by .paul.; Jan 7th, 2010 at 12:18 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width