473,386 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

delegate returning string how do i capture it?

i have the following
public event ButtonPressedHandler InputButtonPressed;

public delegate string ButtonPressedHandler(string Value);

i am trying to capture that string
how can i do this?

i am basically trying to pass a variable from one form to another when OK
button is pressed
Nov 17 '05 #1
3 1653
First of all, there are two strings there: one is called Value, and the
other is the return value of the delegate method. I'm not sure what the
latter does.

I'm assuming that these two forms are independent. That is, that the
form that needs the value when the OK button is pressed has not
displayed the other form using ShowDialog(). If form1 displays form2
using ShowDialog() like this, then the answer is easy: form2 should
expose a property called WhateverString (or whatever :-), and form1
should query it before disposing of form2:

string resultString = "";
Form2Type form2 = new Form2Type();
if (form2.ShowDialog() == DialogResult.OK)
{
resultString = form2.WhateverString;
}
form2.Dispose();

However, if the two forms are unrelated (form2 is started using Show(),
not ShowDialog()), then the solution is, as you suggested to use an
event. However, I wouldn't use a specific "button pressed" event as
you're suggesting. That strikes me as too much in the "language" of the
insides of form2. What you really want, I think, it to say, "When the
user indicates that he's done with form2, signal form1, so maybe an
event called "Result" would be more appropriate:

public class Form2ResultArgs
{
... just fields, a constructor, and properties to get values out...
}

public delegate void Form2ResultHandler(object sender, Form2ResultArgs
args);

Now, in form2, declare:

public event Form2ResultHandler Result;

and

private void OKbutton_Clicked(object sender, System.EventArgs args)
{
Form2ResultArgs args = new Form2ResultArgs(...the results...);
if (this.Result != null)
{
this.Result(this, args);
}
this.DialogResult = DialogResult.OK;
}

And, in form1 (assuming that you can get your hands on an instance of
Form2... use the Singleton pattern in Form2 if there should only ever
be one):

public Form1()
{
... designer stuff...
... get hands on instance of form2 ...
form2.Result += new Form2ResultHandler(HandleForm2Result);
}

and

private void HandleForm2Result(object sender, Form2ResultArgs args)
{
string resultString = args.ResultString; // or whatever
}

Nov 17 '05 #2
Raj Chudasama <raj@asteriasgi_spamkiller.com> wrote:
i have the following
public event ButtonPressedHandler InputButtonPressed;

public delegate string ButtonPressedHandler(string Value);

i am trying to capture that string
how can i do this?

i am basically trying to pass a variable from one form to another when OK
button is pressed


Where are you trying to capture it? Anything that calls the handler can
just do:

string x = InputButtonPressed();

But that relies on the place you call the handler being the place you
want to get the string...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
1>Declare the delegate in the form (Form1) you want to pass the variable.
2>Register the event in the form(Form2) you want to handle the event.

Something like this in Form2:

this.form1.InputButtonPressed+=new ButtonPressedHandler(
EventHanlderFunction)
Then in Form2, you can receive the value in the function

private string EventHanlderFunction(string value)
{
}

There are plenty of resources on the Internet about this topic, play
with a couple of examples, you'll know it.

Raj Chudasama wrote:
i have the following
public event ButtonPressedHandler InputButtonPressed;

public delegate string ButtonPressedHandler(string Value);

i am trying to capture that string
how can i do this?

i am basically trying to pass a variable from one form to another when OK
button is pressed

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: N8 | last post by:
I am trying to get an exception to occur and consequently found that when adding a target method to a delegates invocation list, a copy of that object is added instead of a reference to the object....
5
by: karch | last post by:
I can't figure out why this code does not compile - says that 'num' in an undeclared identifier and that 'delegate':identifier not found. Thoughts? Help? void MethodD() { List<int>^ data;...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
1
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
1
by: PJ6 | last post by:
I have a generalized delegate, Public Delegate Function DataRetreivingDelegate() as Object with an associated property in a certain class. Almost never will I actually be wanting to set the...
0
by: RSH | last post by:
I am rather new to delegates and I was working through an exersize. I have created a simple commandline project that basically adds or edits customers. I am using two delegates, one to Notify...
2
by: james | last post by:
(Sorry if this is a double post) Hi Guys, I am creating a delegate a couple times and passing in a local variable. However, when the delegate is invoked it uses the variable passed into the...
9
by: Klaudiusz Bryja | last post by:
Hi, I need Delegate.CreateDelegate method equivalent in compact framework. Best regards, Klaudiusz
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.