ugp

ugp

  • NA
  • 11
  • 0

Accessing Variables from Different programs

Jun 11 2007 11:32 PM
There is a Form (Form1.cs) with a textbox and a button. textbox contains the text "hellow" And there is a another .cs class file named GeneratedCode.cs (There it will automatically generate simple code. automatic code generate part is working.) What i want is...; when run the program it will appear the Form1.cs and displays the "hellow" in the textbox. When click the button i want to get this text - "hellow" and want to send it to the GeneratedCode.cs file.but; unable to do it. (When click the Generate Code button; the namespace should be "hellow".but currently it is nothing displays.) -------------------------------------------------------------------------------- Here is the GeneratedCode.cs using System; using System.CodeDom; using System.CodeDom.Compiler; using System.Collections; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.IO; using System.Windows.Forms; using Microsoft.CSharp; using WindowsApplication35; namespace ProjTest { public class GeneratedCode { public String thisIsMyVariable; public static CodeCompileUnit BuildHelloWorldGraph() { CodeCompileUnit compileUnit = new CodeCompileUnit(); //This is the Problem area //CodeNamespace samplesmy = new CodeNamespace("Samples"); //THIS Works fine. CodeNamespace samplesmy = new CodeNamespace(new GeneratedCode().thisIsMyVariable); //By using the above line im hoping this. //CodeNamespace samplesmy = new CodeNamespace("hellow");//but not working //if it working: it should display as the 'namespace' of the generated code. but now nothing displays as the namespace //End of the Problem area compileUnit.Namespaces.Add(samplesmy); samplesmy.Imports.Add(new CodeNamespaceImport("System")); CodeTypeDeclaration class1 = new CodeTypeDeclaration("Class1"); samplesmy.Types.Add(class1); CodeEntryPointMethod start = new CodeEntryPointMethod(); CodeTypeReferenceExpression csSystemConsoleType = new CodeTypeReferenceExpression("System.Console"); CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression( csSystemConsoleType, "WriteLine", new CodePrimitiveExpression("Hello World!!!!!")); start.Statements.Add(cs1); CodeMethodInvokeExpression cs2 = new CodeMethodInvokeExpression( csSystemConsoleType, "WriteLine", new CodePrimitiveExpression("Press the Enter key to continue.")); start.Statements.Add(cs2); CodeMethodInvokeExpression csReadLine = new CodeMethodInvokeExpression( csSystemConsoleType, "ReadLine"); start.Statements.Add(csReadLine); class1.Members.Add(start); return compileUnit; } public static void GenerateCode(CodeDomProvider provider, CodeCompileUnit compileunit) { String sourceFile; if (provider.FileExtension[0] == '.') { sourceFile = "TestGraph" + provider.FileExtension; } else { sourceFile = "TestGraph." + provider.FileExtension; } IndentedTextWriter tw = new IndentedTextWriter(new StreamWriter(sourceFile, false), " "); provider.GenerateCodeFromCompileUnit(compileunit, tw, new CodeGeneratorOptions()); tw.Close(); } public static CompilerResults CompileCode(CodeDomProvider provider, String sourceFile, String exeFile) { String[] referenceAssemblies = { "System.dll" }; CompilerParameters cp = new CompilerParameters(referenceAssemblies, exeFile, false); cp.GenerateExecutable = true; CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceFile); return cr; } public class CodeDomExampleForm : System.Windows.Forms.Form { public System.Windows.Forms.Button run_button = new System.Windows.Forms.Button(); private System.Windows.Forms.Button compile_button = new System.Windows.Forms.Button(); private System.Windows.Forms.Button generate_button = new System.Windows.Forms.Button(); private System.Windows.Forms.TextBox textBox1 = new System.Windows.Forms.TextBox(); private System.Windows.Forms.ComboBox comboBox1 = new System.Windows.Forms.ComboBox(); private System.Windows.Forms.Label label1 = new System.Windows.Forms.Label(); private void generate_button_Click(object sender, System.EventArgs e) { CodeDomProvider provider = GetCurrentProvider(); GeneratedCode.GenerateCode(provider, GeneratedCode.BuildHelloWorldGraph()); String sourceFile; if (provider.FileExtension[0] == '.') { sourceFile = "TestGraph" + provider.FileExtension; } else { sourceFile = "TestGraph." + provider.FileExtension; } StreamReader sr = new StreamReader(sourceFile); textBox1.Text = sr.ReadToEnd(); sr.Close(); } private void compile_button_Click(object sender, System.EventArgs e) { CodeDomProvider provider = GetCurrentProvider(); String sourceFile; if (provider.FileExtension[0] == '.') { sourceFile = "TestGraph" + provider.FileExtension; } else { sourceFile = "TestGraph." + provider.FileExtension; } CompilerResults cr = GeneratedCode.CompileCode(provider, sourceFile, "TestGraph.exe"); if (cr.Errors.Count > 0) { // Display compilation errors. textBox1.Text = "Errors encountered while building " + sourceFile + " into " + cr.PathToAssembly + ": \r\n\n"; foreach (CompilerError ce in cr.Errors) textBox1.AppendText(ce.ToString() + "\r\n"); run_button.Enabled = false; } else { textBox1.Text = "Source " + sourceFile + " built into " + cr.PathToAssembly + " with no errors."; run_button.Enabled = true; } } private void run_button_Click(object sender, System.EventArgs e) { Process.Start("TestGraph.exe"); } private CodeDomProvider GetCurrentProvider() { CodeDomProvider provider; switch ((string)this.comboBox1.SelectedItem) { case "CSharp": provider = CodeDomProvider.CreateProvider("CSharp"); break; case "Visual Basic": provider = CodeDomProvider.CreateProvider("VisualBasic"); break; case "JScript": provider = CodeDomProvider.CreateProvider("JScript"); break; default: provider = CodeDomProvider.CreateProvider("CSharp"); break; } return provider; } public CodeDomExampleForm() { this.SuspendLayout(); // Set properties for label1 this.label1.Location = new System.Drawing.Point(395, 20); this.label1.Size = new Size(180, 22); this.label1.Text = "Select a programming language:"; // Set properties for comboBox1 this.comboBox1.Location = new System.Drawing.Point(560, 16); this.comboBox1.Size = new Size(190, 23); this.comboBox1.Name = "comboBox1"; this.comboBox1.Items.AddRange(new string[] { "CSharp", "Visual Basic", "JScript" }); this.comboBox1.Anchor = System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right | System.Windows.Forms.AnchorStyles.Top; this.comboBox1.SelectedIndex = 0; // Set properties for generate_button. this.generate_button.Location = new System.Drawing.Point(8, 16); this.generate_button.Name = "generate_button"; this.generate_button.Size = new System.Drawing.Size(120, 23); this.generate_button.Text = "Generate Code"; this.generate_button.Click += new System.EventHandler(this.generate_button_Click); // Set properties for compile_button. this.compile_button.Location = new System.Drawing.Point(136, 16); this.compile_button.Name = "compile_button"; this.compile_button.Size = new System.Drawing.Size(120, 23); this.compile_button.Text = "Compile"; this.compile_button.Click += new System.EventHandler(this.compile_button_Click); // Set properties for run_button. this.run_button.Enabled = false; this.run_button.Location = new System.Drawing.Point(264, 16); this.run_button.Name = "run_button"; this.run_button.Size = new System.Drawing.Size(120, 23); this.run_button.Text = "Run"; this.run_button.Click += new System.EventHandler(this.run_button_Click); // Set properties for textBox1. this.textBox1.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right); this.textBox1.Location = new System.Drawing.Point(8, 48); this.textBox1.Multiline = true; this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(744, 280); this.textBox1.Text = ""; // Set properties for the CodeDomExampleForm. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(768, 340); this.MinimumSize = new System.Drawing.Size(750, 340); this.Controls.AddRange(new System.Windows.Forms.Control[] {this.textBox1, this.run_button, this.compile_button, this.generate_button, this.comboBox1, this.label1 }); this.Name = "CodeDomExampleForm"; this.Text = "CodeDom Hello World Example"; this.ResumeLayout(false); } protected override void Dispose(bool disposing) { base.Dispose(disposing); } /* [STAThread] static void Main() { Application.Run(new CodeDomExampleForm()); }*/ } } } ------------------------------------------------------------------------------------------------------- This is the way i accessed the variables from the Form1.cs private void button1_Click(object sender, EventArgs e) { GeneratedCode genCode = new GeneratedCode(); genCode.thisIsMyVariable = textBox1.Text; ProjTest.GeneratedCode.CodeDomExampleForm pgc = new GeneratedCode.CodeDomExampleForm(); pgc.Visible = true; } ------- But notworking.. need help! (I upload the program to this) http://www.MegaShare.com/200371 .- WindowsApplication35.zip