|
|
Hi all,
I am having problems passing a method from form 1 to form 2. I have on form 1 a dgv and a button. When the form 1 button is clicked it takes me to form 2. On form 2 I have the same set up, a dgv and a button. However, the button on Form 2, when clicked should call a method from form 1 then closes form 2. When I run the windows form application I get no errors. The method on form 1 that I try to call from form 2 is public. Any help is much appreciated. Thank you!
Code:
Form 1
public void UpdateGridView()
{
// Method code
}
form 2
public void MapFormUpdate_Click(object sender, EventArgs e)
{
Form1 MainForm = new Form1();
MainForm.UpdateGridView();
this.Close();
}
|
|
|
|
|
You made a new form to call a method on and then throw it away. I would advice you not to ask why that does not do what you want, I'm sure several people will laugh at you and/or 1-vote you.
|
|
|
|
|
I tried it without the Form1 MainForm = new Form1(); code but it gives an error NullReferenceException. How can I get around this?
|
|
|
|
|
You want to call it on the original form, not a new one. Usually you only have 1 instance of the main form anyway, so you could use the singleton pattern (makes managers and Java programmers happy) or just pass the instance of form1 to form2 when you make form2. (or use an event, see Davey's post)
There is a difference between a class and an instance of it. I'm sure you know, but it seems like you need to be reminded of that. But no offense..
|
|
|
|
|
No offense taken. I'm new to C# programming. Usually work with SQL. Thanks for the input!
|
|
|
|
|
Form1 MainForm = new Form1(); is not going to work as it's a new instance of Form1, not the one that you started with.
The recommended way of doing this is to raise a custom event in form2, that Form1 subscribes to, so Form1 calls its own method.
using System;
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonShowForm2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.DoUpdate += new EventHandler(form2_DoUpdate);
form2.Show();
}
void form2_DoUpdate(object sender, EventArgs e)
{
Console.Write("Update");
}
}
using System;
using System.Windows.Forms;
public partial class Form2 : Form
{
public event EventHandler DoUpdate;
public Form2()
{
InitializeComponent();
}
void buttonUpdate_Click(object sender, EventArgs e)
{
OnDoUpdate(EventArgs.Empty);
}
protected virtual void OnDoUpdate(EventArgs e)
{
EventHandler eh = DoUpdate;
if (eh != null)
eh(this, e);
Close();
}
} If you need to pass data along with the event, create your own class derived from EventArgs and pass an instance of that instead of EventArgs.Empty. You will need to change the EventHandler to EventHandler<YourEventArgs>
See my Events Made Simple[^] article for more details.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Thank you for your reply. That works perfectly.
|
|
|
|
|
Hej bwood2020
You Migth whant to use a static resource that can laungh that form1 passes an event to
static class EventHandler
{
static Action Event;
public static void SetGlobalEvent(Action a)
{
Event = a;
}
public static void Execute_Event()
{
Event();
}
}
class Form1
{
public Form1()
{
EventHandler.SetGlobalEvent(Event);
}
public void Event()
{
var x = 2 + 2;
}
}
class Form2
{
public void Onclick()
{
EventHandler.Execute_Event();
this.Destroy();
}
}
There migth be a bether way of doing this if form1 creates form2 you can pass
in "this" from form1 as a parameter that way form2 whill be able to call the method on that object
Hopes this helps Patrik
|
|
|
|
|
Patrik.karlin wrote: if form1 creates form2 you can pass
in "this" from form1 as a parameter
... but now you're starting to couple unrelated classes - not a great idea. The easiest, sure - but best avoided.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
DaveyM69 wrote: ... but now you're starting to couple unrelated classes - not a great idea. The easiest, sure - but best avoided.
.... Yea Sure .. maybe the logic should be in the model....
form1 can subscribe to a datachanged event and form2 youst update's the
data in the model...
then they whill be completle seperated.
|
|
|
|
|
Does this look at all familiar? clickety[^]
[Edit]
Sorry wrong link! Try this one. here[^]
[/Edit]
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi Henry,
It does and I tried to aply the same logic but it wasn't working. The code I used to get this to work was differnt. I think it had to do with adding another event handler to take care of it.
|
|
|
|
|
|
You're looking for the ASP.NET forum. What you asked has nothing to do with C#.
|
|
|
|
|
Dave Kreskowiak wrote: You're looking for the ASP.NET forum
Ftfy, Web Development
Manas Bhardwaj
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
Please, help.... or suggest approach; this is a second posting.
I'm sure that there have been a thousand questions on this topic, but here goes...
I have an application that uses the System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces() to retreive each friendly interface name on a machine with multiple adapters. Later, depending on other choices, the application needs to start UDP multicasting. For this I need to be able to find the IP address of the interface chosen. As I said, all I have is the friendly adpter name. How can retreive the IP address(es) of the NIC starting only the adapter name?
Raven
|
|
|
|
|
Get all interfaces again and search for the one with the right name?
|
|
|
|
|
The call that provices the adapter names has no option to produce the IP addresses assigned to the interface... therein lies the problem--how to find the IP addresses associated with the network interface
|
|
|
|
|
IIRC they are in the same order as returned by Dns.GetHostAddresses
That worked for me, anyway.
would the person who 1-voted me care to explain why? Did I remember incorrectly? (couldn't you just have told me so?) Last modified: 18mins after originally posted --
|
|
|
|
|
It lokos like you got hit by a Drive-By Univoter...
|
|
|
|
|
Yes it would seem so.. he didn't drive around the rest of the forum too much though lol
|
|
|
|
|
When you call GetAllNetworkInterfaces , each instance of the NetworkInterface object you get back has a GetIpProperties method. Call that method on each interface and you'll get an IpInterfaceProperties object with all the IP address details in it for that adapter.
modified on Thursday, July 9, 2009 4:29 PM
|
|
|
|
|
That would be better than my less-than-reliable approach.
|
|
|
|
|
Hi Harold,
I'm not the one that onevoted you, however since I liked your latest message much more than the previous one, here is a 5.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|