Click here to Skip to main content
15,881,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a very specific situation, so please, don't question the approach, but snippets of other approaches are welcome for educational purposes.

I have a for loop and label.
For loop performs calculations and assign the result to the label.
Then, the loop runs again, and I need to reassign the value to the same label only if the new result is higher than the one that is currently "stored" in the label.

To avoid creating extra variables that will hold the previous value, I think I can extract the numbers from the label by removing text (there is "Result = " + result), and convert them to int, and then compare to the new score.

Here is what I got so far, but it throws a "not correct format" error:

C#
var labVal = Int32.Parse(Regex.Match(label1.Text, @"\d+").Value);

if (result >= labVal)
{
label1.Text = result
}
else
{
label1.Text = labVal
}


What I have tried:

Everything. The best I managed to make is a code above
Posted
Updated 4-Jan-22 2:18am
v2

Actually that code is gong to produce something like "; expected" as it won't even compile. When I fix that problem your code appears to work ... unless of course the label text is completely blank.
You either need to start with the label containing the text "Result = 0" OR put a condition around the calculation of labVal, setting it to 0 if the condition fails e.g.
C#
var labVal = label1.Text.Length > 0 ? Int32.Parse(Regex.Match(test, @"\d+").Value) : 0;


Edit:
Snippets of other approaches you could use
- This will get rid of the Result = from the label
C#
label1.Text.Replace("Result = ", "")

- You could split the string e.g.
C#
var res = label1.Text.Split(' ','=');
Console.WriteLine("{0}",res[res.Length-1]);


Finally, a couple of other comments
- you don't need the else part of your if-statement as you are just leaving the label text as is.
- You haven't included the "Result = " text when you are assigning to the label text
- It is bad practice to assign numeric values to text - use the .ToString() method or Format the string e.g.
C#
label1.Text = string.Format("Result = {0}",result);
 
Share this answer
 
v2
As an improvement on Chill60's solution, use the string Whitespace checker:
C#
int labVal = string.IsNullOrWhiteSpace(label1.Text) ? 0 : Int32.Parse(Regex.Match(label1.Text, @"\d+").Value);

But a better solution if you are using a loop is to keep the previous value of labVal instead of recalculating each time, and set the label value at the end - since the displayed value will not be updated anyway until the method exits.
 
Share this answer
 
Comments
John Bon Jovy 5-Jan-22 20:57pm    
It throws:
System.FormatException: 'Input string was not in a correct format.'
Store the numeric value in the Tag Property of the Label and define Extension Get and Set Methods for the Label.

C#
public static void
SetLabelValue
(
  this System.Windows.Forms.Label Label
,
  string                          Format
,
  object                          Value
)
{
  Label.Text = System.String.Format
  (
    Format
  ,
    Value
  ) ;

  Label.Tag = Value ;

  return ;
}

public static T
GetLabelValue<T>
(
  this System.Windows.Forms.Label Label
)
{
  return ( (T) Label.Tag ) ;
}


Or derive your own Label which does what you want.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900