Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
hey guys,
 
i have been making my programming language and thanks to some very helpful people, i am now able to create windows forms using my programming language, but that is all. i need to be able to add things to it like buttons and text boxes. the first thing is buttons and this is my current code:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
 
namespace programming_language_test_1
{
    class Program : System.Windows.Forms.Form
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(args[0]);
            string code = sr.ReadToEnd();
            Run(code);
        }
 
        static void Run(string code)
        {
            code = code.Replace(((char)13).ToString(), "");
 
            foreach (string a in code.Split('\n'))
            {
                if (a == "new form")
                {
                    Form form = new Form();
                    form.Activate();
                    form.Enabled = true;
                    form.Focus();
                    Application.EnableVisualStyles();
                    Application.Run(form);
                }
 
                if (a == "new button")
                {
                    Button button = new Button();
                    button.Visible = true;
                    button.Enabled = true;
                    button.Focus();
                    button.Text = "i am a button";
                    button.Location = new Point(50, 50);
                }
            }
        }
    }
}
 
1. make a new console application
2. add a windows form
3. add this code in

 
if you were going to test this code out by using:
 
new form
new button
 
you would soon see that a new form is created, but the button isn't on the form. I need help on making buttons and everything on the form.
Posted 15-Sep-12 0:00am
Edited 15-Sep-12 0:34am
v2
Comments
Ganesh Nikam at 15-Sep-12 6:33am
   
hii you need to add control to form
Ganesh Nikam at 15-Sep-12 6:34am
   
i.e form.controls.add(button)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You have the wrong approach for this.
A Button class has to be assigned to a Form before it appears.
Like
 
Form f = new Form();
Button b = new Button();
 
f.Controls.Add(b);
 
Create you Form, and in the constructor of the From you then create the Button.
The best help you can do for your self, is to start a new project in Visual Studio with a Form project.
From there you can see all the code that Visual Studio Form designer is making.
 
Good luck Smile | :)
  Permalink  
Comments
Member 8378691 at 15-Sep-12 6:44am
   
this does help, it introduced me to the Form.Controls.Add() function. this isn't the full answer so i won't 'Accept Solution', but i do want you to know that you help me a lot. thnx :)
Kim Togo at 18-Sep-12 2:03am
   
Great to hear.
But writing all the code for you, will not make you a better coder.
The best way to learn to code, is to do it your self. But I am glad I could help.
Wes Aday at 15-Sep-12 10:31am
   
I have upvoted this one. This is the answer the OP was looking for. He just won't accept the solution because you did not write his code for him.
Kim Togo at 18-Sep-12 2:04am
   
Thanks Wes
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hey you must add your button to the form ..
 Button button = new Button();
                    button.Visible = true;
                    button.Enabled = true;
                    button.Focus();
                    button.Text = "i am a button";
                    button.Location = new Point(50, 50);
this.Controls.Add(button);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This sounds like homework to me.
However, How to Convert a Console App into a Windows App in C#[^] should help you out.
 
This describes the various assemblies / steps to use a form with a console application.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Dnyaneshwar@Pune 604
1 thatraja 370
2 OriginalGriff 319
3 Sergey Alexandrovich Kryukov 243
4 Gihan Liyanage 194
0 OriginalGriff 737
1 Dnyaneshwar@Pune 604
2 Kornfeld Eliyahu Peter 505
3 Sergey Alexandrovich Kryukov 372
4 thatraja 370


Advertise | Privacy | Mobile
Web04 | 2.8.140902.1 | Last Updated 15 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100