如何使用文本框编译代码?

时间:2017-05-19 11:38:49

标签: c# visual-studio input textbox

我正在创建一个游戏,你需要编码才能在视觉工作室社区2015中围绕屏幕移动一个块。这个项目的目标是创建一个游戏来教人们如何编码。我已经创建了一个移动块的方法(到目前为止)。

<?xml version="1.0" encoding="utf-8"?>
<modification>
    <name>Remove wishlist buttons</name>
    <code>cjohanssonremovewishlistbutons</code>
    <version>1.0</version>
    <author>Christian Johansson</author>
    <link></link>
    <file path="catalog/view/theme/default/template/product/product.tpl">
        <operation>
        <search trim="true">
            <![CDATA[<button type="button" data-toggle="tooltip" class="btn btn-default" title="<?php echo $button_wishlist; ?>" onclick="wishlist.add('<?php echo $product_id; ?>');"><i style="color:<?php echo html_entity_decode($config->get('ekometcss_css_wishlist_color'), ENT_QUOTES, 'UTF-8'); ?>;" <i class="<?php global $config; echo $config->get('ekometcss_custom_icons_wishlist') ?  'fa fa-' . $config->get('ekometcss_custom_icons_wishlist') : 'fa fa-star'; ?>"></i></button>]]>
        </search>
        <add position="replace">
            <![CDATA[]]>
        </add>
        </operation>
    </file>
    <file path="catalog/view/theme/default/template/module/*.tpl">
        <operation>
        <search trim="true">
            <![CDATA[<button style="width: 80%;" type="button" class="cat-cart"]]>
        </search>
        <add position="replace">
            <![CDATA[<button style="width: 100%;" type="button" class="cat-cart"]]>
        </add>
        </operation>
        <operation>
        <search trim="true">
            <![CDATA[<button style="width: 20%;" type="button" class="cat-wishlist" data-toggle="tooltip" title="<?php echo $button_wishlist; ?>" onclick="wishlist.add('<?php echo $product['product_id']; ?>');"><i style="color:<?php echo html_entity_decode($config->get('ekometcss_css_wishlist_color'), ENT_QUOTES, 'UTF-8'); ?>;" <i class="<?php global $config; echo $config->get('ekometcss_custom_icons_wishlist') ?  'fa fa-' . $config->get('ekometcss_custom_icons_wishlist') : 'fa fa-star'; ?>"></i></button>]]>
        </search>
        <add position="replace">
            <![CDATA[]]>
        </add>
        </operation>
    </file>
    <file path="catalog/view/theme/default/template/product/category.tpl">
        <operation>
        <search trim="true">
            <![CDATA[<button style="width: 80%;" type="button" class="cat-cart"]]>
        </search>
        <add position="replace">
            <![CDATA[<button style="width: 100%;" type="button" class="cat-cart"]]>
        </add>
        </operation>
        <operation>
        <search trim="true">
            <![CDATA[<button style="width: 20%;" type="button" class="cat-wishlist" data-toggle="tooltip" title="<?php echo $button_wishlist; ?>" onclick="wishlist.add('<?php echo $product['product_id']; ?>');"><i style="color:<?php echo html_entity_decode($config->get('ekometcss_css_wishlist_color'), ENT_QUOTES, 'UTF-8'); ?>;" <i class="<?php global $config; echo $config->get('ekometcss_custom_icons_wishlist') ?  'fa fa-' . $config->get('ekometcss_custom_icons_wishlist') : 'fa fa-star'; ?>"></i></button>]]>
        </search>
        <add position="replace">
            <![CDATA[]]>
        </add>
        </operation>
    </file>
</modification>

我有一个文本框(textBox1)和一个按钮(btnGo),我想设置它,这样当用户键入textBox1时,单击执行代码的go按钮。

例如,用户输入&#34;移动(向上);&#34;在textBox1中并单击btnGo,该块将向上移动1个空格(块为30x30像素,因此30像素= 1个空格)

3 个答案:

答案 0 :(得分:0)

如果你在移动功能中检查参数值,为什么不检查命令本身呢?

取出文本框中的值并用&#34;(&#34;符号分割,然后取新列表的第一部分,在这种情况下&#34;移动&#34;并执行逻辑根据这个价值:

 switch (textBoxValue.split("(")[0])
  {
     case "Move":
          move();
          break;

等等。

答案 1 :(得分:0)

(此帖子已更改,以便要编译的文本显示在可编辑的文本框中)所有必须完成的操作是创建一个新的Windows表单项目,并使用此处的复制和粘贴覆盖项目文件文章到名称所示的项目文件中

首先要做的事情: 您需要做的第一件事是加载Roslyn编译器包。

在Visual Studio中,使用Tools / NuGet包管理器/包管理器控制台

在PM&gt;提示,输入Install-Package Microsoft.CodeAnalysis

创建项目时,请在project / properties / application / Target framework

中定位.NET Framework 4.6.1

此代码已使用Visual Studio 2015和.NET Framework 4.6.1

进行了测试

Program.cs的

using System;
using System.Windows.Forms;

namespace ExecuteText {
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    } 
 }

Form1.cs的

using System;
using System.Drawing;
using System.Windows.Forms;

using System.Linq;
using System.IO;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using System.Reflection;

namespace ExecuteText
{

    /**************************************************************************************************
    * A form 1.
    *
    * @sa System.Windows.Forms.Form
    **************************************************************************************************/

    public partial class Form1 : Form
    {

        /**************************************************************************************************
        * Default constructor.
        **************************************************************************************************/

        public Form1()
        {
            InitializeComponent();
        }

        /**************************************************************************************************
        * Gets the assembly files in this collection.
        *
        * @param assembly The assembly.
        *
        * @return An enumerator that allows foreach to be used to process the assembly files in this
        * collection.
        **************************************************************************************************/

        IEnumerable<string> GetAssemblyFiles(Assembly assembly)
        {
            var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (var assemblyName in assembly.GetReferencedAssemblies())
                yield return loadedAssemblies.SingleOrDefault(a => a.FullName == assemblyName.FullName)?.Location;
        }

        /**************************************************************************************************
        * Creates compiler context.
        *
        * @return The new compiler context.
        **************************************************************************************************/

        private CSharpCompilation CreateCompilerContext()
        {
            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(TextBox.Text);
            var assemblyName = Path.GetRandomFileName();
            Assembly a = typeof(Form1).Assembly;
            var refs = GetMetadataReferencesInAssembly(a);

            var compilation = CSharpCompilation.Create(
                                                       assemblyName,
                                                       new[] {syntaxTree},
                                                       refs.ToArray(),
                                                       new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
            return compilation;
        }

        /**************************************************************************************************
        * Console write errors.
        *
        * @param result The result.
        **************************************************************************************************/

        private void ConsoleWriteErrors(EmitResult result)
        {
            var failures = result.Diagnostics.Where(CodeHasError);
            var err = "";
            foreach (var diagnostic in failures)
               err += $"{diagnostic.Id}: {diagnostic.GetMessage()}";

            ErrorTextBox.AppendText(err);
        }

        /**************************************************************************************************
        * Executes the compiled code operation.
        *
        * @param ms The milliseconds.
        **************************************************************************************************/

        private  void ExecuteCompiledCode(MemoryStream ms)
        {
            ms.Seek(0, SeekOrigin.Begin);
            var assembly = Assembly.Load(ms.ToArray());

            var type = assembly.GetType("AStringOfCode.FunctionClass");
            var obj = Activator.CreateInstance(type);
            type.InvokeMember("Run",
                              BindingFlags.Default | BindingFlags.InvokeMethod,
                              null,
                              obj,
                              new object[] {Block});
            ShowPosition();
        }

        /**************************************************************************************************
        * Gets metadata references in assembly.
        *
        * @param assembly The assembly.
        *
        * @return The metadata references in assembly.
        **************************************************************************************************/

        private List<MetadataReference> GetMetadataReferencesInAssembly(Assembly assembly)
        {


            var assemblyFiles = GetAssemblyFiles(assembly);

            Console.WriteLine(assemblyFiles);

            List<MetadataReference> refs = new List<MetadataReference>();
            foreach (var assemblyname in assemblyFiles)
                refs.Add(MetadataReference.CreateFromFile(assemblyname));

            return refs;
        }



        /**************************************************************************************************
        * Code has error.
        *
        * @param diagnostic The diagnostic.
        *
        * @return True if it succeeds, false if it fails.
        **************************************************************************************************/

        private bool CodeHasError(Diagnostic diagnostic)
        {
            return diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error;
        }

        private void ShowPosition()
        {
            PositionTextBox.Text = Block.Location.X + "," + Block.Location.Y;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           ShowPosition(); 
        }


        private void Run_Click(object sender, EventArgs e)
        {

            var compilation = CreateCompilerContext();
            using (var ms = new MemoryStream())
            {
                var result = compilation.Emit(ms);

                if (result.Success)
                {
                    ExecuteCompiledCode(ms);
                }
                else
                {
                    ConsoleWriteErrors(result);
                }
            }
        }

        private void TextBox_TextChanged(object sender, EventArgs e)
        {

        }
    }

}

Form1.Designer.cs

namespace ExecuteText
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.TextBox = new System.Windows.Forms.RichTextBox();
            this.Block = new System.Windows.Forms.PictureBox();
            this.Run = new System.Windows.Forms.Button();
            this.ErrorTextBox = new System.Windows.Forms.RichTextBox();
            this.PositionTextBox = new System.Windows.Forms.RichTextBox();
            ((System.ComponentModel.ISupportInitialize)(this.Block)).BeginInit();
            this.SuspendLayout();
            // 
            // TextBox
            // 
            this.TextBox.Location = new System.Drawing.Point(12, 43);
            this.TextBox.Name = "TextBox";
            this.TextBox.Size = new System.Drawing.Size(400, 605);
            this.TextBox.TabIndex = 0;
            this.TextBox.Text = resources.GetString("TextBox.Text");
            this.TextBox.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
            // 
            // Block
            // 
            this.Block.BackColor = System.Drawing.Color.DodgerBlue;
            this.Block.Location = new System.Drawing.Point(591, 314);
            this.Block.Name = "Block";
            this.Block.Size = new System.Drawing.Size(52, 50);
            this.Block.TabIndex = 1;
            this.Block.TabStop = false;
            // 
            // Run
            // 
            this.Run.Location = new System.Drawing.Point(12, 12);
            this.Run.Name = "Run";
            this.Run.Size = new System.Drawing.Size(75, 23);
            this.Run.TabIndex = 2;
            this.Run.Text = "Run";
            this.Run.UseVisualStyleBackColor = true;
            this.Run.Click += new System.EventHandler(this.Run_Click);
            // 
            // ErrorTextBox
            // 
            this.ErrorTextBox.Location = new System.Drawing.Point(12, 665);
            this.ErrorTextBox.Name = "ErrorTextBox";
            this.ErrorTextBox.Size = new System.Drawing.Size(747, 96);
            this.ErrorTextBox.TabIndex = 3;
            this.ErrorTextBox.Text = "";
            // 
            // PositionTextBox
            // 
            this.PositionTextBox.Location = new System.Drawing.Point(418, 43);
            this.PositionTextBox.Name = "PositionTextBox";
            this.PositionTextBox.Size = new System.Drawing.Size(341, 44);
            this.PositionTextBox.TabIndex = 4;
            this.PositionTextBox.Text = "";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(771, 773);
            this.Controls.Add(this.PositionTextBox);
            this.Controls.Add(this.ErrorTextBox);
            this.Controls.Add(this.Run);
            this.Controls.Add(this.Block);
            this.Controls.Add(this.TextBox);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.Block)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.RichTextBox TextBox;
        private System.Windows.Forms.Button Run;
        public System.Windows.Forms.PictureBox Block;
        private System.Windows.Forms.RichTextBox ErrorTextBox;
        private System.Windows.Forms.RichTextBox PositionTextBox;
    }
}

Form1.resx中

<?xml version="1.0" encoding="utf-8"?>
<root>
  <!-- 
    Microsoft ResX Schema 

    Version 2.0

    The primary goals of this format is to allow a simple XML format 
    that is mostly human readable. The generation and parsing of the 
    various data types are done through the TypeConverter classes 
    associated with the data types.

    Example:

    ... ado.net/XML headers & schema ...
    <resheader name="resmimetype">text/microsoft-resx</resheader>
    <resheader name="version">2.0</resheader>
    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
        <value>[base64 mime encoded serialized .NET Framework object]</value>
    </data>
    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
        <comment>This is a comment</comment>
    </data>

    There are any number of "resheader" rows that contain simple 
    name/value pairs.

    Each data row contains a name, and value. The row also contains a 
    type or mimetype. Type corresponds to a .NET class that support 
    text/value conversion through the TypeConverter architecture. 
    Classes that don't support this are serialized and stored with the 
    mimetype set.

    The mimetype is used for serialized objects, and tells the 
    ResXResourceReader how to depersist the object. This is currently not 
    extensible. For a given mimetype the value must be set accordingly:

    Note - application/x-microsoft.net.object.binary.base64 is the format 
    that the ResXResourceWriter will generate, however the reader can 
    read any of the formats listed below.

    mimetype: application/x-microsoft.net.object.binary.base64
    value   : The object must be serialized with 
            : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
            : and then encoded with base64 encoding.

    mimetype: application/x-microsoft.net.object.soap.base64
    value   : The object must be serialized with 
            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
            : and then encoded with base64 encoding.

    mimetype: application/x-microsoft.net.object.bytearray.base64
    value   : The object must be serialized into a byte array 
            : using a System.ComponentModel.TypeConverter
            : and then encoded with base64 encoding.
    -->
  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
    <xsd:element name="root" msdata:IsDataSet="true">
      <xsd:complexType>
        <xsd:choice maxOccurs="unbounded">
          <xsd:element name="metadata">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" />
              </xsd:sequence>
              <xsd:attribute name="name" use="required" type="xsd:string" />
              <xsd:attribute name="type" type="xsd:string" />
              <xsd:attribute name="mimetype" type="xsd:string" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="assembly">
            <xsd:complexType>
              <xsd:attribute name="alias" type="xsd:string" />
              <xsd:attribute name="name" type="xsd:string" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="data">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="resheader">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" />
            </xsd:complexType>
          </xsd:element>
        </xsd:choice>
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
  <resheader name="resmimetype">
    <value>text/microsoft-resx</value>
  </resheader>
  <resheader name="version">
    <value>2.0</value>
  </resheader>
  <resheader name="reader">
    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <resheader name="writer">
    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <data name="TextBox.Text" xml:space="preserve">
    <value>using System;
using System.Drawing;
using System.Windows.Forms;

namespace AStringOfCode
{

 public   class FunctionClass
    {
        enum Directions
        {
            e_Up,
            e_Down,
            e_Left,
            e_Right
        };

        private void Move(PictureBox p, Directions direction)
        {
            var fromPoint = p.Location;
            var toPoint = p.Location;
            switch (direction)
            {
                case Directions.e_Up:
                    toPoint = new Point(fromPoint.X, fromPoint.Y - 1);
                    break;
                case Directions.e_Down:
                    toPoint = new Point(fromPoint.X, fromPoint.Y + 1);
                    break;
                case Directions.e_Left:
                    toPoint = new Point(fromPoint.X - 1, fromPoint.Y);
                    break;
                case Directions.e_Right:
                    toPoint = new Point(fromPoint.X + 1, fromPoint.Y);
                    break;
            }

            p.Location = toPoint;
        }

        public void Run(PictureBox p)
        {
            Move(p, Directions.e_Up);
        }
    }

}</value>
  </data>
</root>

这应该是您正在寻找的。click here for screen image

    When you run the application, you will see three windows and a button 
     1. the code window
     2. the position window
     3. the error window

     and the "Run" Button.
     ...oh yes, let's not forget the "Block" we're moving!

您可以通过将第二个参数更改为4个定义的方向之一来修改“移动”的调用。单击“运行”按钮时,代码窗口中的代码将被编译并运行(如果没有错误),蓝色块将按参数指示的方向移动一个像素为Move(),并且新的位置将显示在“位置”窗口中。

如果编辑中有错误,它们将显示在错误窗口中。

希望这有帮助

答案 2 :(得分:-1)

首先使用string.Split或regex从字符串中提取方法名称。

然后你可以使用反射来调用方法,如下所示:

Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod("MethodName");
theMethod.Invoke(this, parameters, ...);

这需要using System.Reflection;参考。

您可以通过使用正则表达式或string.Split获取参数以获取括号内的内容。将其放入try catch语句中以处理函数何时不存在。