Click here to Skip to main content
15,883,841 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello Experts

I am working on WPF windows application where I am trying to create Rectangle through vb.net

I am able to do it for just one row.
I would need to repeat this on Each row until there is no place in the Canvas.

Here is my XAML
XML
<Grid>
            <ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="visible">
                <Canvas Height="700" Width="1200" Name="front_canvas" Grid.Row="3" Grid.Column="2" >
                    <Canvas.RenderTransform>
                        <ScaleTransform ScaleX="1" ScaleY="1" />
                        
                    </Canvas.RenderTransform>

</Canvas>
</scrollviewer>
</Grid>


Here is my Vb.net
C#
Private _top As Double = 0
Protected Sub DrawRect()
     
            'init rectangles
            'front_canvas.Width - 1 Step 1100
                               For i As Integer = 1 To front_canvas.Width - 1 Step 100
                        Dim rect As New Rectangle()
                        rect.StrokeThickness = 1
                        rect.Stroke = System.Windows.Media.Brushes.Black
                        rect.Width = 50
                        rect.Height = 50
                        rect.Name = "box" + i.ToString()
                        If Not i / 2 = 0 Then
                            Canvas.SetLeft(rect, i)
                     Canvas.SetTop(rect, Top)
                    _top += rect.Height
                        End If

                        _rectangles.Add(rect)
                    Next
                    For Each rect In _rectangles
                        front_canvas.Children.Add(rect)
                    Next
              End Sub 


I am looking for something like below
C#
o o o o 
o o o o 
o o o o 

or 

oo oo oo oo
oo oo oo oo 
oo oo oo oo





All the o's in the above are Rectangles in my case.

With the current code my application shows like below

C#
o o o o o o o o 



Would appreciate some help in this
Posted
Updated 29-Jan-16 5:48am
v4
Comments
Sergey Alexandrovich Kryukov 29-Jan-16 10:45am    
First problem (unrelated to your problem) that catches my eye is: you should not give a name to a rectangle. Why?
Now, I think you simply need to write code accurately, to solve such "problems".
—SA

1 solution

The problem seems to be as obvious as your formulation. You mention "just one row" and immediately one would try to see where you shift the rectangle for a row. And it becomes obvious, because you use Canvas.SetLeft, but never Canvas.SetTop. Apparently, for every row you should use different top coordinate. Add the call to Canvas.SetTop:
Canvas.SetTop Method (System.Windows.Controls)[^].

That's all.

Now, please see my comment to the question. In addition to that: many your minor bugs such as this one will go if you simply start to write neat code. Look, right now your code is based on immediate constants like 1110, 10, 5, 100 and 50. It's apparent that they are interrelated. You should not hard-code, say, shifts, because they are calculated out of number of rectangles and the size. You should calculate everything. Most of your immediate constants should become variables with calculated values, other should become either explicitly declared constants, never repeated, or method parameters. Only then you will be able to maintain your code and will have less bugs.

—SA
 
Share this answer
 
v2
Comments
sudevsu 29-Jan-16 11:20am    
Did not quite understand
Sergey Alexandrovich Kryukov 29-Jan-16 11:30am    
What part you did not understand?
Where is your Canvas.SetTop?
—SA
sudevsu 29-Jan-16 11:40am    
I tried to set Canvas top didn't work. Please see updated question's Code
Sergey Alexandrovich Kryukov 29-Jan-16 12:25pm    
Just do it properly, for each rectangle, and the Y value changes for each row. What could be unclear?
And look, programming is not done by "trying". You should not "try" but write exactly what you want to achieve, based on full understanding of what you are doing. If you are "trying", we cannot be help responsible for that. I gave you precise solution and expect you accept it formally.

You have 115 questions; and I believe a lot more have been removed. You should have learned something already. If you cannot use even the clearest answers to do your job, nothing can help you.

—SA
Kenneth Haugland 29-Jan-16 12:37pm    
Haha, I messed up X and Y in may comment (it's correct now). Gave you a 5 anyway :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900