Monday, April 28, 2008

Opening Single Window at MDI Form C#

Hello guys. This simple example codes will open a single window. I have 2 ways of opening a window in MDI Form in C#.

1) Creating a Instance in a Form
2) Just create a variable of a Form


Example 1:

-Create a MDI Form and a Form1.

*At you Form1 write this codes:

private static Form1 sForm = null;
public static Form1 Instance()
{
if (sForm == null)
{ sForm = new Form1 (); }
return sForm;
}

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
sForm = null;
}

*In the MDI Form button for showing a Form1 write this codes:

Form sForm = new Form1.Instance();
sForm.MdiParent=this;
sForm.show();
sForm.Activate();


Example 2:

-Create a MDI Form and a Form1.

*Write this codes at your MDI Form:

//Create a variable:
private Form1 frm1 = new Form1();
//Button showing a Form1:
frm1.MdiParent=this;
frm1.show();



I hope this 2 ways of example can help you for those dont known on how to open a single window at C#.

Thursday, April 10, 2008

How to Pass values to another Form

Ok. Lets say you have 2 Forms. Form1 and Form2.
In Visual Basic 6 this is simple codes with 1 line only: Form2.text1.text = Form1.text1.text

Thats so Simple.

But in C# not 1 line code. I have 2 ways to get a text from Form1/Form2.

FIRST WAY:

1.) Goto Form1 then Double Click it. At the code type this.

public string CaptionText
{get {return textBox1.Text;}
set { textBox1.Text = value; }}


note: the value of your textbox1.text = sayre;

2.) Goto Form2 then Double click it. At the code type this.

// At your command button In Form2
private void button1_Click(object sender, EventArgs e)
{
Form1 sForm1 = new Form1();
textBox1.Text = sForm1.CaptionText;
}


SECOND WAY:

1.) Goto Form2 then Double click it. At the code type this.

public Form2(string sTEXT)
{
InitializeComponent();
textBox1.Text = sTEXT;
}


2.) Goto Form1 then Double click it. At the code type this.
//At your command button in Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 sForm = new Form2(textBox1.Text);
sForm.Show();
}