Two froms and one procedure

  • Thread starter Thread starter rozrabiaka
  • Start date Start date
R

rozrabiaka

Hi everybody!

I have two forms in C#. In the main form I have procedure that i need to
run from second form when I click button. How I do it?

Thanks for help, gregory.
 
Create an 'internal' or 'public' procedure in Form2...

\\\
internal void myProcedure()
{
MessageBox.Show("Procedure in Form2", "MyProject");
}
///

....then call it from Form1...

\\\
private void button1_Click(object sender, System.EventArgs e)
{
Form2 objForm2 = new Form2();

objForm2.Show();
objForm2.myProcedure();
}
///

HTH,
Gary
 
U¿ytkownik Gary Milton napisa³:
Create an 'internal' or 'public' procedure in Form2...

\\\
internal void myProcedure()
{
MessageBox.Show("Procedure in Form2", "MyProject");
}
///

Thanks for You help, but I need exactly reverse :)

Gretings, gregory
 
rozrabiaka said:
I have two forms in C#. In the main form I have procedure that i need to
run from second form when I click button. How I do it?

If you answer the following question, it'll be easier to give you a
decent answer: What does this procedure do?
 
Gregory,

In that case, in Form1 create an 'internal' or 'public' procedure...

\\\
internal void myProcedure()
{
MessageBox.Show("Procedure in Form1", "MyProject");
}
///

....then in Form2 create an overloaded constructor that will take a reference
to Form1...

\\\
private Form1 myForm1;

public Form2(Form1 objForm1)
{
myForm1 = objForm1;

InitializeComponent();
}
///

....and when you create and instance of Form2 in Form1, pass the reference to
itself...

\\\
Form2 objForm2 = new Form2(this);

objForm2.Show();
///

....which enables you to call the procedure in instance of Form1 that called
Form2...

\\\
// Button in Form2

private void button1_Click(object sender, System.EventArgs e)
{
myForm1.myProcedure();
}
///

HTH,
Gary
 
Uz.ytkownik C# Learner napisa?:
rozrabiaka wrote:

If you answer the following question, it'll be easier to give you a
decent answer: What does this procedure do?

Open Connection to database, geting from second form from textBoxes
parameters and executing query at database. The same procedure is
executing after run the program.

(sorry for my english :( )
 
rozrabiaka said:
Uz.ytkownik C# Learner napisa?:


Open Connection to database, geting from second form from textBoxes
parameters and executing query at database. The same procedure is
executing after run the program.

Is the second form a modal form?

If so, something like the following would be better:

class SecondForm : Form
{
//...
public DialogResult ShowDialog(out string a, out string b)
{
DialogResult result = ShowDialog();
a = firstTextBox.Text;
b = secondTextBox.Text;
return result;
}
}

class FirstForm : Form
{
//...
void Foo()
{
string a;
string b;

SecondForm f = new SecondForm();
if (f.ShowDialog(out a, out b) == DialogResult.OK) {
DoSomethingWith(a, b);
}
}
}

On 'SecondForm' you might have 'OK' and 'Cancel' buttons. Their
'DialogResult' properties should be set to 'OK' and 'Cancel', respectively.
 
Since a Form is a class, create the procedure as 'public' and through
the instance of Form1 (create this instance in Form2) as below

Form1 f1=new Form1();

call the procedure from Form2 as below

f1.ProcedureName();

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Uz.ytkownik Ravichandran J.V. napisa?:
Since a Form is a class, create the procedure as 'public' and through
the instance of Form1 (create this instance in Form2) as below

Form1 f1=new Form1();

call the procedure from Form2 as below

f1.ProcedureName();

with regards,

Then the procedure is running in NEW form1 not in existing, I know it,
because I do it.

The problem is corrected, EOT

gregory
 
Back
Top