Link to home
Start Free TrialLog in
Avatar of Mezillinu
MezillinuFlag for Malta

asked on

How do I convert this in a function? Only 10 lins of code, couldnt seem to find out how!

I am drawing a grid in a panel at the load, and am using the panels paint,

"handles panel1.paint"

I cannot seem to convert the code I did to create a grid, into a function so I could pass the column numbers and rows in a drop down lists or some other control, to give the user some flexibility.

can somebody help me out?  

What I could not get rid of is the E.

I want to pass the panel into it, so I could draw a grid in it, and pass column number, row number, row height and column width.

All help appreciated!

Regards
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
        'Start drawing lines and continue adding your own styles
        Dim rowHeight As Integer = 16
        Dim rowCount As Integer = 100
        Dim columnWidth As Integer = 10
        Dim columnCount As Integer = 50
        For i As Integer = 0 To rowCount
            e.Graphics.DrawLine(Pens.Black, 0, i * rowHeight, columnCount * columnWidth, i * rowHeight)
        Next
        For j As Integer = 0 To columnCount
            e.Graphics.DrawLine(Pens.Black, (j * columnWidth), 0, (j * columnWidth), rowCount * rowHeight)
        Next
    End Sub

Open in new window

Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Avatar of Mezillinu

ASKER

Hi idle mind - could you show me how on each step of the process of the game for it to be placed in a list box.

example if you input F1R3F2 in the algorithm, and currently executing F1, F1 is shown in the list box, and if R3 is currently being executed it is incremented in the list box.

if an error occours in R3, then its color is changed or something to indicate that something has gone wrong

could you please show me how to do this please

thanks :-)
Hi Mezillinu,

I can certainly show you how to make these changes...

...what exactly am I helping you with here though?  Is this homework?  A take home test?  A "final" project?  Extra Credit?  Tutoring help for someone?  Something else?...

Well,

it is difficult to explain but between homework and extra credit I guess.

the problem is that in the code i managed to increment and all that, but it all happened at once, and could not make it do the increment step by step as i could not understand were you parsed the string/algorithm given - and how did you make this execute 2 characters by 2 characters, or pairs if you may call them pairs.

if you could, this is the only and last part I guess i will need help with

thanks for all the help
The parsing code is here:

    Public Sub ExecuteCommandString(ByVal cmds As String)
        cmdQueue = New Queue(Of String)
        Dim value As Integer
        For Each m As Match In Regex.Matches(cmds.ToUpper, "[FRL]\d*")
            If Integer.TryParse(m.ToString.Substring(1), value) Then
                For i As Integer = 1 To value
                    cmdQueue.Enqueue(m.ToString.Substring(0, 1))
                Next
            Else
                MessageBox.Show(m.ToString, "Invalid Number", MessageBoxButtons.OK, MessageBoxIcon.Error)
                RaiseEvent BadCommandString(Me)
                Exit Sub
            End If
        Next
        tmr.Start()
    End Sub
 
The "guts" of parsing is done using the Regular Expression engine in this line:

    For Each m As Match In Regex.Matches(cmds.ToUpper, "[FRL]\d*")

The RegEx is "[FRL]\d*" which translates to: Find any occurrence of the letters "F", "R", "L" that is followed by one or more digits.

So if you passed in "F1R3F2", the engine would spit back out "F1", "R3", and "F2" via the "m" parameter in the looping structure.

The next part grabs just the NUMERIC portion of the "pair" and attempts to convert it to an Integer.  If successful, it then "expands" the command out into the appopriate number of commands:

            If Integer.TryParse(m.ToString.Substring(1), value) Then
                For i As Integer = 1 To value
                    cmdQueue.Enqueue(m.ToString.Substring(0, 1))
                Next
            Else
                MessageBox.Show(m.ToString, "Invalid Number", MessageBoxButtons.OK, MessageBoxIcon.Error)
                RaiseEvent BadCommandString(Me)
                Exit Sub
            End If

If the current command in "m" was "R3", it would add THREE SEPERATE "R" commands to the queue:

                For i As Integer = 1 To value
                    cmdQueue.Enqueue(m.ToString.Substring(0, 1))
                Next

A command string of "F1R3F2" would become this in the queue:

    F
    R
    R
    R
    F
    F

The Timer then simply dequeues each command and executes it in turn:

    Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
        If cmdQueue.Count > 0 Then
            Select Case cmdQueue.Dequeue
                Case "F"
                    Me.MoveForward()

                Case "R"
                    Me.TurnRight()

                Case "L"
                    Me.TurnLeft()

            End Select
        Else
            tmr.Stop()
            RaiseEvent Failed(Me)
        End If
    End Sub
So

  For i As Integer = 1 To value
                    cmdQueue.Enqueue(m.ToString.Substring(0, 1))
                Next


If I add a parameter, and pass a listview, and then put like listview.items.add(m)

it would increment? no, if I do this, which I have already done so - it will first put the items, and then start the actions.

or is this because of the timer? hmmm, like since the actions are based on the timer, and the incrementing is not? that I do not know

no no, I understand most of the code, in not that beginner in vb.net either - lets say around a year experience more or less using asp.net, vb.net in a work environment.  but usually i work with web based methods/classes & sql server. i am not used to using graphics for one, and raising events is completely something new for me including some other stuff in your code but all is understandable i guess.  

the logic is a bit hard to follow :s but one day i wish i could code out fast! its funny, after a year, i'm just getting the basic stuff going out by heart, but not anything else :s

but anyways experience makes perfect :-)
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks :-) Thats works perfectly!

Is there any way to make the grid never go larger than the actual window? Like if I put the maximum number of columns and rows, the grid goes way larger than the window - which makes no sense.

And are there any other approaches to make this game? So I could redo it, this time using different approaches to methods and classes and inputting a lot of MY effort this time?

And another question which is not related to this question at all, which I am going to post another thread. Have you ever worked with error handling techniques? I am planning to build an error handler that could be attached to the global.ascx, just a link to it and it could be a DLL, a class, well basically - it can be attached to global.ascx.

The point of this is to
1)Detect differences between errors example run time errors, sql errors, index out of range errors, and all those
2)Send an email to the people that are listed in a table with a strack trace (Detailed version) of the error and were it came from
3)a user could decide for example that user 1, could recieve index out of range errors, user 2 only recieves sql errors - depending on what they work with actually.

Now 2 and 3 - I have no problem creating, could you please help me out with option 1?

If you know of any handy links which I can use, you do not have to supply code or anything, although I would not not accept it either

Thanks for your time, and excellent help that you have supplied to me