[Mono-winforms-list] Adding controls at runtime

Juergen Moeller juergen.moeller@elektrobude.de
Wed, 16 Jun 2004 15:42:08 +0200


Hi,

I tried to add a button on a form at runtime. The following code creates a 
form with a button "Make new button" and a second form. By clicking on the 
button a new button on the second form shold appear. In .NET it does, but 
not in Mono...

Is there something I have done wrong or is it a bug?

I am using Mono Beta 1, Suse9.1, Wine-20040505

Code:
/////////////////////////////////////////////////////
using System;
using System.Windows.Forms;

public class TestForm : System.Windows.Forms.Form {
	
	Button btn;
	
	public TestForm(){
	
		this.Text = "Text";
		this.Width= 320;
		this.Height = 240;
	}
		
	public void AddButton(){

		btn = new Button();
		btn.Text = "test";
		btn.Top=8;
		btn.Left=8;
		this.Controls.Add(btn);
	}
		
}
public class ControlForm : System.Windows.Forms.Form {
	
	Button btn;
	TestForm tf;
	
	public ControlForm(){
	
		this.Text = "ControlForm";
		InitializeComponents();
		Controls.Add(btn);
		tf = new TestForm();
		tf.Left = this.Left+this.Width;
		tf.Show();
	}
		
	public void InitializeComponents(){

		btn = new Button();
		btn.Text = "Make new Button";
		btn.Width = 160;
		btn.Click += new EventHandler(btnClick);
		this.Controls.Add(btn);
	}
		
	public void btnClick(Object o, EventArgs e){
	
		tf.AddButton();
	}
	
	public static void Main(){
	
		Application.Run(new ControlForm());	
	}
}
/////////////////////////////////




- Juergen