.net Rectangle Snapto
.net Rectangle Snapto
.net Rectangle Snapto
.net Rectangle Snapto
.net Rectangle Snapto
.net Rectangle Snapto .net Rectangle Snapto .net Rectangle Snapto .net Rectangle Snapto .net Rectangle Snapto .net Rectangle Snapto .net Rectangle Snapto .net Rectangle Snapto
.net Rectangle Snapto .net Rectangle Snapto
.net Rectangle Snapto
Go Back  Xtreme Visual Basic Talk > > > .net Rectangle Snapto


Reply
 
Thread Tools Display Modes
  #1  
Old 06-19-2013, 01:02 AM
rski rski is offline
Newcomer
 
Join Date: Jun 2013
Posts: 10
Default .net Rectangle Snapto


I am trying to create a program that will simulate graphing paper. I do a lot of work drawing on graphing paper daily and this would be easier accomplished on the computer as I wouldn't have to erase mistakes with a pencil/paper. I am taking beginners courses in .net programming so I figured this would be a good way of learning. I know there are millions of programs out there that do this for you, but again, I would like to create my own. I know how to draw the basic rectangles based on the mouse down event, and I know how to set the picture box to draw a grid. Where I am having issues though is telling the mouse down event to "snap" the rectangle to the grid for me, so I don't have to literally click the tiny pixel in every top left corner of the grid I would like to color in. Here is my code thus far:

Code:
Public Class Form1
    Dim mClick = False

    Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
        mClick = True
        PictureBox1.CreateGraphics.FillRectangle(Brushes.Black, e.X, e.Y, 15, 15)
    End Sub

    Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
        mClick = False
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PictureBox1.Refresh()
    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        For x = 0 To 500 Step 15
            e.Graphics.DrawLine(Pens.Black, x, 0, x, 500)
        Next
        For y = 0 To 500 Step 15
            e.Graphics.DrawLine(Pens.Black, 0, y, 500, y)
        Next
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click

    End Sub
End Class
The extra mClick on the mouse up event was because I was debating on having a draw feature, but I decided against it, as I simply want to be able to color in boxes on a grid, one by one.

I am new to coding, so I don't know how close this is to what I would like to achieve, but I figure this is the basics or "skeleton" of the working program I would like to finish, thanks.
Reply With Quote
  #2  
Old 06-19-2013, 06:15 AM
DrPunk's Avatar
DrPunk.net Rectangle Snapto DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

If I understand correctly then the following code should do it.

First of all, I've ditched the picturebox and just using a form for this example. There's not much difference using the two in this case. Also, instead of writing 15 everywhere for the cellsize, it's worth using a constant that if you want to change the size of a cell then you only have to change the code in one place.
Code:
Private Const CELLSIZE As Integer = 15
As for drawing the grid, I'd preferably use the size of the control instead of a fixed number to work out how many lines need to be drawn...
Code:
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    Dim i As Integer

    For i = 0 To CInt(Me.ClientRectangle.Width / CELLSIZE)
        e.Graphics.DrawLine(Pens.Black, i * CELLSIZE, 0, i * CELLSIZE, Me.ClientRectangle.Height)
    Next

    For i = 0 To CInt(Me.ClientRectangle.Height / CELLSIZE)
        e.Graphics.DrawLine(Pens.Black, 0, i * CELLSIZE, Me.ClientRectangle.Width, i * CELLSIZE)
    Next
End Sub
It divides the dimension of the form by the size of the cell for how many lines there should be and then draws each line.

Then for filling the cell. My math's isn't brilliant, but this works. Mainly because of the differences between "\" and "/" in VB (explained here -> http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx). Basically it calculates the position of the cell from the x and y clicked and then paints that cell.
Code:
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    Dim x As Integer
    Dim y As Integer

    x = e.X \ CELLSIZE
    y = e.Y \ CELLSIZE

    Dim g As Graphics = Me.CreateGraphics
    g.FillRectangle(Brushes.Red, New Rectangle((x * CELLSIZE) + 1, (y * CELLSIZE) + 1, CELLSIZE - 1, CELLSIZE - 1))
    g.Dispose()
End Sub
The "+ 1"s and "- 1"s are so that it doesn't paint over the grid.

I dare say there are better ways of doing it.
__________________
There are no computers in heaven!
Reply With Quote
  #3  
Old 06-19-2013, 06:30 AM
DrPunk's Avatar
DrPunk.net Rectangle Snapto DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Another way of doing it, if you aren't happy with the hocuspocus of "\", would be to use Mod to take off the difference of where the cursor was clicked to find the top left coordinate of the cell that was clicked.
Code:
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    Dim x As Integer
    Dim y As Integer

    x = e.X - (e.X Mod CELLSIZE)
    y = e.Y - (e.Y Mod CELLSIZE)

    Dim g As Graphics = Me.CreateGraphics
    g.FillRectangle(Brushes.Red, New Rectangle(x + 1, y + 1, CELLSIZE - 1, CELLSIZE - 1))
    g.Dispose()

End Sub
__________________
There are no computers in heaven!
Reply With Quote
  #4  
Old 06-19-2013, 08:34 AM
rski rski is offline
Newcomer
 
Join Date: Jun 2013
Posts: 10
Default

Quote:
Originally Posted by DrPunk View Post
Another way of doing it, if you aren't happy with the hocuspocus of "\", would be to use Mod to take off the difference of where the cursor was clicked to find the top left coordinate of the cell that was clicked.
Code:
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    Dim x As Integer
    Dim y As Integer

    x = e.X - (e.X Mod CELLSIZE)
    y = e.Y - (e.Y Mod CELLSIZE)

    Dim g As Graphics = Me.CreateGraphics
    g.FillRectangle(Brushes.Red, New Rectangle(x + 1, y + 1, CELLSIZE - 1, CELLSIZE - 1))
    g.Dispose()

End Sub
Hey,

Thanks so much, that was exactly what I was looking for. The cell's being defined as a constant cleaned the code up a lot, plus having the total width and height of the cells be defined by the dimensions of the form was pure genius.

I have 1-2 more questions though, if you don't mind sir. First, I was trying to come up with a "delete" option, for when I were to make a mistake. With the paint box, I was just using a "refresh" parameter, it wasn't ideal as it would wipe all data contained in the paint box and I would have to start over on my drawing. What I came up with was to the middle mouse click a different color, one that matched the background of the form, and that way when I made a mistake I could simply middle mouse click that cell and change it's color to the bg color, kind of like an ignorant mans eraser, if you will. This effectively changed the color of all of the cells though, previous ones I had drawn in as well. I couldn't find a "Right Mouse Click" event, so I just went with mouse wheel.

"Make Shift Eraser Code"

Code:
    Private Sub Form1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        Dim x As Integer
        Dim y As Integer

        x = e.X \ CELLSIZE
        y = e.Y \ CELLSIZE

        Dim g As Graphics = Me.CreateGraphics
        g.FillRectangle(Brushes.LightGray, New Rectangle((x * CELLSIZE) + 1, (y * CELLSIZE) + 1, CELLSIZE - 1, CELLSIZE - 1))
        g.Dispose()
    End Sub
The next question was; I was trying to change the color of the brush dynamically, i.e. Brushes."Textbox1.Text" or something like that. Would my only option be to use if/then variables, such as if checkbox1.checked or whatever then Brushes.Green or Brushes.Brown, etc?
Reply With Quote
  #5  
Old 06-19-2013, 09:07 AM
DrPunk's Avatar
DrPunk.net Rectangle Snapto DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Quote:
The first issue was assigning another event to change the color of a cell in-case I messed up on my measurements and had to "erase" a cell square, I figured that out, though. I just assigned the mouse wheel event to the color white and set my form bg as white as well, this way it would give the appearance of being "erased". Would there be a way to use the right mouse button though, instead of the mouse wheel event?

Second question, can I change the "Brush.Color" dynamically, like "Brush.Textbox1.Text" or the like, or am I going to have to assign if/then statements linked to check boxes or the like, i.e. if checkbox1.checked = true then "Brush.Black" or "Brush.Red" etc.?
The MouseEventArgs passed to the mouse event contains a Button property. You can check what that button is set to to determine the mouse button that was pressed.

Code:
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    Dim x As Integer
    Dim y As Integer

    Dim b as SolidBrush
    if e.Button = Windows.Forms.MouseButtons.Left Then
        b = new SolidBrush(Color.Black)
    elseif e.button = Windows.Forms.MouseButtons.Right Then
        b = new SolidBrush(Color.White)
    else
        ' Don't draw for any other button
        exit sub
    end if

    x = e.X - (e.X Mod CELLSIZE)
    y = e.Y - (e.Y Mod CELLSIZE)

    Dim g As Graphics = Me.CreateGraphics
    ' Use the created brush to draw with
    g.FillRectangle(b, New Rectangle(x + 1, y + 1, CELLSIZE - 1, CELLSIZE - 1))
    g.Dispose()
    b.dispose
End Sub
I don't know if that goes some way to answer your second question or not. Note you can create any color brush with...
Code:
Dim b as new SolidBrush(Color.FromARGB(0,0,0))
__________________
There are no computers in heaven!
Reply With Quote
  #6  
Old 06-19-2013, 09:19 AM
rski rski is offline
Newcomer
 
Join Date: Jun 2013
Posts: 10
Default

If they approve my original reply, you will see the errors I was making, for starters I had not changed mouse wheel to handle the mouse wheel, it was still set to handle mouse down, which caused me loads of headaches, I got that sorted out. I then finally thought to use if/else to associate the right mouse button.

Finally, to erase the whole setup and start over i did a Me.Refresh() call. That works out perfectly.

The last issue I have is wanting to change those colors on the fly, so say I have a button and I label "button1" as "Red", when I click that button, I wanted to change:

b = new SolidBrush(Color.Black) to say b = new SolidBrush(Color.Red) or whatever color, dynamically. Basically change the color of the brush by clicking a button.

I would normally do something like this by using b = new SolidBrush(Color.Textbox1.Text), but I have never worked with anything graphical in .net and this does not seem to be the correct syntax.
Reply With Quote
  #7  
Old 06-19-2013, 09:30 AM
DrPunk's Avatar
DrPunk.net Rectangle Snapto DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

You can make a color selector easily enough.

Say you have a picturebox to display the selected colour (set its borderstyle to Fixed3D so it looks like a control). The BackColor property becomes the Color you are going to use.

Then you just set it so when you click that picturebox it displays a color picker dialog which if OKed then it sets the BackColor to the selected color.
Code:
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
    Dim cp As New ColorDialog

    If cp.ShowDialog = Windows.Forms.DialogResult.OK Then
        PictureBox1.BackColor = cp.Color
    End If

    cp.dispose()
End Sub
When painting you create a brush with that color
Code:
Dim b as New SolidBrush(Picturebox1.BackColor)
Simple enough to do 2 pictureboxes, one for left click color and one for right click color.

Don't forget to initilalise the pictureboxes with colors when the form loads or they'll both be grey.
__________________
There are no computers in heaven!
Reply With Quote
  #8  
Old 06-19-2013, 09:43 AM
rski rski is offline
Newcomer
 
Join Date: Jun 2013
Posts: 10
Default

Thanks, that worked beautifully. For whatever reason I was convinced I was going to have to use if then statements throughout, just to change the color of my brush. I appreciate all of your help today.
Reply With Quote
  #9  
Old 06-19-2013, 10:17 AM
Gruff's Avatar
Gruff.net Rectangle Snapto Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Nice work DrPunk,

rski,

The only thing I might consider if I were to write such a program is persistence.

1st Use the Paint event to do all your graphics.
There are issues with CreateGraphics()

2nd I would use a 2D array of type Color to holds a map of your grid. (This way you simply display the map in your paint event.
One other benefit is that now you can also save your map to disk and load it again later.

3rd Snapping: I do not know if you still want to do this but if you do basically you
use the MouseMove event and override the mouse x,y position by using the same method you used in MouseDown only adjust the virtual grid to start 1/2 grid offsect in x and y. This will make your mouse cursor snap to the center of each cell when you move it.

4th your color palette could be as simple as your form/picturebox.
Use the same grid system to create your palette and pick from it.. Put a 2D array of color behind it as well. This way your could have multiple color palettes on disk you could flip through.

---

I guess if it were me I would create a user control that drew a grid based on properties
that way I could use it for both the main grid and the color palette grid.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 06-21-2013 at 01:15 PM.
Reply With Quote
  #10  
Old 06-19-2013, 11:05 AM
rski rski is offline
Newcomer
 
Join Date: Jun 2013
Posts: 10
Default

Gruff, I have just started reading up about 2D arrays last night. Between work and caring for my daughter I have roughly 1-2 hours per day to devote to studies, so I have not fully dove into them yet. I understand that it's a more effective way of accomplishing the tasks I am looking to accomplish, but at the moment it's a bit ambitious for me.

I have been reviewing the code provided by DrPunk over and over again, making sure I fully understand what is happening and why. Besides making a few programs utilizing sendkeys, timers, basic variables, etc..I haven't gotten very far into this yet.

I started out years and years ago making .bat files to launch program/games on my computer, I then "graduated" to qbasic, doing pretty much the same things, with some added functions such as running netstat or a ping. Life took over from there and I am now trying to get back into it all again.

I was playing around with the code a minute ago and I tried to place a checkbox on the form, and added the code:

Code:
If CheckBox1.Checked = True Then
            Dim i As Integer

            For i = 0 To CInt(Me.ClientRectangle.Width / CELLSIZE)
                e.Graphics.DrawLine(Pens.Black, i * CELLSIZE, 0, i * CELLSIZE, Me.ClientRectangle.Height)
            Next

            For i = 0 To CInt(Me.ClientRectangle.Height / CELLSIZE)
                e.Graphics.DrawLine(Pens.Black, 0, i * CELLSIZE, Me.ClientRectangle.Width, i * CELLSIZE)
            Next

        Else

            For i = 0 To CInt(Me.ClientRectangle.Width / CELLSIZE)
                e.Graphics.DrawLine(Pens.White, i * CELLSIZE, 0, i * CELLSIZE, Me.ClientRectangle.Height)
            Next

            For i = 0 To CInt(Me.ClientRectangle.Height / CELLSIZE)
                e.Graphics.DrawLine(Pens.White, 0, i * CELLSIZE, Me.ClientRectangle.Width, i * CELLSIZE)
            Next
        End If
Now this works fine, I can draw the grid in white or black depending on if the checkbox is checked or not, but I am forced to refresh the form to see the changes take effect, thus wiping all of the work I had completed. I tried to create a conditional loop but it effectively locked the program and I was forced to close the process in task manager. Any ideas on this one?
Reply With Quote
  #11  
Old 06-19-2013, 05:09 PM
Gruff's Avatar
Gruff.net Rectangle Snapto Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

This is why I suggested you not use CreateGraphics.

In VB.NET the proper why to draw graphics is to use the paint event of whatever object you are trying to draw on.

If you use the paint event windows know when to fire the event and does it automatically. This saves you from having to handle all the things that can cause your display to get visually corrupted.

With VB.NET you can force the paint event to fire by using the Invalidate method of the object you are drawing on.

I've been working up a sample control for you. I used an inherited Picturebox control instead of a usercontrol. Inheritance is one of the very powerful tools that you never had under previous versions.

I will attach the code for the class and a sample form based program to run it.
It is just he bare bones but you could enhance it to get what you want.

Basically it has all the properties and methods of a standard Picturebox plus enhancements that do grid drawing for you. The cool thing about using this technology is that it makes your final program much much simpler.

The new control is called PictureBoxGrid.
It is meant to be a sample. It contains no error checking code.
You draw it on your form just like a picturebox.

New Properties:
-------
GridColor: color of the grid lines.
GridCellColor: current drawing color
GridColWt: cell width
GridRowHt: cell height
GridColorFlowIn: boolean toggles setting a cell color vs getting the cell color on mousedown.

New Events
-----
GridCellColorChanged Fires on mousedown if GridColorFlowin = false

New Methods:
-----
SetCellColor() Allow you to preset some cell colors as in a color palette
GridSave(<filename>) Save your grid pattern to a text file
GridLoad(<filename) Loads your grid pattern from a text file.

Features:
-----
Right mouse draws the background color. (undo the cell color)


Code to follow...
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 06-19-2013 at 06:09 PM.
Reply With Quote
  #12  
Old 06-19-2013, 05:21 PM
Gruff's Avatar
Gruff.net Rectangle Snapto Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

' *** Install instructions moved to POST #19 below. ***

Code:
Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load With PaletteGrid .GridColorFlowIn = False .GridColor = Color.White ' Fill the palette with some colors .SetCellColor(0, 0, Color.White) .SetCellColor(1, 0, Color.Yellow) .SetCellColor(2, 0, Color.Green) .SetCellColor(3, 0, Color.Blue) .SetCellColor(0, 1, Color.Magenta) .SetCellColor(1, 1, Color.Red) .SetCellColor(2, 1, Color.Gold) .SetCellColor(3, 1, Color.Black) End With With Grid .GridColorFlowIn = True .BackColor = Color.White .GridColor = Color.DarkCyan .GridCellColor = Color.Red End With End Sub Private Sub PaletteGrid_GridCellColorChanged(ColorValue As System.Drawing.Color) Handles PaletteGrid.GridCellColorChanged ' you clicked on the palette. Make the value the drawing color in the Grid control. Grid.GridCellColor = ColorValue End Sub Private Sub cmdSave_Click(sender As System.Object, e As System.EventArgs) Handles cmdSave.Click Grid.GridSave("C:\Test.txt") MessageBox.Show("Grid Pattern Saved") End Sub Private Sub cmdLoad_Click(sender As System.Object, e As System.EventArgs) Handles cmdLoad.Click Grid.GridLoad("C:\Test.txt") MessageBox.Show("Grid Pattern loaded") End Sub End Class
Attached Images
File Type: png Sample Grid Drawing App.png (22.7 KB, 12 views)
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 06-21-2013 at 01:33 PM.
Reply With Quote
  #13  
Old 06-19-2013, 05:23 PM
rski rski is offline
Newcomer
 
Join Date: Jun 2013
Posts: 10
Default

Thanks, it's starting to make sense now. I should have explained all of my needs in the beginning. I am a scatter brain at times and focusing on details has become more of a chore in my older years lol. The "While" loop I am trying to inject in the creategraphics is creating an endless loop and thus breaking the program, I appreciate you guys taking the time out of your day to help me with these issues. I never move on to another project without fully understanding my current one, so this has been invaluable information you have gentlemen have provided.
Reply With Quote
  #14  
Old 06-19-2013, 05:26 PM
Gruff's Avatar
Gruff.net Rectangle Snapto Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Code to create the PictureBoxGrid Class (Control)

Code:
Imports System Imports System.IO Public Class PictureBoxGrid Inherits PictureBox Private Grid(,) As Color = ResizeGrid(RowCount, ColCount) Private RowCount As Integer = 0 Private ColCount As Integer = 0 #Region "Property vaults" Private _GridColwt As Integer = 20 Private _GridRowHt As Integer = 20 Private _GridColor As Color = Color.Black #End Region #Region "Properties" Public Property GridColWt As Integer Get Return _GridColwt End Get Set(value As Integer) _GridColwt = value Me.Invalidate() End Set End Property Public Property GridRowHt As Integer Get Return _GridRowHt End Get Set(value As Integer) _GridRowHt = value Me.Invalidate() End Set End Property Public Property GridColor As Color Get Return _GridColor End Get Set(value As Color) _GridColor = value Me.Invalidate() End Set End Property Public Property GridColorFlowIn As Boolean = True Public Property GridCellColor As Color = Color.Red #End Region #Region "New Events" Public Event GridCellColorChanged(ColorValue As Color) #End Region #Region "Event Overrides" Protected Overrides Sub OnSizeChanged(e As System.EventArgs) MyBase.OnSizeChanged(e) ColCount = CInt(Me.ClientRectangle.Width / GridColWt) RowCount = CInt(Me.ClientRectangle.Height / GridRowHt) Grid = ResizeGrid(RowCount, ColCount) End Sub Protected Overrides Sub OnPaint(pe As System.Windows.Forms.PaintEventArgs) MyBase.OnPaint(pe) With pe.Graphics Dim GridPen As Pen = New Pen(GridColor) Dim Cols As Integer = CInt(Me.ClientRectangle.Width / GridColWt) Dim Rows = CInt(Me.ClientRectangle.Height / GridRowHt) For Y As Integer = 0 To RowCount For X As Integer = 0 To ColCount Dim R As New Rectangle(X * GridColWt, Y * GridRowHt, GridColWt, GridRowHt) Dim CellBrush As New SolidBrush(Grid(X, Y)) .FillRectangle(CellBrush, R) .DrawRectangle(GridPen, R) Next Next End With End Sub Protected Overrides Sub OnMouseDown(e As System.Windows.Forms.MouseEventArgs) MyBase.OnMouseDown(e) Dim X As Integer = e.X \ GridColWt Dim Y As Integer = e.Y \ GridRowHt If GridColorFlowIn Then If e.Button = Windows.Forms.MouseButtons.Left Then Grid(X, Y) = GridCellColor ElseIf e.Button = Windows.Forms.MouseButtons.Right Then Grid(X, Y) = Me.BackColor End If Else 'GridCellColor = Grid(X, Y) RaiseEvent GridCellColorChanged(Grid(X, Y)) End If Me.Invalidate() End Sub #End Region #Region "Procedures" Public Sub SetCellColor(X As Integer, Y As Integer, ColorValue As Color) Grid(X, Y) = ColorValue End Sub Public Sub GridSave(FileName As String) 'Array dimensions as the first line Dim sOut As String = RowCount & "," & ColCount & vbCrLf '2D array of colors For y As Integer = 0 To RowCount For x As Integer = 0 To ColCount sOut &= Grid(x, y).ToArgb.ToString If x < ColCount Then sOut &= "," Next sOut &= vbCrLf Next File.WriteAllText(FileName, sOut) End Sub Public Sub GridLoad(filename As String) Dim sLines() As String = File.ReadAllLines(filename) Dim sHeader() As String = sLines(0).Split(","c) Dim Rows As Integer = CInt(sHeader(0)) Dim Cols As Integer = CInt(sHeader(1)) If Not (Rows = RowCount And Cols = ColCount) Then MessageBox.Show("Saved 2D array is not the same size as the current 2D array") Exit Sub End If Dim y As Integer = 0 For i As Integer = 1 To sLines.Count - 1 Dim Cells() As String = sLines(i).Split(New String() {","c}, StringSplitOptions.None) For x = 0 To Cells.Length - 1 Dim sArgb As String = Cells(x) Grid(x, y) = Color.FromArgb(CInt(sArgb)) Next y += 1 Next Me.Invalidate() End Sub #End Region #Region "Supporting Routines" Private Function ResizeGrid(Rows As Integer, Cols As Integer) As Color(,) Dim NewGrid(Cols, Rows) As Color Return NewGrid End Function #End Region End Class
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 06-19-2013 at 05:35 PM.
Reply With Quote
  #15  
Old 06-19-2013, 05:37 PM
rski rski is offline
Newcomer
 
Join Date: Jun 2013
Posts: 10
Default

Quote:
Error 9 'GridCellColorAquired' is not an event of 'Pixel_Me_v2.PictureBoxGrid'. C:\Users\admin\AppData\Local\Temporary Projects\Pixel Me v2\PictureBoxGrid.vb 108 24 Pixel Me v2
I can't get it to build at all. There are 9 other errors on the form, but those are related to missing control we are trying to build in the new class you created.
Reply With Quote
  #16  
Old 06-19-2013, 06:03 PM
rski rski is offline
Newcomer
 
Join Date: Jun 2013
Posts: 10
Default

I've rebuilt it and it seems okay, until I go to run the project and then I am stuck with this error, I cannot save, build, debug, run, etc.

Quote:
Error HRESULT E_FAIL has been returned from a call to a COM component.
Reply With Quote
  #17  
Old 06-19-2013, 06:10 PM
rski rski is offline
Newcomer
 
Join Date: Jun 2013
Posts: 10
Default

So it seems that it is either not building the PaletteGrid class properly, or it does not have access to it. I am stuck here though. Thanks again for your help.
Reply With Quote
  #18  
Old 06-19-2013, 09:02 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

Very nice grid class Gruff.
I thought you forgot the
delete part of rski question
but then i right clicked on
a color in the grid and it was
replaced by a white square.

Very nice job old man..
Reply With Quote
  #19  
Old 06-19-2013, 10:52 PM
Gruff's Avatar
Gruff.net Rectangle Snapto Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

No biggie, Just fleshed out DrPunks / rski concept to use a map so drawings could be saved, loaded, and displayed in the paint event. Then wrapped the whole thing in an inherited picturebox for ease of use.

rski: I made several edits to the code I posted. You probably copied one of the earlier versions. The event name was changed after about three edits.

Hmmm. I am home now and I tried to follow my own instructions. Boom. Errors.

Looks Like I gave the instructions in the wrong order.
Should be.

1) Open a new win forms project.
2) Add the PictureboxGrid empty class.
3) Copy the PictureBoxGrid Class Code from the forum into the empty class.
4) Build the project.
Now there should be a PictureboxGrid control somewhere in the controls toolbox,
5) Draw the two pictureboxGrid controls onto the form.
6) Rename them PaletteGrid and Grid.
7) Size and position them. 4 x 2 and 16 x 16
8) Add two button controls
9) Rename them cmdSave and cmdLoad.
10) Change their text properties to read 'Save' and 'Load'

11) Lastly copy the form code from the forum and paste it into the empty form.

12 Rebuild one last time.
You should have no errors.

Run the program and try it out.

Do you see how encapsulating the primary function of the graphics into a control makes
the main program utterly simple? Outside of the load event there's what... three small procedures.

You can add all sorts of controls to the form to adjust other PictureboxGrid properties.
For example: You wanted to control the grid line color on the fly.

You could use a slider control to adjust the PictureBoxGrid.GridColor property with but a tiny bit of code.
You could also put an empty label with a border on the form.
Change its background color in the GridCellColorChanged() event so you have feedback as to what color was picked. Etc...
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 06-24-2013 at 12:37 PM.
Reply With Quote
  #20  
Old 06-20-2013, 08:13 AM
rski rski is offline
Newcomer
 
Join Date: Jun 2013
Posts: 10
Default

Yes, I could see the edits in the post's after the fact. I should have realized I needed to build the class beforehand. Thank you very much as this is working well.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
.net Rectangle Snapto
.net Rectangle Snapto
.net Rectangle Snapto .net Rectangle Snapto
.net Rectangle Snapto
.net Rectangle Snapto
.net Rectangle Snapto .net Rectangle Snapto .net Rectangle Snapto .net Rectangle Snapto .net Rectangle Snapto .net Rectangle Snapto .net Rectangle Snapto
.net Rectangle Snapto
.net Rectangle Snapto
 
.net Rectangle Snapto
.net Rectangle Snapto
 
-->