Changing a label's text in another form in C#?
I have a label called LabelX1. This is on form2. On form1, i have a button. I want the button's text to be transferred to the other form's label. I have tried
form2 frm2 = new form2();
frm2.labelX1.Text = this.button1.text;
But it does not work. Is there an easy, straight forward way of doing this?
c# winforms
|
show 3 more comments
I have a label called LabelX1. This is on form2. On form1, i have a button. I want the button's text to be transferred to the other form's label. I have tried
form2 frm2 = new form2();
frm2.labelX1.Text = this.button1.text;
But it does not work. Is there an easy, straight forward way of doing this?
c# winforms
4
Why doesn't it work? What happens?
– SLaks
May 22 '12 at 14:25
2
Do you have the instance of form2 already displayed? In that way you create another instance of form2 and set the label text there. And that instance is neved displayed (eg. Show() / ShowDialog())
– Steve
May 22 '12 at 14:27
1
Does your code compile?
– Francesco Baruchelli
May 22 '12 at 14:30
1
Is that kind of joke question (puzzled by looking on your reputation)?
– Val Bakhtin
May 22 '12 at 14:34
10
Well, i am 15, and i am learning
– Hunter Mitchell
May 22 '12 at 14:44
|
show 3 more comments
I have a label called LabelX1. This is on form2. On form1, i have a button. I want the button's text to be transferred to the other form's label. I have tried
form2 frm2 = new form2();
frm2.labelX1.Text = this.button1.text;
But it does not work. Is there an easy, straight forward way of doing this?
c# winforms
I have a label called LabelX1. This is on form2. On form1, i have a button. I want the button's text to be transferred to the other form's label. I have tried
form2 frm2 = new form2();
frm2.labelX1.Text = this.button1.text;
But it does not work. Is there an easy, straight forward way of doing this?
c# winforms
c# winforms
edited May 22 '12 at 14:25
SLaks
683k13916361756
683k13916361756
asked May 22 '12 at 14:23
Hunter MitchellHunter Mitchell
2,8121448102
2,8121448102
4
Why doesn't it work? What happens?
– SLaks
May 22 '12 at 14:25
2
Do you have the instance of form2 already displayed? In that way you create another instance of form2 and set the label text there. And that instance is neved displayed (eg. Show() / ShowDialog())
– Steve
May 22 '12 at 14:27
1
Does your code compile?
– Francesco Baruchelli
May 22 '12 at 14:30
1
Is that kind of joke question (puzzled by looking on your reputation)?
– Val Bakhtin
May 22 '12 at 14:34
10
Well, i am 15, and i am learning
– Hunter Mitchell
May 22 '12 at 14:44
|
show 3 more comments
4
Why doesn't it work? What happens?
– SLaks
May 22 '12 at 14:25
2
Do you have the instance of form2 already displayed? In that way you create another instance of form2 and set the label text there. And that instance is neved displayed (eg. Show() / ShowDialog())
– Steve
May 22 '12 at 14:27
1
Does your code compile?
– Francesco Baruchelli
May 22 '12 at 14:30
1
Is that kind of joke question (puzzled by looking on your reputation)?
– Val Bakhtin
May 22 '12 at 14:34
10
Well, i am 15, and i am learning
– Hunter Mitchell
May 22 '12 at 14:44
4
4
Why doesn't it work? What happens?
– SLaks
May 22 '12 at 14:25
Why doesn't it work? What happens?
– SLaks
May 22 '12 at 14:25
2
2
Do you have the instance of form2 already displayed? In that way you create another instance of form2 and set the label text there. And that instance is neved displayed (eg. Show() / ShowDialog())
– Steve
May 22 '12 at 14:27
Do you have the instance of form2 already displayed? In that way you create another instance of form2 and set the label text there. And that instance is neved displayed (eg. Show() / ShowDialog())
– Steve
May 22 '12 at 14:27
1
1
Does your code compile?
– Francesco Baruchelli
May 22 '12 at 14:30
Does your code compile?
– Francesco Baruchelli
May 22 '12 at 14:30
1
1
Is that kind of joke question (puzzled by looking on your reputation)?
– Val Bakhtin
May 22 '12 at 14:34
Is that kind of joke question (puzzled by looking on your reputation)?
– Val Bakhtin
May 22 '12 at 14:34
10
10
Well, i am 15, and i am learning
– Hunter Mitchell
May 22 '12 at 14:44
Well, i am 15, and i am learning
– Hunter Mitchell
May 22 '12 at 14:44
|
show 3 more comments
12 Answers
12
active
oldest
votes
You need to expose your label or its property.
In form 2:
public string LabelText
{
get
{
return this.labelX1.Text;
}
set
{
this.labelX1.Text = value;
}
}
Then you can do:
form2 frm2 = new form2();
frm2.LabelText = this.button1.text;
add a comment |
You could modify the constructor of Form2 like this:
public Form2(string labelText)
{
InitializeComponent();
this.labelX1.Text = labelText;
}
then create Form2 passing in the text:
Form2 frm2 = new Form2(this.button1.text);
I agree this will work, but it would be a one time change. And maybe that is good enough for the OP.
– General Grey
May 22 '12 at 14:35
@K'Leg well, they didn't indicate whether or not they need to change it again...I'm just providing options.
– Eric Dahlvang
May 22 '12 at 14:37
I agree if this is all he wants then I think this is the best answer. It is easier then creating a property for the text label.
– General Grey
May 22 '12 at 14:39
add a comment |
inside form2 write this
public void ChangeLabel(string s)
{
labelX1.Text = s;
}
then where you create Form 2 do this
form2 frm2 = new form2();
frm2.ChangeLabel(this.button1.text);
add a comment |
Or you can do this >>
((Label)frm2.Controls["labelX1"]).Text = "test";
add a comment |
You can me labelX1 public and it will work but there is a better way to do this
http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms
add a comment |
Is there an easy, straight forward way of doing this?
Easiest way is to make labelX1 a public member of form2. The issue you're having is because from Form1 code form2.labelX1 isn't visible. In form2 designer you can go to properties of labelX1 and set it's visibility to public/internal.
Better approach would be to expose labelX1.Text as a property which can be set in code outside the class.
1
your answer is still not correct Visibility to protected will not help anything
– Micah Armantrout
May 22 '12 at 14:27
yea i accidentally wrote protected, i really meant public... i was editing while you commented!
– hawk
May 22 '12 at 14:28
1
Or at least internal
– Francesco Baruchelli
May 22 '12 at 14:29
add a comment |
the only think you have to do is to put the label of the other form as public
for instance:
Form1:
public System.Windows.Forms.Label txtInfo;
then in Form2
Form1 frm =new Form1();
frm.txtInfo.text="....."//you have access because is public
add a comment |
I changed my parent window property to the following code:
this.MdiParent.Controls["label1"].Text = "test";
add a comment |
form2 frm2 = new form2();
((Label)frm2.Controls["labelX1"]).Text=button1.Text;
frm2.Show();
add a comment |
If you are needing to access the form2 from elsewhere in your code (like a button press for instance) you will not be able to see the instance of the form you create. To solve that I create a public instance to hold a reference to it like:
public form2 form2_pub;
Then after you create it you assign the new one to your public instance:
form2 frm2 = new form2();
frm2.Show();
form2_pub = frm2
Now you can reference form2_pub throughout your routines.
Works for me at least.
Remember, in your setter you can run whatever other code you want.
For instance, I use the following to show what I want on another form by just setting show_scanning to true:
public bool show_scanning //turns on the scanning screen
{
set
{
scanning_pnl.Visible = true;
notReady_pnl.Visible = false;
timer1.Enabled = true;
}
}
add a comment |
Generally the controllers are private. That is why you unable to access it from another form. Above mentioned ways such as passing data through parameters etc are so correct. There is one another method,
Go to your form Form2.Designer.cs
private System.Windows.Forms.Label labelX1;
Change 'private' into 'public'.
Now the labelX1 is visible to outside.
add a comment |
Do you have exception? You can make public property on form2, with setter to set text on the label, or make labex1 access modifier public and set it directly. It should work.
Show him with an example. The error here is clear...
– MoonKnight
May 22 '12 at 14:28
@Killercam error is clear for who? He did not post any error information.
– Val Bakhtin
May 22 '12 at 14:30
If you do not have enough information to answer comment instead - asking for the details. What you have supplied it not an answer.
– MoonKnight
May 22 '12 at 14:37
For some it is. You set access modifier from a prop box, not a code. But nvm.
– Val Bakhtin
May 22 '12 at 14:39
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/3.0/"u003ecc by-sa 3.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%2f10704020%2fchanging-a-labels-text-in-another-form-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
12 Answers
12
active
oldest
votes
12 Answers
12
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to expose your label or its property.
In form 2:
public string LabelText
{
get
{
return this.labelX1.Text;
}
set
{
this.labelX1.Text = value;
}
}
Then you can do:
form2 frm2 = new form2();
frm2.LabelText = this.button1.text;
add a comment |
You need to expose your label or its property.
In form 2:
public string LabelText
{
get
{
return this.labelX1.Text;
}
set
{
this.labelX1.Text = value;
}
}
Then you can do:
form2 frm2 = new form2();
frm2.LabelText = this.button1.text;
add a comment |
You need to expose your label or its property.
In form 2:
public string LabelText
{
get
{
return this.labelX1.Text;
}
set
{
this.labelX1.Text = value;
}
}
Then you can do:
form2 frm2 = new form2();
frm2.LabelText = this.button1.text;
You need to expose your label or its property.
In form 2:
public string LabelText
{
get
{
return this.labelX1.Text;
}
set
{
this.labelX1.Text = value;
}
}
Then you can do:
form2 frm2 = new form2();
frm2.LabelText = this.button1.text;
answered May 22 '12 at 14:26
DavioDavio
3,11311644
3,11311644
add a comment |
add a comment |
You could modify the constructor of Form2 like this:
public Form2(string labelText)
{
InitializeComponent();
this.labelX1.Text = labelText;
}
then create Form2 passing in the text:
Form2 frm2 = new Form2(this.button1.text);
I agree this will work, but it would be a one time change. And maybe that is good enough for the OP.
– General Grey
May 22 '12 at 14:35
@K'Leg well, they didn't indicate whether or not they need to change it again...I'm just providing options.
– Eric Dahlvang
May 22 '12 at 14:37
I agree if this is all he wants then I think this is the best answer. It is easier then creating a property for the text label.
– General Grey
May 22 '12 at 14:39
add a comment |
You could modify the constructor of Form2 like this:
public Form2(string labelText)
{
InitializeComponent();
this.labelX1.Text = labelText;
}
then create Form2 passing in the text:
Form2 frm2 = new Form2(this.button1.text);
I agree this will work, but it would be a one time change. And maybe that is good enough for the OP.
– General Grey
May 22 '12 at 14:35
@K'Leg well, they didn't indicate whether or not they need to change it again...I'm just providing options.
– Eric Dahlvang
May 22 '12 at 14:37
I agree if this is all he wants then I think this is the best answer. It is easier then creating a property for the text label.
– General Grey
May 22 '12 at 14:39
add a comment |
You could modify the constructor of Form2 like this:
public Form2(string labelText)
{
InitializeComponent();
this.labelX1.Text = labelText;
}
then create Form2 passing in the text:
Form2 frm2 = new Form2(this.button1.text);
You could modify the constructor of Form2 like this:
public Form2(string labelText)
{
InitializeComponent();
this.labelX1.Text = labelText;
}
then create Form2 passing in the text:
Form2 frm2 = new Form2(this.button1.text);
answered May 22 '12 at 14:31
Eric DahlvangEric Dahlvang
6,41342142
6,41342142
I agree this will work, but it would be a one time change. And maybe that is good enough for the OP.
– General Grey
May 22 '12 at 14:35
@K'Leg well, they didn't indicate whether or not they need to change it again...I'm just providing options.
– Eric Dahlvang
May 22 '12 at 14:37
I agree if this is all he wants then I think this is the best answer. It is easier then creating a property for the text label.
– General Grey
May 22 '12 at 14:39
add a comment |
I agree this will work, but it would be a one time change. And maybe that is good enough for the OP.
– General Grey
May 22 '12 at 14:35
@K'Leg well, they didn't indicate whether or not they need to change it again...I'm just providing options.
– Eric Dahlvang
May 22 '12 at 14:37
I agree if this is all he wants then I think this is the best answer. It is easier then creating a property for the text label.
– General Grey
May 22 '12 at 14:39
I agree this will work, but it would be a one time change. And maybe that is good enough for the OP.
– General Grey
May 22 '12 at 14:35
I agree this will work, but it would be a one time change. And maybe that is good enough for the OP.
– General Grey
May 22 '12 at 14:35
@K'Leg well, they didn't indicate whether or not they need to change it again...I'm just providing options.
– Eric Dahlvang
May 22 '12 at 14:37
@K'Leg well, they didn't indicate whether or not they need to change it again...I'm just providing options.
– Eric Dahlvang
May 22 '12 at 14:37
I agree if this is all he wants then I think this is the best answer. It is easier then creating a property for the text label.
– General Grey
May 22 '12 at 14:39
I agree if this is all he wants then I think this is the best answer. It is easier then creating a property for the text label.
– General Grey
May 22 '12 at 14:39
add a comment |
inside form2 write this
public void ChangeLabel(string s)
{
labelX1.Text = s;
}
then where you create Form 2 do this
form2 frm2 = new form2();
frm2.ChangeLabel(this.button1.text);
add a comment |
inside form2 write this
public void ChangeLabel(string s)
{
labelX1.Text = s;
}
then where you create Form 2 do this
form2 frm2 = new form2();
frm2.ChangeLabel(this.button1.text);
add a comment |
inside form2 write this
public void ChangeLabel(string s)
{
labelX1.Text = s;
}
then where you create Form 2 do this
form2 frm2 = new form2();
frm2.ChangeLabel(this.button1.text);
inside form2 write this
public void ChangeLabel(string s)
{
labelX1.Text = s;
}
then where you create Form 2 do this
form2 frm2 = new form2();
frm2.ChangeLabel(this.button1.text);
answered May 22 '12 at 14:35
General GreyGeneral Grey
2,54521831
2,54521831
add a comment |
add a comment |
Or you can do this >>
((Label)frm2.Controls["labelX1"]).Text = "test";
add a comment |
Or you can do this >>
((Label)frm2.Controls["labelX1"]).Text = "test";
add a comment |
Or you can do this >>
((Label)frm2.Controls["labelX1"]).Text = "test";
Or you can do this >>
((Label)frm2.Controls["labelX1"]).Text = "test";
answered May 22 '12 at 14:36
bangingbanging
2,1691524
2,1691524
add a comment |
add a comment |
You can me labelX1 public and it will work but there is a better way to do this
http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms
add a comment |
You can me labelX1 public and it will work but there is a better way to do this
http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms
add a comment |
You can me labelX1 public and it will work but there is a better way to do this
http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms
You can me labelX1 public and it will work but there is a better way to do this
http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms
answered May 22 '12 at 14:29
Micah ArmantroutMicah Armantrout
4,87632552
4,87632552
add a comment |
add a comment |
Is there an easy, straight forward way of doing this?
Easiest way is to make labelX1 a public member of form2. The issue you're having is because from Form1 code form2.labelX1 isn't visible. In form2 designer you can go to properties of labelX1 and set it's visibility to public/internal.
Better approach would be to expose labelX1.Text as a property which can be set in code outside the class.
1
your answer is still not correct Visibility to protected will not help anything
– Micah Armantrout
May 22 '12 at 14:27
yea i accidentally wrote protected, i really meant public... i was editing while you commented!
– hawk
May 22 '12 at 14:28
1
Or at least internal
– Francesco Baruchelli
May 22 '12 at 14:29
add a comment |
Is there an easy, straight forward way of doing this?
Easiest way is to make labelX1 a public member of form2. The issue you're having is because from Form1 code form2.labelX1 isn't visible. In form2 designer you can go to properties of labelX1 and set it's visibility to public/internal.
Better approach would be to expose labelX1.Text as a property which can be set in code outside the class.
1
your answer is still not correct Visibility to protected will not help anything
– Micah Armantrout
May 22 '12 at 14:27
yea i accidentally wrote protected, i really meant public... i was editing while you commented!
– hawk
May 22 '12 at 14:28
1
Or at least internal
– Francesco Baruchelli
May 22 '12 at 14:29
add a comment |
Is there an easy, straight forward way of doing this?
Easiest way is to make labelX1 a public member of form2. The issue you're having is because from Form1 code form2.labelX1 isn't visible. In form2 designer you can go to properties of labelX1 and set it's visibility to public/internal.
Better approach would be to expose labelX1.Text as a property which can be set in code outside the class.
Is there an easy, straight forward way of doing this?
Easiest way is to make labelX1 a public member of form2. The issue you're having is because from Form1 code form2.labelX1 isn't visible. In form2 designer you can go to properties of labelX1 and set it's visibility to public/internal.
Better approach would be to expose labelX1.Text as a property which can be set in code outside the class.
edited May 22 '12 at 14:31
answered May 22 '12 at 14:25
hawkhawk
1,3931925
1,3931925
1
your answer is still not correct Visibility to protected will not help anything
– Micah Armantrout
May 22 '12 at 14:27
yea i accidentally wrote protected, i really meant public... i was editing while you commented!
– hawk
May 22 '12 at 14:28
1
Or at least internal
– Francesco Baruchelli
May 22 '12 at 14:29
add a comment |
1
your answer is still not correct Visibility to protected will not help anything
– Micah Armantrout
May 22 '12 at 14:27
yea i accidentally wrote protected, i really meant public... i was editing while you commented!
– hawk
May 22 '12 at 14:28
1
Or at least internal
– Francesco Baruchelli
May 22 '12 at 14:29
1
1
your answer is still not correct Visibility to protected will not help anything
– Micah Armantrout
May 22 '12 at 14:27
your answer is still not correct Visibility to protected will not help anything
– Micah Armantrout
May 22 '12 at 14:27
yea i accidentally wrote protected, i really meant public... i was editing while you commented!
– hawk
May 22 '12 at 14:28
yea i accidentally wrote protected, i really meant public... i was editing while you commented!
– hawk
May 22 '12 at 14:28
1
1
Or at least internal
– Francesco Baruchelli
May 22 '12 at 14:29
Or at least internal
– Francesco Baruchelli
May 22 '12 at 14:29
add a comment |
the only think you have to do is to put the label of the other form as public
for instance:
Form1:
public System.Windows.Forms.Label txtInfo;
then in Form2
Form1 frm =new Form1();
frm.txtInfo.text="....."//you have access because is public
add a comment |
the only think you have to do is to put the label of the other form as public
for instance:
Form1:
public System.Windows.Forms.Label txtInfo;
then in Form2
Form1 frm =new Form1();
frm.txtInfo.text="....."//you have access because is public
add a comment |
the only think you have to do is to put the label of the other form as public
for instance:
Form1:
public System.Windows.Forms.Label txtInfo;
then in Form2
Form1 frm =new Form1();
frm.txtInfo.text="....."//you have access because is public
the only think you have to do is to put the label of the other form as public
for instance:
Form1:
public System.Windows.Forms.Label txtInfo;
then in Form2
Form1 frm =new Form1();
frm.txtInfo.text="....."//you have access because is public
answered Oct 31 '18 at 21:52
john pispidikisjohn pispidikis
111
111
add a comment |
add a comment |
I changed my parent window property to the following code:
this.MdiParent.Controls["label1"].Text = "test";
add a comment |
I changed my parent window property to the following code:
this.MdiParent.Controls["label1"].Text = "test";
add a comment |
I changed my parent window property to the following code:
this.MdiParent.Controls["label1"].Text = "test";
I changed my parent window property to the following code:
this.MdiParent.Controls["label1"].Text = "test";
edited Oct 21 '15 at 14:18
maxshuty
1,83271838
1,83271838
answered Oct 21 '15 at 13:28
Amit GohelAmit Gohel
1
1
add a comment |
add a comment |
form2 frm2 = new form2();
((Label)frm2.Controls["labelX1"]).Text=button1.Text;
frm2.Show();
add a comment |
form2 frm2 = new form2();
((Label)frm2.Controls["labelX1"]).Text=button1.Text;
frm2.Show();
add a comment |
form2 frm2 = new form2();
((Label)frm2.Controls["labelX1"]).Text=button1.Text;
frm2.Show();
form2 frm2 = new form2();
((Label)frm2.Controls["labelX1"]).Text=button1.Text;
frm2.Show();
answered Nov 10 '15 at 20:54
Hstm91Hstm91
11
11
add a comment |
add a comment |
If you are needing to access the form2 from elsewhere in your code (like a button press for instance) you will not be able to see the instance of the form you create. To solve that I create a public instance to hold a reference to it like:
public form2 form2_pub;
Then after you create it you assign the new one to your public instance:
form2 frm2 = new form2();
frm2.Show();
form2_pub = frm2
Now you can reference form2_pub throughout your routines.
Works for me at least.
Remember, in your setter you can run whatever other code you want.
For instance, I use the following to show what I want on another form by just setting show_scanning to true:
public bool show_scanning //turns on the scanning screen
{
set
{
scanning_pnl.Visible = true;
notReady_pnl.Visible = false;
timer1.Enabled = true;
}
}
add a comment |
If you are needing to access the form2 from elsewhere in your code (like a button press for instance) you will not be able to see the instance of the form you create. To solve that I create a public instance to hold a reference to it like:
public form2 form2_pub;
Then after you create it you assign the new one to your public instance:
form2 frm2 = new form2();
frm2.Show();
form2_pub = frm2
Now you can reference form2_pub throughout your routines.
Works for me at least.
Remember, in your setter you can run whatever other code you want.
For instance, I use the following to show what I want on another form by just setting show_scanning to true:
public bool show_scanning //turns on the scanning screen
{
set
{
scanning_pnl.Visible = true;
notReady_pnl.Visible = false;
timer1.Enabled = true;
}
}
add a comment |
If you are needing to access the form2 from elsewhere in your code (like a button press for instance) you will not be able to see the instance of the form you create. To solve that I create a public instance to hold a reference to it like:
public form2 form2_pub;
Then after you create it you assign the new one to your public instance:
form2 frm2 = new form2();
frm2.Show();
form2_pub = frm2
Now you can reference form2_pub throughout your routines.
Works for me at least.
Remember, in your setter you can run whatever other code you want.
For instance, I use the following to show what I want on another form by just setting show_scanning to true:
public bool show_scanning //turns on the scanning screen
{
set
{
scanning_pnl.Visible = true;
notReady_pnl.Visible = false;
timer1.Enabled = true;
}
}
If you are needing to access the form2 from elsewhere in your code (like a button press for instance) you will not be able to see the instance of the form you create. To solve that I create a public instance to hold a reference to it like:
public form2 form2_pub;
Then after you create it you assign the new one to your public instance:
form2 frm2 = new form2();
frm2.Show();
form2_pub = frm2
Now you can reference form2_pub throughout your routines.
Works for me at least.
Remember, in your setter you can run whatever other code you want.
For instance, I use the following to show what I want on another form by just setting show_scanning to true:
public bool show_scanning //turns on the scanning screen
{
set
{
scanning_pnl.Visible = true;
notReady_pnl.Visible = false;
timer1.Enabled = true;
}
}
answered Nov 1 '17 at 17:10
Joe RuderJoe Ruder
8711927
8711927
add a comment |
add a comment |
Generally the controllers are private. That is why you unable to access it from another form. Above mentioned ways such as passing data through parameters etc are so correct. There is one another method,
Go to your form Form2.Designer.cs
private System.Windows.Forms.Label labelX1;
Change 'private' into 'public'.
Now the labelX1 is visible to outside.
add a comment |
Generally the controllers are private. That is why you unable to access it from another form. Above mentioned ways such as passing data through parameters etc are so correct. There is one another method,
Go to your form Form2.Designer.cs
private System.Windows.Forms.Label labelX1;
Change 'private' into 'public'.
Now the labelX1 is visible to outside.
add a comment |
Generally the controllers are private. That is why you unable to access it from another form. Above mentioned ways such as passing data through parameters etc are so correct. There is one another method,
Go to your form Form2.Designer.cs
private System.Windows.Forms.Label labelX1;
Change 'private' into 'public'.
Now the labelX1 is visible to outside.
Generally the controllers are private. That is why you unable to access it from another form. Above mentioned ways such as passing data through parameters etc are so correct. There is one another method,
Go to your form Form2.Designer.cs
private System.Windows.Forms.Label labelX1;
Change 'private' into 'public'.
Now the labelX1 is visible to outside.
answered Nov 21 '18 at 4:18
Yuresh KarunanayakeYuresh Karunanayake
1006
1006
add a comment |
add a comment |
Do you have exception? You can make public property on form2, with setter to set text on the label, or make labex1 access modifier public and set it directly. It should work.
Show him with an example. The error here is clear...
– MoonKnight
May 22 '12 at 14:28
@Killercam error is clear for who? He did not post any error information.
– Val Bakhtin
May 22 '12 at 14:30
If you do not have enough information to answer comment instead - asking for the details. What you have supplied it not an answer.
– MoonKnight
May 22 '12 at 14:37
For some it is. You set access modifier from a prop box, not a code. But nvm.
– Val Bakhtin
May 22 '12 at 14:39
add a comment |
Do you have exception? You can make public property on form2, with setter to set text on the label, or make labex1 access modifier public and set it directly. It should work.
Show him with an example. The error here is clear...
– MoonKnight
May 22 '12 at 14:28
@Killercam error is clear for who? He did not post any error information.
– Val Bakhtin
May 22 '12 at 14:30
If you do not have enough information to answer comment instead - asking for the details. What you have supplied it not an answer.
– MoonKnight
May 22 '12 at 14:37
For some it is. You set access modifier from a prop box, not a code. But nvm.
– Val Bakhtin
May 22 '12 at 14:39
add a comment |
Do you have exception? You can make public property on form2, with setter to set text on the label, or make labex1 access modifier public and set it directly. It should work.
Do you have exception? You can make public property on form2, with setter to set text on the label, or make labex1 access modifier public and set it directly. It should work.
answered May 22 '12 at 14:26
Val BakhtinVal Bakhtin
1,264810
1,264810
Show him with an example. The error here is clear...
– MoonKnight
May 22 '12 at 14:28
@Killercam error is clear for who? He did not post any error information.
– Val Bakhtin
May 22 '12 at 14:30
If you do not have enough information to answer comment instead - asking for the details. What you have supplied it not an answer.
– MoonKnight
May 22 '12 at 14:37
For some it is. You set access modifier from a prop box, not a code. But nvm.
– Val Bakhtin
May 22 '12 at 14:39
add a comment |
Show him with an example. The error here is clear...
– MoonKnight
May 22 '12 at 14:28
@Killercam error is clear for who? He did not post any error information.
– Val Bakhtin
May 22 '12 at 14:30
If you do not have enough information to answer comment instead - asking for the details. What you have supplied it not an answer.
– MoonKnight
May 22 '12 at 14:37
For some it is. You set access modifier from a prop box, not a code. But nvm.
– Val Bakhtin
May 22 '12 at 14:39
Show him with an example. The error here is clear...
– MoonKnight
May 22 '12 at 14:28
Show him with an example. The error here is clear...
– MoonKnight
May 22 '12 at 14:28
@Killercam error is clear for who? He did not post any error information.
– Val Bakhtin
May 22 '12 at 14:30
@Killercam error is clear for who? He did not post any error information.
– Val Bakhtin
May 22 '12 at 14:30
If you do not have enough information to answer comment instead - asking for the details. What you have supplied it not an answer.
– MoonKnight
May 22 '12 at 14:37
If you do not have enough information to answer comment instead - asking for the details. What you have supplied it not an answer.
– MoonKnight
May 22 '12 at 14:37
For some it is. You set access modifier from a prop box, not a code. But nvm.
– Val Bakhtin
May 22 '12 at 14:39
For some it is. You set access modifier from a prop box, not a code. But nvm.
– Val Bakhtin
May 22 '12 at 14:39
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%2f10704020%2fchanging-a-labels-text-in-another-form-in-c%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
4
Why doesn't it work? What happens?
– SLaks
May 22 '12 at 14:25
2
Do you have the instance of form2 already displayed? In that way you create another instance of form2 and set the label text there. And that instance is neved displayed (eg. Show() / ShowDialog())
– Steve
May 22 '12 at 14:27
1
Does your code compile?
– Francesco Baruchelli
May 22 '12 at 14:30
1
Is that kind of joke question (puzzled by looking on your reputation)?
– Val Bakhtin
May 22 '12 at 14:34
10
Well, i am 15, and i am learning
– Hunter Mitchell
May 22 '12 at 14:44