Answered by:
Cannot resolve an error message

Question
-
Hi
I am a beginner trying to get to grips with C# coding. I have created a label in a form with name = NumberLabel with no Text initially.
I have also added a button (with Text = 1) to the same form with the idea that when the button is clicked the number 1 is assigned to the label and should appear in the label field. Code is as follows:
private void button1_Click(object sender, EventArgs e)
{
NumberLabel.Text += "1";
}I keep getting the following error message which I cannot resolve.
'System.Windows.Forms.Label' does not contain a definition for 'Label' and no extension method 'Label' accepting a first argument of type 'System.Windows.Forms.Label' could be found (are you missing a using directive or an assembley reference?)
My using declarations are as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;Any assistance is greatly appreciated. Thanks.
Tuesday, June 21, 2016 10:29 PM
Answers
-
I am saying that is what the error message is saying.
'System.Windows.Forms.Label' does not contain a definition for 'Label'
The code that you posted clearly does not have such an error, so I was suggesting that perhaps the error is somewhere else in your code (sorry for not making that clear).
- Proposed as answer by DotNet Wang Tuesday, June 28, 2016 10:09 AM
- Marked as answer by DotNet Wang Thursday, June 30, 2016 2:00 AM
Wednesday, June 22, 2016 1:34 AM -
;NumberLabel.Text += "1"; I am adding 1 to what is existing in the text property. I hope my understanding is correct.
https://msdn.microsoft.com/en-us/library/ms228504.aspx?f=255&MSPPError=-2147217396
NumberLabel.Text = NumberLabel.Text + "1" is string concatenation. Whatever was in NumberLabel.Text like 'veritias18' then it would be 'veritias181' .
If you want to add a value to an existing value in the Text property if = "1" in the Text property then it this.
int holdnumber = Convert.ToInt32(NumberLabel.Text) + 1; // it has to be a string number value in the Text property
NumberLabel.Text = holdnumber.Tostring();
https://msdn.microsoft.com/en-us/library/f4a76a1x(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/bb397679.aspx
http://www.dotnetperls.com/cast
int somenumbervalue = (int32)NumberLabel.Text + 1;
NumberLabel.Text = somenumbervalue .Tostring()
int somenumber = 1;
somenumber =+ 1; // increment somenumber by 1 a true number value variable.
- Proposed as answer by DotNet Wang Tuesday, June 28, 2016 10:09 AM
- Marked as answer by DotNet Wang Thursday, June 30, 2016 2:00 AM
Wednesday, June 22, 2016 1:52 AM
All replies
-
http://www.tutorialspoint.com/csharp/csharp_operators.htm
<copied>
= Simple assignment operator, Assigns values from right side operands to left side operand
+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand
<end>
So what is the statement about?
NumberLabel.Text += "1";
Maybe it should just be this below. Maybe what you are doing above has confused the complier.
NumberLabel.Text = "1";
You are dealing with the Text property of the control, and it is a string type property. And all you are trying to do is set the Text property of the control to a value of "1" like the statement above.
It seems to me that you need a C# book or reference guide, possibly.
Tuesday, June 21, 2016 11:13 PM -
I can't see anything wrong with that code snippet that would cause that error. DA924x is right that the += operator probably isn't the one that you meant to use, but it still should have compiled.
The error is telling you that you are trying to use a "Label" property of a Label, of which there is no such thing. Perhaps something like...
NumberLabel.Label = "x";
Tuesday, June 21, 2016 11:28 PM -
Actually, there's a little more to it than that. I am building a calculator. I use the label called NumberLabel to display the numbers entered by the user (via buttons). So string concatenation is really what I am after. As sequential numbers are entered they should be displayed next to the existing numbers in the label text property. I understand that NumberLabel.Text is a string field.
Ante Meridian - why do you say I am trying to use the label property of a label (NumberLabel.Label) when I am using working with NumberLabel.Text property? Lost you on that one!
DA924x - I appreciate your comments, but if I used
NumberLabel.Text = "1"; Then to my understanding I would have only the number 1 in the
property whilst with
NumberLabel.Text += "1"; I am adding 1 to what is existing in the text property. I hope my understanding is correct.
Thanks again.
Wednesday, June 22, 2016 12:49 AM -
I am saying that is what the error message is saying.
'System.Windows.Forms.Label' does not contain a definition for 'Label'
The code that you posted clearly does not have such an error, so I was suggesting that perhaps the error is somewhere else in your code (sorry for not making that clear).
- Proposed as answer by DotNet Wang Tuesday, June 28, 2016 10:09 AM
- Marked as answer by DotNet Wang Thursday, June 30, 2016 2:00 AM
Wednesday, June 22, 2016 1:34 AM -
;NumberLabel.Text += "1"; I am adding 1 to what is existing in the text property. I hope my understanding is correct.
https://msdn.microsoft.com/en-us/library/ms228504.aspx?f=255&MSPPError=-2147217396
NumberLabel.Text = NumberLabel.Text + "1" is string concatenation. Whatever was in NumberLabel.Text like 'veritias18' then it would be 'veritias181' .
If you want to add a value to an existing value in the Text property if = "1" in the Text property then it this.
int holdnumber = Convert.ToInt32(NumberLabel.Text) + 1; // it has to be a string number value in the Text property
NumberLabel.Text = holdnumber.Tostring();
https://msdn.microsoft.com/en-us/library/f4a76a1x(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/bb397679.aspx
http://www.dotnetperls.com/cast
int somenumbervalue = (int32)NumberLabel.Text + 1;
NumberLabel.Text = somenumbervalue .Tostring()
int somenumber = 1;
somenumber =+ 1; // increment somenumber by 1 a true number value variable.
- Proposed as answer by DotNet Wang Tuesday, June 28, 2016 10:09 AM
- Marked as answer by DotNet Wang Thursday, June 30, 2016 2:00 AM
Wednesday, June 22, 2016 1:52 AM -
DA924x, thanks for the very comprehensive answer. I will need to carefully wade through it. Much appreciated.
Ante Meridian - thanks for making that clear.
Regards.
Wednesday, June 22, 2016 7:40 AM -
veritas:
If you are trying to simply add a character to the existing label string then
NumberLabel.Text = NumberLabel.Text + " ";
place "string to add" within parenthesis.
If NumberLabel.Text = 2
then NumberLabel.Text = NumberLabel.Text + "7";
would give you 27.
If you want 2,7 then NumberLabel.Text = NumberLabel.Text + ", " + "7";
or NumberLabel.Text = NumberLabel.Text + ", 7 ";
don't forget the space between comma and 7.
Is this your intent
Hope it helps
tac
Friday, June 24, 2016 4:43 PM -
Hello Troy22222
Yes, you are right, exactly what I was looking for - and it works!Actually, it is no different than what I originally had. When I restarted everything a few days ago (rebooted pc) the error messages were gone! And I did not change a single bit of code. Go figure!
Thanks anyway for the assistance. It confirms what I have done.
Regards.
Saturday, July 2, 2016 8:34 PM -
So my answer did suffice and yet these yahoos claimed credit for themselves, whatever.
tac.
By the way I've also resolved errors by the reboot sequence. No there's no issues with Microsoft software../..
tac
Tuesday, July 12, 2016 8:34 PM -
So my answer did suffice and yet these yahoos claimed credit for themselves, whatever.
tac.
By the way I've also resolved errors by the reboot sequence. No there's no issues with Microsoft software../..
tac
What kind of a clown are you? No one you clown claimed any credit.Tuesday, July 12, 2016 8:57 PM