Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I am having a hard time (failing badly) trying to work out how to draw objects on a canvas (C#/VB winforms or wpf) in a way that I can save to a picture file or a database for later use. (like a mini drawing app)

Mainly interested in richtext editable and moveable, but also eventually some other shapes lines images etc. but one step at a time!

can anyone help with a easy to follow method or sample app to solve the above ?
I have downloaded several demo apps, but none cover richtext, and have been so far difficult to follow!


I'm not trying to recreate coreldraw or autocad.. but want to experiment with a basic designer app to learn positioning richtext and shapes.

many thanks for any help and explanations :-)

Andy T
Posted
Updated 29-Jul-14 12:54pm
v2
Comments
BacchusBeale 29-Jul-14 19:01pm    
do you have sample code or picture to explain what you are trying to do?
Member 10913969 29-Jul-14 19:05pm    
Hi BaccusBeale,
I don't have any working sample code or pictures as I am having so much trouble understanding the concept!
essentially if you look at coreldraw or autocad or MS publisher.. these are like the type of interface I am trying to learn, but just can't work out the code to place, move and edit richtext objects..

Thanks for your comments

You don't need any sample code. You can place anything on Canvas. Just try it, instead of asking of such samples. Think about it. Suppose you have a sample code where a rectangle is put on Canvas. Would you need a separate code sample to put something else on the same Canvas. If, by some reason, you would, you will never come to writing any sensible code by yourself. Hope this is not the case.

In your question, the alarming words are "multiple times". Do you understand what is the type and what is its instance. You can put several instances of, say RichTexBox in some container element, but only if they are different instances. This is a special feature of WPF different from many other UI libraries: it is checked up that each instance have only one parent. For in-depth understanding of WPF parent-child relationships and related concepts, please read: http://msdn.microsoft.com/en-us/library/ms753391%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
Comments
Member 10913969 31-Jul-14 18:11pm    
Thanks Sergey,
I didn't see how that link covered my problem, but still quite interesting - thanks!.
I guess I am still not explaining my request well enough. I am a noobie, and have been playing with code samples for various elements.
My aim is to be able to have a screen area where i can place and edit shapes and text objects.
I need to be able to save these out as a graphic or store the arrangement in a database or text file.

My problem is I don't know how to construct an object stack to hold richtext objects/elements and other drawing shape objects/elements.
I also do not know how to allow someone to draw these objects on screen/area and place them wherever they wish, and then re-edit them as necessary.
It would be nice to see some example code with comments to explain the process, or any existing tutorials that come close to my problems.

Andy
Sergey Alexandrovich Kryukov 1-Aug-14 0:01am    
Just do the following: create an WPF project from scratch, with one window. Add an instance of Canvas to this window. To the canvas, add some RichTextBox instance. Run it. Observe what happens. It will take some 5 minutes...
—SA
Sergey Alexandrovich Kryukov 1-Aug-14 0:01am    
Just do the following: create an WPF project from scratch, with one window. Add an instance of Canvas to this window. To the canvas, add some RichTextBox instance. Run it. Observe what happens. It will take some 5 minutes...
—SA
Member 10913969 1-Aug-14 16:48pm    
I think I'm going to can this question as it really has'nt come across properly!
of course I can drop a richtext on a canvas in design mode and edit it !!

I need to do this programatically like a drawing application (as mentioned originally).
- start the app with a toolbox and blank canvas.
- user chooses to draw a box - and places a box on the canvas.
- user chooses to draw a richtext box - and places it on the canvas.
- user can edit in place any amount of placed items..

Many Thanks
Andy T
Sergey Alexandrovich Kryukov 1-Aug-14 20:58pm    
Okay, I see what do you mean. What prevents you from doing the same in code?
—SA
Member 10913969 wrote:
I think I'm going to can this question as it really hasn't come across properly! of course I can drop a richtext on a canvas in design mode and edit it!
Only after this comment I got an idea what your real problem could be. Probably, this is some confusion on how to write code equivalent to some XAML. And, instead of digging into the detail specific to Canvas or RichTextBox (or whatever else you are asking about), I want to give you the most universal advise which would work in nearly all cases.

First thing to understand is: when you use XAML at design time to create a window or a user control, it is fully consumed during design time and later during build time. When you build your project with such XAML, the XAML code is fully translated into C# code. When the build is complete, this auto-generated code stays under the sub-directory of your project, "obj", you can easily find it.

So, here is my universal advice: if you can do something in XAML or with the designer and not sure how to write similar thing in C# (which is really can be a good way of development, in very many cases), you can do simple thing: in some sample project, do whatever you wanted to do in XAML, build this project, find out auto-generated files and see what this code does. Simple, isn't it? Such exercises are really useful for understanding certain things.

—SA
 
Share this answer
 
v2

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