Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Control

Control

Scheduled Pinned Locked Moved C#
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ImanMahmoud
    wrote on last edited by
    #1

    i want, form another form, and at a specified condition, to create an object from a certain control and add it to the first form, like :

    namespace Win
    {
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1 = new Button();
    private System.Windows.Forms.Button button2 = new Button();
    public System.Windows.Forms.Panel panel1;

    	public Form1()
    	{
    		InitializeComponent();
    	}
    
    	static void Main() 
    	{
    		Application.Run(new Form1());
    	}
    
    	private void button1\_Click(object sender, System.EventArgs e)
    	{
    		this.Close();
    	}
    
    	private void button2\_Click(object sender, System.EventArgs e)
    	{
    		Form2 f2 = new Form2();
    		f2.Show();
    	}
    }// Form1
    
    public class Form2 : System.Windows.Forms.Form
    {
    	private System.Windows.Forms.Button button1 = new Button() ;
    
    	public Form2()
    	{
    		InitializeComponent();
    	}
    
    	private void button1\_Click(object sender, System.EventArgs e)
    	{
    		// at a specified condition :
    
    		Button b = new Button();
    		b.Text = "Hello" ;
    		Form1 f1 = new Form1();
    		f1.panel1.Controls.Add(b);
    	}
    
    }// Form2
    

    }// namespace Win

    this code does not work. What is the wrong here ? thanks

    C W 2 Replies Last reply
    0
    • I ImanMahmoud

      i want, form another form, and at a specified condition, to create an object from a certain control and add it to the first form, like :

      namespace Win
      {
      public class Form1 : System.Windows.Forms.Form
      {
      private System.Windows.Forms.Button button1 = new Button();
      private System.Windows.Forms.Button button2 = new Button();
      public System.Windows.Forms.Panel panel1;

      	public Form1()
      	{
      		InitializeComponent();
      	}
      
      	static void Main() 
      	{
      		Application.Run(new Form1());
      	}
      
      	private void button1\_Click(object sender, System.EventArgs e)
      	{
      		this.Close();
      	}
      
      	private void button2\_Click(object sender, System.EventArgs e)
      	{
      		Form2 f2 = new Form2();
      		f2.Show();
      	}
      }// Form1
      
      public class Form2 : System.Windows.Forms.Form
      {
      	private System.Windows.Forms.Button button1 = new Button() ;
      
      	public Form2()
      	{
      		InitializeComponent();
      	}
      
      	private void button1\_Click(object sender, System.EventArgs e)
      	{
      		// at a specified condition :
      
      		Button b = new Button();
      		b.Text = "Hello" ;
      		Form1 f1 = new Form1();
      		f1.panel1.Controls.Add(b);
      	}
      
      }// Form2
      

      }// namespace Win

      this code does not work. What is the wrong here ? thanks

      C Offline
      C Offline
      Charlie Williams
      wrote on last edited by
      #2

      In Form2.button1_Click you're creating a new instance of Form1, adding a Button to its panel1 control and... well, that's it. f1 is going the way of the dodo bird when the method exits. What I think you're trying to do is add the Button to the instance of Form1 that called Form2.Show in the first place. You'll need a reference to that object in Form2. You can pass it in Form2's constructor or as a property. Here's an example:

      public class Form2 : Form
      {
      Form1 _form1;
      public Form2(Form1 form1)
      {
      _form1 = form1;
      }

      private void button1_Click(object sender, EventArgs e)
      {
      // create button as before
      _form1.panel1.Controls.Add(b);
      }

      // rest of class
      }

      public class Form1 : Form
      {
      private void button2_Click(object sender, System.EventArgs e)
      {
      Form2 f2 = new Form2(this);
      f2.Show();
      }

      // rest of class
      }

      Charlie if(!curlies){ return; }

      1 Reply Last reply
      0
      • I ImanMahmoud

        i want, form another form, and at a specified condition, to create an object from a certain control and add it to the first form, like :

        namespace Win
        {
        public class Form1 : System.Windows.Forms.Form
        {
        private System.Windows.Forms.Button button1 = new Button();
        private System.Windows.Forms.Button button2 = new Button();
        public System.Windows.Forms.Panel panel1;

        	public Form1()
        	{
        		InitializeComponent();
        	}
        
        	static void Main() 
        	{
        		Application.Run(new Form1());
        	}
        
        	private void button1\_Click(object sender, System.EventArgs e)
        	{
        		this.Close();
        	}
        
        	private void button2\_Click(object sender, System.EventArgs e)
        	{
        		Form2 f2 = new Form2();
        		f2.Show();
        	}
        }// Form1
        
        public class Form2 : System.Windows.Forms.Form
        {
        	private System.Windows.Forms.Button button1 = new Button() ;
        
        	public Form2()
        	{
        		InitializeComponent();
        	}
        
        	private void button1\_Click(object sender, System.EventArgs e)
        	{
        		// at a specified condition :
        
        		Button b = new Button();
        		b.Text = "Hello" ;
        		Form1 f1 = new Form1();
        		f1.panel1.Controls.Add(b);
        	}
        
        }// Form2
        

        }// namespace Win

        this code does not work. What is the wrong here ? thanks

        W Offline
        W Offline
        WillemM
        wrote on last edited by
        #3

        ImanMahmoud wrote: public Form1() { InitializeComponent(); } I don't see the method InitializeComponent. Either create it, or get rid of this line :) "Every rule in a world of bits and bytes can be bend or eventually be broken"

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups