12,897,124 members (55,216 online)
Rate this:
See more:
Hi All

I was wondering if someone could show me or point me in the right direction to be able to draw a vector rectangle with Lenght x Width x Height from text box value on a form or picturebox.

Cheers
Posted 9-Apr-11 0:18am
SIFNOk821

Rate this:

## Solution 1

You can use GDI+ for doing this. There is also this[^] nice article on CP which might help you.

Update: Looks like we all missed the point. Assuming you need to draw a cube or a cuboid, length, breadth and height are not enough. This will never tell you the intended orientation of the object in the space. If that is all the information you have, you will end up drawing your perception of object in space. I hope I am clear. To get the real position in the space of the object, you will need at least some co-ordinates if not all. To me it is not enough input to draw the object. You can very well draw the isometric view and then let the user rotate the object. This, surely, will need you to handle the axis about which the rotation is done and also provide a smooth flicker-free movement. I am sure there are libraries available that ca help you do this. So you might be better off using a readily available component rather tht doing yourself. Although it's your call.

Another thing that I noticed is use of "vector". Are you referring that those three input numbers will be the vector values in i, j and k axis and then you need to draw a projection of that? This is totally different from drawing a rectangle. This will involve you drawing upto 4 triangles who share sides with each other. This again will be easier to do using the already available libraries than doing yourself. Your call again.
v2
SAKryukov 10-Apr-11 17:18pm

--SA
Espen Harlinn 10-Apr-11 17:55pm

It might be overkill, but it's definitely a good reply - my 5
SAKryukov 10-Apr-11 19:07pm

Well, come to think about, I agree, a 5.
--SA
SAKryukov 10-Apr-11 23:50pm

--SA
SAKryukov 11-Apr-11 1:52am

@d@nish: I don't think we missed anything essential. All you say is correct, but the whole thing looks like a learning exercise. The whole idea of having a edit box for 3 components of the size tells it's nothing serious.

Should it be a really robust things with 3D navigation, it would be more in the class of advanced industrial design. I did such things, clearly understand the topic. This is where one could put forward real inventions with a very deep motivation of every detail (I mean UI and user experience, even the model of it.) It would be some special 3D control, something like in flight simulators, or much more complex or simple.

Let's just forget about it. What can be required in 3D box with arbitrary sizes, in fixed default orientation, automatically scaled to have the image filling the drawing area -- at best. That would be enough. End of story. If it's well beyond such a simple model -- it would go well beyond the Code Project format -- end of story again. Let's see what OP says...

--SA
Rate this:

## Solution 2

Draw rectangle in some Control (even a form) in overridden `System.Windows.Forms.Control.OnPaint`, or a handler of the event `Paint`. Do yourself a favor, do not use any image control, draw directly on a control. Use the instance of `System.Drawing.Graphics` provided for you in event arguments (in both cases). Do yourself another favor — do not create the instance of Graphics for the control: it's already provided for you (creating this instance is a very common mistake).

The parameters of the rectangle depends on some data, such as position and size. Make those parameters the fields of your control of form. Update their values based one the text from the text box: parse its `Text` into integer (or float) values. Use `string.Split` and `integer.Parse` or float.Parse for this purpose. As soon as the data fields are updated, use `Control.Invalidate` — this method will trigger re-rendering of the control where you render you rectangle (and whatever else).

That's all you may possibly need.

[EDIT]
After d@nish noted that implied image might be a box, not rectangle: sure, but in this case my Answer is valid too: use `Graphics.DrawLine` several times (instead of `DrawRectangle`) to compose the image you need.

—SA
v5
Espen Harlinn 10-Apr-11 17:56pm

Nice and simple, my 5
SAKryukov 10-Apr-11 19:07pm

Thank you, Espen.
--SA
d@nish 10-Apr-11 23:32pm

OP probably needs a cube and not a rectangle. He has mentioned volumne in the post. I can think of two things, either his English is wrong or his maths is wrong. I went with former.
SAKryukov 10-Apr-11 23:51pm

Thank you for this note. I'll add a reference to this note in my Answer -- it is valid in this case as well.
--SA
d@nish 11-Apr-11 0:12am

Of course it is. All one needs to do it use coordinate geometry wisely to create 3-D objects. But it surely is not going to be easy. He will have to find 8 co-ordinates based on LBH. If one uses Graphics class methods, drawing is very easy but the maths involved may put OP in trouble. Although it will be fun to write a library doing this.
SAKryukov 11-Apr-11 0:19am

Not easy -- for just the 3D box? Well, it depends on the skills and understanding of geometry, but what's not about programming here? More degrees of freedom: +2 rotational. Cabinet or isometric projection is trivial, perspective projection need consideration of "camera" position. Some calculations to be done accurately, but certainly not programming issues...

Thank you.
--SA
d@nish 11-Apr-11 0:23am

There are a lot of words in there which reminded me of Engineering Drawing I did in University. :)
d@nish 11-Apr-11 1:40am

we missed something from the question. See my updated response if it makes any sense.
Rate this:

## Solution 3

You might also like:
.NET Shape Control[^]

Regards
Espen Harlinn
SAKryukov 10-Apr-11 19:06pm

Another overkill :-) but a nice one -- a 5.
--SA
Espen Harlinn 12-Apr-11 5:11am

Thanks SAKryukov!

Top Experts
Last 24hrsThis month
 OriginalGriff 330 CHill60 125 ppolymorphe 70 Maciej Los 70 Vinod Jangle 50
 OriginalGriff 5,482 CHill60 3,673 Jochen Arndt 2,958 Karthik Bangalore 2,456 ppolymorphe 2,435