Button to return the value of a variable stringHow do I get a return value from a Button Click event?What is the difference between String and string in C#?How do you give a C# Auto-Property a default value?Case insensitive 'Contains(string)'How do I get a consistent byte representation of strings in C# without manually specifying an encoding?Get int value from enum in C#back to the previous page with values still present in textboxesIs there a reason for C#'s reuse of the variable in a foreach?Windows Application - Custom Event is getting called multiple timesSample onedrive winforms app vb.netHttpWebRequest from a local WCF service
How can I finish my PhD?
How strong is aircraft-grade spruce?
Is there a specific way to describe over-grown, old, tough vegetables?
Bacteria vats to generate edible biomass, require intermediary species?
Does the 2019 UA artificer need to prepare the Lesser Restoration spell to cast it with their Alchemical Mastery feature?
Are there any space probes or landers which regained communication after being lost?
How do Scrum teams manage their dependencies on other teams?
Why would an airport be depicted with symbology for runways longer than 8,069 feet even though it is reported on the sectional as 7,200 feet?
Poor management handling of recent sickness and how to approach my return?
After a few interviews, What should I do after told to wait?
Contour plot of a sequence of spheres with increasing radius
Why can't some airports handle heavy aircraft while others do it easily (same runway length)?
How to find a reviewer/editor for my paper?
How should Thaumaturgy's "three times as loud as normal" be interpreted?
How to reference a custom counter that shows section number?
is it possible to change a material depending on whether it is intersecting with another object?
How to set any file manager in Linux to show the duration like the Length feature in Windows Explorer?
How would two worlds first establish an exchange rate between their currencies
Lost & Found Mobile Telepone
Why can linguists decide which use of language is correct and which is not?
Strategies for dealing with chess burnout?
What makes an ending "happy"?
What makes things real?
Yet another calculator problem
Button to return the value of a variable string
How do I get a return value from a Button Click event?What is the difference between String and string in C#?How do you give a C# Auto-Property a default value?Case insensitive 'Contains(string)'How do I get a consistent byte representation of strings in C# without manually specifying an encoding?Get int value from enum in C#back to the previous page with values still present in textboxesIs there a reason for C#'s reuse of the variable in a foreach?Windows Application - Custom Event is getting called multiple timesSample onedrive winforms app vb.netHttpWebRequest from a local WCF service
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Can a button return the value of a variable?is it possible? I'm new in winform so...understand me. The final goal is to have a simple code with two buttons that if clicked return a msgbox with the value of a var.
private string button3_Click(object sender, EventArgs e)
string res = "PASS";
MessageBox.Show(res);
return res;
private string button4_Click(object sender, EventArgs e)
string res = "FAIL";
MessageBox.Show(res);
return res;
c# winforms
|
show 3 more comments
Can a button return the value of a variable?is it possible? I'm new in winform so...understand me. The final goal is to have a simple code with two buttons that if clicked return a msgbox with the value of a var.
private string button3_Click(object sender, EventArgs e)
string res = "PASS";
MessageBox.Show(res);
return res;
private string button4_Click(object sender, EventArgs e)
string res = "FAIL";
MessageBox.Show(res);
return res;
c# winforms
You are already doing it when you are passingres
value toMessageBox.Show(res)
– iSahilSharma
Mar 28 at 7:24
You could write:private void DisplayMessage(string message) MessageBox.Show(message);
and then call this method in both button click function like:DisplayMessage(res);
– Prashant Pimpale
Mar 28 at 7:24
Who is supposed to consume the value returned?
– Dmitry Bychenko
Mar 28 at 7:24
Possible duplicate of How do I get a return value from a Button Click event?
– SᴇM
Mar 28 at 7:25
oh hi again @DmitryBychenko,the returned value is going to be used by another app in wpf
– codroipo
Mar 28 at 7:27
|
show 3 more comments
Can a button return the value of a variable?is it possible? I'm new in winform so...understand me. The final goal is to have a simple code with two buttons that if clicked return a msgbox with the value of a var.
private string button3_Click(object sender, EventArgs e)
string res = "PASS";
MessageBox.Show(res);
return res;
private string button4_Click(object sender, EventArgs e)
string res = "FAIL";
MessageBox.Show(res);
return res;
c# winforms
Can a button return the value of a variable?is it possible? I'm new in winform so...understand me. The final goal is to have a simple code with two buttons that if clicked return a msgbox with the value of a var.
private string button3_Click(object sender, EventArgs e)
string res = "PASS";
MessageBox.Show(res);
return res;
private string button4_Click(object sender, EventArgs e)
string res = "FAIL";
MessageBox.Show(res);
return res;
c# winforms
c# winforms
edited Mar 28 at 8:32
Matt Clegg
933 silver badges13 bronze badges
933 silver badges13 bronze badges
asked Mar 28 at 7:21
codroipocodroipo
8810 bronze badges
8810 bronze badges
You are already doing it when you are passingres
value toMessageBox.Show(res)
– iSahilSharma
Mar 28 at 7:24
You could write:private void DisplayMessage(string message) MessageBox.Show(message);
and then call this method in both button click function like:DisplayMessage(res);
– Prashant Pimpale
Mar 28 at 7:24
Who is supposed to consume the value returned?
– Dmitry Bychenko
Mar 28 at 7:24
Possible duplicate of How do I get a return value from a Button Click event?
– SᴇM
Mar 28 at 7:25
oh hi again @DmitryBychenko,the returned value is going to be used by another app in wpf
– codroipo
Mar 28 at 7:27
|
show 3 more comments
You are already doing it when you are passingres
value toMessageBox.Show(res)
– iSahilSharma
Mar 28 at 7:24
You could write:private void DisplayMessage(string message) MessageBox.Show(message);
and then call this method in both button click function like:DisplayMessage(res);
– Prashant Pimpale
Mar 28 at 7:24
Who is supposed to consume the value returned?
– Dmitry Bychenko
Mar 28 at 7:24
Possible duplicate of How do I get a return value from a Button Click event?
– SᴇM
Mar 28 at 7:25
oh hi again @DmitryBychenko,the returned value is going to be used by another app in wpf
– codroipo
Mar 28 at 7:27
You are already doing it when you are passing
res
value to MessageBox.Show(res)
– iSahilSharma
Mar 28 at 7:24
You are already doing it when you are passing
res
value to MessageBox.Show(res)
– iSahilSharma
Mar 28 at 7:24
You could write:
private void DisplayMessage(string message) MessageBox.Show(message);
and then call this method in both button click function like: DisplayMessage(res);
– Prashant Pimpale
Mar 28 at 7:24
You could write:
private void DisplayMessage(string message) MessageBox.Show(message);
and then call this method in both button click function like: DisplayMessage(res);
– Prashant Pimpale
Mar 28 at 7:24
Who is supposed to consume the value returned?
– Dmitry Bychenko
Mar 28 at 7:24
Who is supposed to consume the value returned?
– Dmitry Bychenko
Mar 28 at 7:24
Possible duplicate of How do I get a return value from a Button Click event?
– SᴇM
Mar 28 at 7:25
Possible duplicate of How do I get a return value from a Button Click event?
– SᴇM
Mar 28 at 7:25
oh hi again @DmitryBychenko,the returned value is going to be used by another app in wpf
– codroipo
Mar 28 at 7:27
oh hi again @DmitryBychenko,the returned value is going to be used by another app in wpf
– codroipo
Mar 28 at 7:27
|
show 3 more comments
2 Answers
2
active
oldest
votes
Well, button3_Click
and alike are so-called callback functions which are called by system and that's why do not return any value (the system doesn't need it). Let's extract a method:
private string MyButtonClick(object sender)
string result = "";
if (sender == button3)
result = "PASS"; //TODO: better read it from resources, not hadcoded
else if (sender == button4)
result = "FAIL"; //TODO: better read it from resources, not hadcoded
if (!string.IsNullOrEmpty(result))
MessageBox.Show(result);
return result;
And then you can put:
// System callback function
// void: System callback function signature
private void button3_Click(object sender, EventArgs e)
MyButtonClick(sender);
// void: System callback function signature
private void button4_Click(object sender, EventArgs e)
MyButtonClick(sender);
Or
// custom code
string myClickMessage = MyButtonClick(button4);
you'll never disappoint me,that was a easy shot...thank you so much dmitry...again
– codroipo
Mar 28 at 7:40
only a little improovement th types of the buttons is void not string
– codroipo
Mar 28 at 7:41
@mad codroipo: sure, it should bevoid
:void button3_Click
andvoid button4_Click
– Dmitry Bychenko
Mar 28 at 7:48
the only remaining question is how could i pick up the result from another app and insert it into a db?
– codroipo
Mar 28 at 7:52
@mad codroipo: applications interaction (if "another app" is a different exe file) is a difficult procedure (say, both exe establish named pipe(s) and send data to it in order to interact). If "another app" is just another form of the same exe all you have to do is to modifyMyButtonClick
(e.g. write code to save the message into RDBMS)
– Dmitry Bychenko
Mar 28 at 7:56
|
show 3 more comments
Click event handlers have strongly typed signature, i.e. the return type must be void
, so you cannot return anything from them.
But, if you are working with windows Forms, you have class for your form, where you placed those event hadnlers. So what you can do is: define private variable:
pirvate string _res;
and then assign it value in your click event hadlers, eg.:
private string button3_Click(object sender, EventArgs e)
_res = "PASS";
MessageBox.Show(_res);
Then in the code you could use this value as you want.
If you want to use it outside your class, define property:
public string Res
get return _res;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55392105%2fbutton-to-return-the-value-of-a-variable-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Well, button3_Click
and alike are so-called callback functions which are called by system and that's why do not return any value (the system doesn't need it). Let's extract a method:
private string MyButtonClick(object sender)
string result = "";
if (sender == button3)
result = "PASS"; //TODO: better read it from resources, not hadcoded
else if (sender == button4)
result = "FAIL"; //TODO: better read it from resources, not hadcoded
if (!string.IsNullOrEmpty(result))
MessageBox.Show(result);
return result;
And then you can put:
// System callback function
// void: System callback function signature
private void button3_Click(object sender, EventArgs e)
MyButtonClick(sender);
// void: System callback function signature
private void button4_Click(object sender, EventArgs e)
MyButtonClick(sender);
Or
// custom code
string myClickMessage = MyButtonClick(button4);
you'll never disappoint me,that was a easy shot...thank you so much dmitry...again
– codroipo
Mar 28 at 7:40
only a little improovement th types of the buttons is void not string
– codroipo
Mar 28 at 7:41
@mad codroipo: sure, it should bevoid
:void button3_Click
andvoid button4_Click
– Dmitry Bychenko
Mar 28 at 7:48
the only remaining question is how could i pick up the result from another app and insert it into a db?
– codroipo
Mar 28 at 7:52
@mad codroipo: applications interaction (if "another app" is a different exe file) is a difficult procedure (say, both exe establish named pipe(s) and send data to it in order to interact). If "another app" is just another form of the same exe all you have to do is to modifyMyButtonClick
(e.g. write code to save the message into RDBMS)
– Dmitry Bychenko
Mar 28 at 7:56
|
show 3 more comments
Well, button3_Click
and alike are so-called callback functions which are called by system and that's why do not return any value (the system doesn't need it). Let's extract a method:
private string MyButtonClick(object sender)
string result = "";
if (sender == button3)
result = "PASS"; //TODO: better read it from resources, not hadcoded
else if (sender == button4)
result = "FAIL"; //TODO: better read it from resources, not hadcoded
if (!string.IsNullOrEmpty(result))
MessageBox.Show(result);
return result;
And then you can put:
// System callback function
// void: System callback function signature
private void button3_Click(object sender, EventArgs e)
MyButtonClick(sender);
// void: System callback function signature
private void button4_Click(object sender, EventArgs e)
MyButtonClick(sender);
Or
// custom code
string myClickMessage = MyButtonClick(button4);
you'll never disappoint me,that was a easy shot...thank you so much dmitry...again
– codroipo
Mar 28 at 7:40
only a little improovement th types of the buttons is void not string
– codroipo
Mar 28 at 7:41
@mad codroipo: sure, it should bevoid
:void button3_Click
andvoid button4_Click
– Dmitry Bychenko
Mar 28 at 7:48
the only remaining question is how could i pick up the result from another app and insert it into a db?
– codroipo
Mar 28 at 7:52
@mad codroipo: applications interaction (if "another app" is a different exe file) is a difficult procedure (say, both exe establish named pipe(s) and send data to it in order to interact). If "another app" is just another form of the same exe all you have to do is to modifyMyButtonClick
(e.g. write code to save the message into RDBMS)
– Dmitry Bychenko
Mar 28 at 7:56
|
show 3 more comments
Well, button3_Click
and alike are so-called callback functions which are called by system and that's why do not return any value (the system doesn't need it). Let's extract a method:
private string MyButtonClick(object sender)
string result = "";
if (sender == button3)
result = "PASS"; //TODO: better read it from resources, not hadcoded
else if (sender == button4)
result = "FAIL"; //TODO: better read it from resources, not hadcoded
if (!string.IsNullOrEmpty(result))
MessageBox.Show(result);
return result;
And then you can put:
// System callback function
// void: System callback function signature
private void button3_Click(object sender, EventArgs e)
MyButtonClick(sender);
// void: System callback function signature
private void button4_Click(object sender, EventArgs e)
MyButtonClick(sender);
Or
// custom code
string myClickMessage = MyButtonClick(button4);
Well, button3_Click
and alike are so-called callback functions which are called by system and that's why do not return any value (the system doesn't need it). Let's extract a method:
private string MyButtonClick(object sender)
string result = "";
if (sender == button3)
result = "PASS"; //TODO: better read it from resources, not hadcoded
else if (sender == button4)
result = "FAIL"; //TODO: better read it from resources, not hadcoded
if (!string.IsNullOrEmpty(result))
MessageBox.Show(result);
return result;
And then you can put:
// System callback function
// void: System callback function signature
private void button3_Click(object sender, EventArgs e)
MyButtonClick(sender);
// void: System callback function signature
private void button4_Click(object sender, EventArgs e)
MyButtonClick(sender);
Or
// custom code
string myClickMessage = MyButtonClick(button4);
answered Mar 28 at 7:34
Dmitry BychenkoDmitry Bychenko
122k15 gold badges115 silver badges150 bronze badges
122k15 gold badges115 silver badges150 bronze badges
you'll never disappoint me,that was a easy shot...thank you so much dmitry...again
– codroipo
Mar 28 at 7:40
only a little improovement th types of the buttons is void not string
– codroipo
Mar 28 at 7:41
@mad codroipo: sure, it should bevoid
:void button3_Click
andvoid button4_Click
– Dmitry Bychenko
Mar 28 at 7:48
the only remaining question is how could i pick up the result from another app and insert it into a db?
– codroipo
Mar 28 at 7:52
@mad codroipo: applications interaction (if "another app" is a different exe file) is a difficult procedure (say, both exe establish named pipe(s) and send data to it in order to interact). If "another app" is just another form of the same exe all you have to do is to modifyMyButtonClick
(e.g. write code to save the message into RDBMS)
– Dmitry Bychenko
Mar 28 at 7:56
|
show 3 more comments
you'll never disappoint me,that was a easy shot...thank you so much dmitry...again
– codroipo
Mar 28 at 7:40
only a little improovement th types of the buttons is void not string
– codroipo
Mar 28 at 7:41
@mad codroipo: sure, it should bevoid
:void button3_Click
andvoid button4_Click
– Dmitry Bychenko
Mar 28 at 7:48
the only remaining question is how could i pick up the result from another app and insert it into a db?
– codroipo
Mar 28 at 7:52
@mad codroipo: applications interaction (if "another app" is a different exe file) is a difficult procedure (say, both exe establish named pipe(s) and send data to it in order to interact). If "another app" is just another form of the same exe all you have to do is to modifyMyButtonClick
(e.g. write code to save the message into RDBMS)
– Dmitry Bychenko
Mar 28 at 7:56
you'll never disappoint me,that was a easy shot...thank you so much dmitry...again
– codroipo
Mar 28 at 7:40
you'll never disappoint me,that was a easy shot...thank you so much dmitry...again
– codroipo
Mar 28 at 7:40
only a little improovement th types of the buttons is void not string
– codroipo
Mar 28 at 7:41
only a little improovement th types of the buttons is void not string
– codroipo
Mar 28 at 7:41
@mad codroipo: sure, it should be
void
: void button3_Click
and void button4_Click
– Dmitry Bychenko
Mar 28 at 7:48
@mad codroipo: sure, it should be
void
: void button3_Click
and void button4_Click
– Dmitry Bychenko
Mar 28 at 7:48
the only remaining question is how could i pick up the result from another app and insert it into a db?
– codroipo
Mar 28 at 7:52
the only remaining question is how could i pick up the result from another app and insert it into a db?
– codroipo
Mar 28 at 7:52
@mad codroipo: applications interaction (if "another app" is a different exe file) is a difficult procedure (say, both exe establish named pipe(s) and send data to it in order to interact). If "another app" is just another form of the same exe all you have to do is to modify
MyButtonClick
(e.g. write code to save the message into RDBMS)– Dmitry Bychenko
Mar 28 at 7:56
@mad codroipo: applications interaction (if "another app" is a different exe file) is a difficult procedure (say, both exe establish named pipe(s) and send data to it in order to interact). If "another app" is just another form of the same exe all you have to do is to modify
MyButtonClick
(e.g. write code to save the message into RDBMS)– Dmitry Bychenko
Mar 28 at 7:56
|
show 3 more comments
Click event handlers have strongly typed signature, i.e. the return type must be void
, so you cannot return anything from them.
But, if you are working with windows Forms, you have class for your form, where you placed those event hadnlers. So what you can do is: define private variable:
pirvate string _res;
and then assign it value in your click event hadlers, eg.:
private string button3_Click(object sender, EventArgs e)
_res = "PASS";
MessageBox.Show(_res);
Then in the code you could use this value as you want.
If you want to use it outside your class, define property:
public string Res
get return _res;
add a comment |
Click event handlers have strongly typed signature, i.e. the return type must be void
, so you cannot return anything from them.
But, if you are working with windows Forms, you have class for your form, where you placed those event hadnlers. So what you can do is: define private variable:
pirvate string _res;
and then assign it value in your click event hadlers, eg.:
private string button3_Click(object sender, EventArgs e)
_res = "PASS";
MessageBox.Show(_res);
Then in the code you could use this value as you want.
If you want to use it outside your class, define property:
public string Res
get return _res;
add a comment |
Click event handlers have strongly typed signature, i.e. the return type must be void
, so you cannot return anything from them.
But, if you are working with windows Forms, you have class for your form, where you placed those event hadnlers. So what you can do is: define private variable:
pirvate string _res;
and then assign it value in your click event hadlers, eg.:
private string button3_Click(object sender, EventArgs e)
_res = "PASS";
MessageBox.Show(_res);
Then in the code you could use this value as you want.
If you want to use it outside your class, define property:
public string Res
get return _res;
Click event handlers have strongly typed signature, i.e. the return type must be void
, so you cannot return anything from them.
But, if you are working with windows Forms, you have class for your form, where you placed those event hadnlers. So what you can do is: define private variable:
pirvate string _res;
and then assign it value in your click event hadlers, eg.:
private string button3_Click(object sender, EventArgs e)
_res = "PASS";
MessageBox.Show(_res);
Then in the code you could use this value as you want.
If you want to use it outside your class, define property:
public string Res
get return _res;
answered Mar 28 at 7:35
Michał TurczynMichał Turczyn
20.4k13 gold badges24 silver badges42 bronze badges
20.4k13 gold badges24 silver badges42 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55392105%2fbutton-to-return-the-value-of-a-variable-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You are already doing it when you are passing
res
value toMessageBox.Show(res)
– iSahilSharma
Mar 28 at 7:24
You could write:
private void DisplayMessage(string message) MessageBox.Show(message);
and then call this method in both button click function like:DisplayMessage(res);
– Prashant Pimpale
Mar 28 at 7:24
Who is supposed to consume the value returned?
– Dmitry Bychenko
Mar 28 at 7:24
Possible duplicate of How do I get a return value from a Button Click event?
– SᴇM
Mar 28 at 7:25
oh hi again @DmitryBychenko,the returned value is going to be used by another app in wpf
– codroipo
Mar 28 at 7:27