[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 460
  • Last Modified:

best way to build an empty rectangular grid layout graphic with Vb.net

Hey Experts,
I have a vb.net application that calculates an optimal layout for cutting sheets of material.
I am outputting the calculations results (text) into a datagridview on a main form.  That part is working BUT...
Now, I have been asked to display the layout graphics with the cuts displayed on some type of grid layout. It does not have to be exactly to scale, but it should be close.  I am starting out by trying to use another form with a dgv and using part widths and lengths as column widths and row heights. I am kinda stuck on that when I try to build a dgv with all empty cells.
Maybe there is a better way or a good way to continue with the Dgv?

Please help and thanks in advance.
RogerK
0
rogerk_fsww
Asked:
rogerk_fsww
  • 8
  • 8
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can use a TableLayoutPanel, which allows you to specify the widths of the columns as percentages of a total, thus making them to scale no matter what size you make it.  The same can be done for the rows.  You can place a Panel with a Border on, and Dock set to Fill to make it take up each Cell.  You can also make the Panels span multiple columns and/or rows.

See TableLayoutPanel:
http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.aspx
http://msdn.microsoft.com/en-us/library/h21wykkx.aspx
0
 
rogerk_fswwAuthor Commented:
Wow, That sounds like exactly like what I need!

I will try that shortly.

Thanks
Rog
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could also draw a grid  directly to a Bitmap, and then display it in a PictureBox with the SizeMode set to Zoom so it scales while maintaining the aspect ratio.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rogerk_fswwAuthor Commented:
Do you know where I can get an example of either of these methods?
I am not familiar with any graphics or drawing layouts in vb.net.
I have played around with the tableLayoutPanel but I'm lost on how to get it setup for what I need.
For an example scenario: sheet 96 x 48 (the overall grid size)
I need columns for say 4 cuts of 22" (column width) and the 5th column for the left over of width of 8".
For rows say 4 parts 10" wide (row height) with a 5th row that is equal to the left over of 8".

Thanks, Roger
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here's an example for the TableLayoutPanel approach.  The form has just a Button and a regular Panel (that will hold the created TableLayoutPanel):
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ' sheet 96 x 48 (the overall grid size)
        ' I need columns for say 4 cuts of 22" (column width) and the 5th column for the left over of width of 8".
        ' For rows say 4 parts 10" wide (row height) with a 5th row that is equal to the left over of 8"

        Dim columns() As Integer = {22, 22, 22, 22, 8}
        Dim rows() As Integer = {10, 10, 10, 10, 8}
        Dim width As Integer = 0
        For Each columnWidth As Integer In columns
            width = width + columnWidth
        Next
        Dim height As Integer = 0
        For Each rowHeight As Integer In rows
            height = height + rowHeight
        Next

        Dim TLP As New TableLayoutPanel

        TLP.ColumnStyles.Clear()
        TLP.ColumnCount = columns.Length
        For c As Integer = 0 To columns.Length - 1
            TLP.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, columns(c) / width * 100))
        Next

        TLP.RowStyles.Clear()
        TLP.RowCount = rows.Length
        For r As Integer = 0 To rows.Length - 1
            TLP.RowStyles.Add(New RowStyle(SizeType.Percent, rows(r) / height * 100))
        Next

        TLP.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single
        TLP.Dock = DockStyle.Fill
        Panel1.Controls.Add(TLP)
    End Sub

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here's an example of the Bitmap and PictureBox approach.  The form has just a Button and a PictureBox on it:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ' sheet 96 x 48 (the overall grid size)
        ' I need columns for say 4 cuts of 22" (column width) and the 5th column for the left over of width of 8".
        ' For rows say 4 parts 10" wide (row height) with a 5th row that is equal to the left over of 8"

        Dim columns() As Integer = {22, 22, 22, 22, 8}
        Dim rows() As Integer = {10, 10, 10, 10, 8}
        Dim width As Integer = 0
        For Each columnWidth As Integer In columns
            width = width + columnWidth
        Next
        Dim height As Integer = 0
        For Each rowHeight As Integer In rows
            height = height + rowHeight
        Next

        Dim bmp As New Bitmap(width, height)
        Using G As Graphics = Graphics.FromImage(bmp)
            G.Clear(Color.White)
            G.DrawRectangle(Pens.Black, 0, 0, width - 1, height - 1)
            Dim runningSum As Integer = 0
            For c As Integer = 0 To columns.Length - 2 ' skip the last one
                runningSum = runningSum + columns(c)
                G.DrawLine(Pens.Black, runningSum, 0, runningSum, height)
            Next

            runningSum = 0
            For r As Integer = 0 To rows.Length - 2 ' skip the last one
                runningSum = runningSum + rows(r)
                G.DrawLine(Pens.Black, 0, runningSum, width, runningSum)
            Next
        End Using

        PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
        PictureBox1.Image = bmp
    End Sub

Open in new window


*Since the bitmap is so small, it might not scale very well when large.  If you don't like the look of it then use larger values (just multiply everything by 100 for instance).
0
 
rogerk_fswwAuthor Commented:
Idle_Mind, You rock!!

That works right out of the box.
I can easily adapt it to different sizes and layouts.

MANY THANKS,

RogerK
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You're welcome.

If you add this bit code to the bottom of that routine, then you'll have labels for each grid:
        For c As Integer = 0 To columns.Length - 1
            For r As Integer = 0 To rows.Length - 1
                Dim lbl As New Label
                lbl.AutoSize = False
                lbl.Dock = DockStyle.Fill
                lbl.TextAlign = ContentAlignment.MiddleCenter
                lbl.Text = columns(c) & " x " & rows(r)
                TLP.Controls.Add(lbl, c, r)
            Next
        Next

Open in new window


Cuts w/ Annotated Sizes
0
 
rogerk_fswwAuthor Commented:
Man. That is awesome!
I was planning on adding that, but didn't know yet how I was going to do it.

I am forever grateful to you for this.
Thanks again.
RogerK
0
 
rogerk_fswwAuthor Commented:
Hello I_M if you're still hanging around...

I ran into a little snag trying to Highlight the columns or rows where there is a wasted part on the width or the length. i.e. the last column or last row.
It represents the amount of waste each direction.
It looks like this is not as easy as I thought. In searching I found a lot of stuff about using the paint event, but I'm not sure how to do it.

Any ideas on this?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Define "highlight".  It could be as simple as setting the BackColor of the Labels for last Column/Row:

        For c As Integer = 0 To columns.Length - 1
            For r As Integer = 0 To rows.Length - 1
                Dim lbl As New Label
                lbl.AutoSize = False
                lbl.Dock = DockStyle.Fill
                lbl.TextAlign = ContentAlignment.MiddleCenter
                lbl.Text = columns(c) & " x " & rows(r)
               If c = columns.Length - 1 OrElse r = rows.Length - 1 Then
                    lbl.BackColor = Color.Yellow
                End If

                TLP.Controls.Add(lbl, c, r)
            Next
        Next

The waste is "highlighted" by setting the Label BackColor to Yellow.
0
 
rogerk_fswwAuthor Commented:
Yep that will work. I have been experimenting with this.
That's exactly what I meant, by the way.
What's the property to set the "padding" or border size (not sure what it's called) so the labels fill the whole space without the edge spacing between cells?

P.S. If you're ever in St Louis I'll buy you a beer (or 2) or whatever is your pleasure.

I think I will stop bugging you, at least for now.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Set the Margin() to 0 (zero):

    Dim lbl As New Label
   lbl.Margin = New Padding(0)
    ...

I don't mind the additional questions at all.  They are still related to the original problem, and the approach that I suggested you use.
0
 
rogerk_fswwAuthor Commented:
Will that still show the cell divider line? it would be nice if I could hide these if I needed to.
Because what happens sometimes is if the offal (waste) especially on the length end is wide enough we will cut another piece or more across the width of the end waste. But then the dimension (row height) just for the end piece would be the Length of the part dimension. I.E. part is 5" wide and 20" long so two 20" X 5" parts can be cut from 48" wide leftover on the length.
Sorry, Just dreaming..
I am getting picky, and I can live without that. Besides, I need to do the rest of this myself, you've already gone way beyond the original request. Please don't fret about this - Really.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The cell borders were turned on with this line in the original code:

      TLP.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single

Good luck!  =)
0
 
rogerk_fswwAuthor Commented:
Thanks. I have found out how to set / change these so they look better.

See Ya! You've been very patient, thanks.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now