none
Use c# to draw RRS feed

  • Question

  • I want to use c# to design a program similar to a drawing program using LOGO language. The user enters the code in a textbox, and the graphics can be generated simultaneously in a window. When designing the program, I don't know how to translate the user input language to c#, and implement real-time drawing of the program.



    • Edited by Avery Ye Wednesday, October 24, 2018 8:04 AM
    Wednesday, October 24, 2018 2:21 AM

Answers

  • Hi Avery Ye,

    I think that such feature will be simple to design and develop using WPF Canvas control, and some parsing mechanism to parse your code, here is an example of using Canvas in WPF (source: https://www.dotnetperls.com/canvas-wpf):

    Note: the code here is a starting point, you should combine techniques in those examples to attain your goal.

    <Window x:Class="WpfApplication27.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525"
            Loaded="Window_Loaded">
        <Grid>
            <Canvas HorizontalAlignment="Left" Height="319" Margin="0"
                    VerticalAlignment="Top" Width="517"
                    Name="Can1"/>
        </Grid>
    </Window>
    

    C# code behind:

    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Media;
    using System.Windows.Shapes;
    
    namespace WpfApplication27
    {
        class Rect
        {
            public int Width { get; set; }
            public int Height { get; set; }
            public int Left { get; set; }
            public int Top { get; set; }
            public SolidColorBrush Color { get; set; }
        }
    
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                // ... Create list of our Rect objects.
                List<Rect> rects = new List<Rect>();
    
                // ... Add Rect objects.
                rects.Add(new Rect()
                {
                    Width = 100,
                    Height = 100,
                    Left = 0,
                    Top = 0,
                    Color = Brushes.Aquamarine
                });
                rects.Add(new Rect()
                {
                    Width = 50,
                    Height = 50,
                    Left = 100,
                    Top = 100,
                    Color = Brushes.Cornsilk
                });
                rects.Add(new Rect()
                {
                    Width = 25,
                    Height = 25,
                    Left = 150,
                    Top = 150,
                    Color = Brushes.Peru
                });
    
                foreach (Rect rect in rects)
                {
                    // ... Create Rectangle object.
                    Rectangle r = new Rectangle();
                    r.Width = rect.Width;
                    r.Height = rect.Height;
                    r.Fill = rect.Color;
    
                    // ... Set canvas position based on Rect object.
                    Canvas.SetLeft(r, rect.Left);
                    Canvas.SetTop(r, rect.Top);
    
                    // ... Add to canvas.
                    Can1.Children.Add(r);
                }
            }
        }
    }
    

    for the parsing mechanism, you can use XML parser, here is a simple example:

    // https://www.dotnetperls.com/xmlreader

    using System; using System.Xml; class Program { static void Main() { // Create an XML reader for this file. using (XmlReader reader = XmlReader.Create("perls.xml")) { while (reader.Read()) { // Only detect start elements. if (reader.IsStartElement()) { // Get element name and switch on it. switch (reader.Name) { case "perls": // Detect this element. Console.WriteLine("Start <perls> element."); break; case "article": // Detect this article element. Console.WriteLine("Start <article> element."); // Search for the attribute name on this current node. string attribute = reader["name"]; if (attribute != null) { Console.WriteLine(" Has attribute name: " + attribute); } // Next read will contain text. if (reader.Read()) { Console.WriteLine(" Text node: " + reader.Value.Trim()); } break; } } } } } }


    <?xml version="1.0" encoding="utf-8" ?>
    <perls>
        <article name="backgroundworker">
            Example text.
        </article>
        <article name="threadpool">
            More text.
        </article>
        <article></article>
        <article>Final text.</article>
    </perls>
    
    

    and this is another example using XDocument to parse a string how to parse a string.

    Good Coding;

    Wednesday, October 24, 2018 3:03 PM
  • You didn't actually answer any of Wayne's question.  You just repeated your original question.

    What do you expect the user to type?  Do you want them to enter LOGO commands, or your own language, or do you actually expect them to enter C# code?  If you want them to enter LOGO or your own custom language, then you will not be translating the user input into C#.  You will be interpreting their inputs, and storing the results in your code.

    The best way to design something like this is to come up with a sample input, and figure out what needs to happen.  Consider this:

        color black
        line 0,0 to 100,100

    As a human, how do you handle that?  You'd look at the first word, and use a "switch" statement to know how to process the rest of the line.  You'll probably create an internal "list of commands" which you will have to reinterpret every time you need to refresh your window.

    During the real-time refresh, you'd scan this list of commands and take some actions.  The result of the first command would be to set a "current foreground color" variable somewhere.  The result of the second command would be to call Graphics.Line using that color.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by Avery Ye Sunday, October 28, 2018 3:16 AM
    Thursday, October 25, 2018 9:25 PM

All replies

  • How can I design a program,which draws a graph  with user input in a textbox.


    What's your level of knowledge and experience with C#? With .NET?
    With programming in general?

    What have you tried so far? What have you researched?

    Have you looked at any of the tutorials on the Web re creating graphs 
    in C#?  There are many so do a web search.

    Also see:

    Microsoft.VisualStudio.GraphModel Namespace
    https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.graphmodel?view=visualstudiosdk-2017

    Graph Class 
    https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.graphmodel.graph?redirectedfrom=MSDN&view=visualstudiosdk-2017

    - Wayne

    Wednesday, October 24, 2018 3:58 AM
  • Thank you for your answer. I want to use c# to design a program similar to a drawing program using LOGO language. The user enters the code in a textbox, and the graphics can be generated simultaneously in a window. When designing the program, I don't know how to translate the user input language to c#, and implement real-time drawing of the program.
    Wednesday, October 24, 2018 8:03 AM
  • Hi Avery Ye,

    I think that such feature will be simple to design and develop using WPF Canvas control, and some parsing mechanism to parse your code, here is an example of using Canvas in WPF (source: https://www.dotnetperls.com/canvas-wpf):

    Note: the code here is a starting point, you should combine techniques in those examples to attain your goal.

    <Window x:Class="WpfApplication27.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525"
            Loaded="Window_Loaded">
        <Grid>
            <Canvas HorizontalAlignment="Left" Height="319" Margin="0"
                    VerticalAlignment="Top" Width="517"
                    Name="Can1"/>
        </Grid>
    </Window>
    

    C# code behind:

    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Media;
    using System.Windows.Shapes;
    
    namespace WpfApplication27
    {
        class Rect
        {
            public int Width { get; set; }
            public int Height { get; set; }
            public int Left { get; set; }
            public int Top { get; set; }
            public SolidColorBrush Color { get; set; }
        }
    
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                // ... Create list of our Rect objects.
                List<Rect> rects = new List<Rect>();
    
                // ... Add Rect objects.
                rects.Add(new Rect()
                {
                    Width = 100,
                    Height = 100,
                    Left = 0,
                    Top = 0,
                    Color = Brushes.Aquamarine
                });
                rects.Add(new Rect()
                {
                    Width = 50,
                    Height = 50,
                    Left = 100,
                    Top = 100,
                    Color = Brushes.Cornsilk
                });
                rects.Add(new Rect()
                {
                    Width = 25,
                    Height = 25,
                    Left = 150,
                    Top = 150,
                    Color = Brushes.Peru
                });
    
                foreach (Rect rect in rects)
                {
                    // ... Create Rectangle object.
                    Rectangle r = new Rectangle();
                    r.Width = rect.Width;
                    r.Height = rect.Height;
                    r.Fill = rect.Color;
    
                    // ... Set canvas position based on Rect object.
                    Canvas.SetLeft(r, rect.Left);
                    Canvas.SetTop(r, rect.Top);
    
                    // ... Add to canvas.
                    Can1.Children.Add(r);
                }
            }
        }
    }
    

    for the parsing mechanism, you can use XML parser, here is a simple example:

    // https://www.dotnetperls.com/xmlreader

    using System; using System.Xml; class Program { static void Main() { // Create an XML reader for this file. using (XmlReader reader = XmlReader.Create("perls.xml")) { while (reader.Read()) { // Only detect start elements. if (reader.IsStartElement()) { // Get element name and switch on it. switch (reader.Name) { case "perls": // Detect this element. Console.WriteLine("Start <perls> element."); break; case "article": // Detect this article element. Console.WriteLine("Start <article> element."); // Search for the attribute name on this current node. string attribute = reader["name"]; if (attribute != null) { Console.WriteLine(" Has attribute name: " + attribute); } // Next read will contain text. if (reader.Read()) { Console.WriteLine(" Text node: " + reader.Value.Trim()); } break; } } } } } }


    <?xml version="1.0" encoding="utf-8" ?>
    <perls>
        <article name="backgroundworker">
            Example text.
        </article>
        <article name="threadpool">
            More text.
        </article>
        <article></article>
        <article>Final text.</article>
    </perls>
    
    

    and this is another example using XDocument to parse a string how to parse a string.

    Good Coding;

    Wednesday, October 24, 2018 3:03 PM
  • You didn't actually answer any of Wayne's question.  You just repeated your original question.

    What do you expect the user to type?  Do you want them to enter LOGO commands, or your own language, or do you actually expect them to enter C# code?  If you want them to enter LOGO or your own custom language, then you will not be translating the user input into C#.  You will be interpreting their inputs, and storing the results in your code.

    The best way to design something like this is to come up with a sample input, and figure out what needs to happen.  Consider this:

        color black
        line 0,0 to 100,100

    As a human, how do you handle that?  You'd look at the first word, and use a "switch" statement to know how to process the rest of the line.  You'll probably create an internal "list of commands" which you will have to reinterpret every time you need to refresh your window.

    During the real-time refresh, you'd scan this list of commands and take some actions.  The result of the first command would be to set a "current foreground color" variable somewhere.  The result of the second command would be to call Graphics.Line using that color.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by Avery Ye Sunday, October 28, 2018 3:16 AM
    Thursday, October 25, 2018 9:25 PM
  • Thank you for your answer.
    Friday, October 26, 2018 3:25 AM
  • Thank you for your answer.
    Friday, October 26, 2018 3:25 AM
  • Hi Avery Ye,

    If the reply is helpful to you, please mark it as answer, it will be beneficial to other communities who have the similar issue.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Avery Ye Sunday, October 28, 2018 3:15 AM
    • Unmarked as answer by Avery Ye Sunday, October 28, 2018 3:16 AM
    Friday, October 26, 2018 7:39 AM
    Moderator
  • Welcome, Avery ye.

    Friday, October 26, 2018 9:14 AM