Calling a method from another form
-
Hello again I have two forms, one being opened by another. What I want to do is that the second form that is opened by the first one is when it is closing, I want it to run a method in first form. The code I used is something like this.
private void frmAddCategory_FormClosed(object sender, FormClosedEventArgs e) { frmCore core = new frmCore(); core.comboRefresh("newPost"); //frmStart start = (frmStart)this.Owner; }
It doesnt work. I checked with debug and it seems to run, but nothing seems to happens to the first form. Its a dropdownlist that has items in it, the second form adds new items to it in a database. the method that is called is taking data from a database and updates the dropdownlist with the new items.ah those vb programmers :)
frmCore core = new frmCore();
creates a new instance of your mainform. so calling a method on this new instance will not affect the first form. i wouldn't do it like this anyway. my preferred solution for such things is this:public class frmAddCategory:Form
{
//your stuff for the form here
public string NewPost
{
get
{
return txtNewPost.Text; // i guess you want to add the entered Value into the callers ComboBox..
}
}
}
public class frmCore:Form
{
// your code here..
private void SomeButton_Click(object sender, EventArgs e)
{
frmAddCategory blah = new frmAddCategory();
if (blah.ShowDialog() == DialogResult.OK)
{
myComboBox.Items.Add(blah.NewPost);
}
}
}greets M@u
-
Hello again I have two forms, one being opened by another. What I want to do is that the second form that is opened by the first one is when it is closing, I want it to run a method in first form. The code I used is something like this.
private void frmAddCategory_FormClosed(object sender, FormClosedEventArgs e) { frmCore core = new frmCore(); core.comboRefresh("newPost"); //frmStart start = (frmStart)this.Owner; }
It doesnt work. I checked with debug and it seems to run, but nothing seems to happens to the first form. Its a dropdownlist that has items in it, the second form adds new items to it in a database. the method that is called is taking data from a database and updates the dropdownlist with the new items.Deques wrote:
frmCore core = new frmCore();
This will create a new instance for the form and method in that instance will be called. I prefer to use delegates in this scenario. Create delegate in second form and hook a method handler from first form when second form is invoked. On second forms
FormClosed
event, invoke this delegate. Hope it helpsAll C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
ah those vb programmers :)
frmCore core = new frmCore();
creates a new instance of your mainform. so calling a method on this new instance will not affect the first form. i wouldn't do it like this anyway. my preferred solution for such things is this:public class frmAddCategory:Form
{
//your stuff for the form here
public string NewPost
{
get
{
return txtNewPost.Text; // i guess you want to add the entered Value into the callers ComboBox..
}
}
}
public class frmCore:Form
{
// your code here..
private void SomeButton_Click(object sender, EventArgs e)
{
frmAddCategory blah = new frmAddCategory();
if (blah.ShowDialog() == DialogResult.OK)
{
myComboBox.Items.Add(blah.NewPost);
}
}
}greets M@u
m@u wrote:
public class frmAddCategory:Form
{
//your stuff for the form here
public string NewPost
{
get
{
return txtNewPost.Text; // i guess you want to add the entered Value into the callers ComboBox..
}
}
}
public class frmCore:Form
{
// your code here..
private void SomeButton_Click(object sender, EventArgs e)
{
frmAddCategory blah = new frmAddCategory();
if (blah.ShowDialog() == DialogResult.OK)
{
myComboBox.Items.Add(blah.NewPost);
}
}
}What is this ? This looks totally out of topic to me !
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
ah those vb programmers :)
frmCore core = new frmCore();
creates a new instance of your mainform. so calling a method on this new instance will not affect the first form. i wouldn't do it like this anyway. my preferred solution for such things is this:public class frmAddCategory:Form
{
//your stuff for the form here
public string NewPost
{
get
{
return txtNewPost.Text; // i guess you want to add the entered Value into the callers ComboBox..
}
}
}
public class frmCore:Form
{
// your code here..
private void SomeButton_Click(object sender, EventArgs e)
{
frmAddCategory blah = new frmAddCategory();
if (blah.ShowDialog() == DialogResult.OK)
{
myComboBox.Items.Add(blah.NewPost);
}
}
}greets M@u
Im not a VB coder, but I guess I use their method or something like that Anyway. It looks like it pass a value to a variable to the first form, which isnt what I wanted to do. I wanted to call a method. But maybe a similiar way to do like you stated above. Can you show me some sample code to call a method (function, procedure, whatever :P)
-
Deques wrote:
frmCore core = new frmCore();
This will create a new instance for the form and method in that instance will be called. I prefer to use delegates in this scenario. Create delegate in second form and hook a method handler from first form when second form is invoked. On second forms
FormClosed
event, invoke this delegate. Hope it helpsAll C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
How does it work? I am not familiar with delegates >.< Could you provide me some code examples, please?
These may help: http://www.codeproject.com/info/search.asp?cats=3&searchkw=delegates&Submit1=Search&author=&sd=15+Nov+1999&ed=28+Nov+2007[^]
My current favourite word is: PIE! I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for. -
The Undefeated
-
Im not a VB coder, but I guess I use their method or something like that Anyway. It looks like it pass a value to a variable to the first form, which isnt what I wanted to do. I wanted to call a method. But maybe a similiar way to do like you stated above. Can you show me some sample code to call a method (function, procedure, whatever :P)
sorry i thought you want to pass a value entered in frmAddCategory. for calling a method, like navaneeth says, a delegate/event would be appropriate:
public class frmAddCategory
{
public event EventHandler CategoryAdded;
protected virtual void onCategoryAdded(EventHandler e)
{
if (CategoryAdded != null)
{
CategoryAdded(this,e);
}
}
private void someEventHandler(...)
{
onCategoryAdded(EventArgs.Empty);
}
//...
}in the calling Form after creating frmAddCategory say
myForm.CategoryAdded += new EventHandler(myForm_CategoryAdded);
and in themyForm_CategoryAdded
you can then add the Entry to the Combobox. -
How does it work? I am not familiar with delegates >.< Could you provide me some code examples, please?
Deques wrote:
I am not familiar with delegates
Delegates are function pointers in which you can assign a function's reference, and when delegate is invoked, supplied function will be called. For explaining this we have two forms say
Form1
andForm2
. I am invoking Form2 from Form1's load event. Inside Form2 I have declared a delegate. See the Form2 code belowpublic class Form2 : System.Windows.Forms.Form
{
public delegate void FormClosed();
public FormClosed FormClosedHandler;private void Form2\_Closed(object sender, System.EventArgs e) { FormClosedHandler(); //Invoking delegate }
}
In the above code, you can see I have created a
FormClosed()
delegate and an object for that delegate. This object I will be assigning from the Form1 just before Form2 is shown. See theForm1
code belowpublic class Form1 : System.Windows.Forms.Form
{
private void Form1_Load(object sender, System.EventArgs e)
{
Form2 obj = new Form2();
obj.FormClosedHandler += new Form2.FormClosed(this.Form2ClosedHandler);
obj.Show();
}
}In this I have assigned a private method
Form2ClosedHandler
to the delegate. So when delegate is invoked, this private method in Form1 will get called. You can refresh your combo inside this method. Delegates are good for communicating between classes. Hope this helpsAll C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
Hello again I have two forms, one being opened by another. What I want to do is that the second form that is opened by the first one is when it is closing, I want it to run a method in first form. The code I used is something like this.
private void frmAddCategory_FormClosed(object sender, FormClosedEventArgs e) { frmCore core = new frmCore(); core.comboRefresh("newPost"); //frmStart start = (frmStart)this.Owner; }
It doesnt work. I checked with debug and it seems to run, but nothing seems to happens to the first form. Its a dropdownlist that has items in it, the second form adds new items to it in a database. the method that is called is taking data from a database and updates the dropdownlist with the new items.implement delegate and raise event. the problem in your code it's making the new copy of your form and you need to perform some action on your previously opened form
-
Deques wrote:
I am not familiar with delegates
Delegates are function pointers in which you can assign a function's reference, and when delegate is invoked, supplied function will be called. For explaining this we have two forms say
Form1
andForm2
. I am invoking Form2 from Form1's load event. Inside Form2 I have declared a delegate. See the Form2 code belowpublic class Form2 : System.Windows.Forms.Form
{
public delegate void FormClosed();
public FormClosed FormClosedHandler;private void Form2\_Closed(object sender, System.EventArgs e) { FormClosedHandler(); //Invoking delegate }
}
In the above code, you can see I have created a
FormClosed()
delegate and an object for that delegate. This object I will be assigning from the Form1 just before Form2 is shown. See theForm1
code belowpublic class Form1 : System.Windows.Forms.Form
{
private void Form1_Load(object sender, System.EventArgs e)
{
Form2 obj = new Form2();
obj.FormClosedHandler += new Form2.FormClosed(this.Form2ClosedHandler);
obj.Show();
}
}In this I have assigned a private method
Form2ClosedHandler
to the delegate. So when delegate is invoked, this private method in Form1 will get called. You can refresh your combo inside this method. Delegates are good for communicating between classes. Hope this helpsAll C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
Thanks for the information I have tried this code, but when I build it I get this error the private method I have assigned with. I thnk I have done something wrong It says "Method name expected" This is the code I used for form 1
private void btnNewPost\_Click(object sender, EventArgs e) { Forms.frmAddCategory addcats = new frmAddCategory(); addcats.Refresher += new Forms.frmAddCategory.refreshcat(this.comboRefresh("newPost")); addcats.ShowDialog(); }
comboRefresh(string blah)
{
//code to refresh the combobox
}and this is for the form 2
public partial class frmAddCategory : Form { //Delegate public delegate void refreshcat(); public refreshcat Refresher; private void frmAddCategory\_FormClosed(object sender, FormClosedEventArgs e) { Refresher(); }
-
Thanks for the information I have tried this code, but when I build it I get this error the private method I have assigned with. I thnk I have done something wrong It says "Method name expected" This is the code I used for form 1
private void btnNewPost\_Click(object sender, EventArgs e) { Forms.frmAddCategory addcats = new frmAddCategory(); addcats.Refresher += new Forms.frmAddCategory.refreshcat(this.comboRefresh("newPost")); addcats.ShowDialog(); }
comboRefresh(string blah)
{
//code to refresh the combobox
}and this is for the form 2
public partial class frmAddCategory : Form { //Delegate public delegate void refreshcat(); public refreshcat Refresher; private void frmAddCategory\_FormClosed(object sender, FormClosedEventArgs e) { Refresher(); }
Deques wrote:
addcats.Refresher += new Forms.frmAddCategory.refreshcat(this.comboRefresh("newPost"));
addcats.Refresher += new Forms.frmAddCategory.refreshcat(this.comboRefresh);
If your
comboRefresh()
method takes string parameter, you should change the delegate likeDeques wrote:
public delegate void refreshcat();
public delegate void refreshcat(string str);
Deques wrote:
private void frmAddCategory_FormClosed(object sender, FormClosedEventArgs e) { Refresher(); }
private void frmAddCategory_FormClosed(object sender, FormClosedEventArgs e)
{
Refresher("string to be passed");
}All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
Deques wrote:
addcats.Refresher += new Forms.frmAddCategory.refreshcat(this.comboRefresh("newPost"));
addcats.Refresher += new Forms.frmAddCategory.refreshcat(this.comboRefresh);
If your
comboRefresh()
method takes string parameter, you should change the delegate likeDeques wrote:
public delegate void refreshcat();
public delegate void refreshcat(string str);
Deques wrote:
private void frmAddCategory_FormClosed(object sender, FormClosedEventArgs e) { Refresher(); }
private void frmAddCategory_FormClosed(object sender, FormClosedEventArgs e)
{
Refresher("string to be passed");
}All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
Thanks. It works now :D Only thing that the combobox doesnt update directly sometimes. But thats another problem
Welcome. Glad to hear that it helped
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions