So I want to have a number that can be added to but then written out in a label. I'm doing it in Visual C#.NET.
For example, if I had code like this (either in a timer or while loop):
int i = 0;
i = i + 5;
label4 = "Current Number of Views: " + i.ToString();
I can't use ToString() to convert it to a string. So how would I make i displayable
While I don't understand why you can't use i.toString(), I suppose that you could also use something like
label4.Text = String.Format("Current Number of Views: {0:d}", i);
This should yield the same result.
Edit: as #BiggsTRC points out (which I didn't think of), your error is probably a result of not assigning to the right variable. You probably should access the property label4.Text to assign to. The code example fixes this properly now.
u could use String.Format without explicitly using ToString()
label4.Text = String.Format("Current Number of Views: {0}", i);
This should work. The one issue i see is the label itself. It should look like this:
label4.Text = "Current Number of Views: " + i.ToString();
I dont understand why cant you use i.ToString(). By default, i is assigned with 0 value and hence, i.ToString() would not throw any exception.
if you are using WPF , then you should assign the value to the content property of the label.
label4.Content = "Current Number of Views: " + i.ToString();
Related
I'm working on a project now, to make it easier to work I decided to make related textbox, the question is how to set the value of textbox so that it will be appears automatically?
Inthis case, Rnol = Pnol + Qnol + Nnol
I want Rnol value to be printed automatically in tbRnol/textbox Rnol when I click Button..
Pnol = double.Parse(tbPnol.Text);
Qnol = double.Parse(tbQnol.Text);
Nnol = double.Parse(tbNnol.Text);
Rnol = Pnol + Qnol + Nnol(tbRnol.Text); //I've try this but it clearly wrong syntax
You could modify your last line to:
Rnol = Pnol + Qnol + Nnol;
And add this one:
tbRnol.Text = Rnol.ToString();
I'm reading a field On a table it only has 3 values ("",ESD,R&S)
I don't know exactly why, but when I read the R&S value, the print out label is R ("empty space") S
this is the code I'm using:
char[] area = read1[8].ToString().ToCharArray();
// if array is less than one do nothing
if (area.Length > 1)
{
//trying to use this to check if the second item of array is the "&" symbol (print this format data)
if (area[1].ToString() == "&")
{
Arealbl.Text = area[0].ToString() + "\n" + "&" + "\n" + area[2].ToString();
}
//else print out this format data
else
{
Arealbl.Text = area[0].ToString() + "\n" + area[1].ToString() + "\n" + area[2].ToString();
}
}
I using this code because I haven't found an easy way to put a label on vertical.
The & is a special char in MenuItems, Labels and Buttons, used to indicate that the next char should be underscored. When you manage to focus Arealbl and hit Alt you might see that.
Set
Arealbl.UseMnemonic = false;
somewhere. Like with the designer.
In addition to #Henk Holterman's answer, here are a few code review suggestions. You can access a string as an array, so there is no need to .ToString().ToCharArray(), just to .ToString() everything further down the method. Simplifying the concatenation to a string.Format can help improve readability and assuming you don't have to do this a large number of times (tens of thousands) it shouldn't impact performance.
string area = read1[8].ToString()
if(area.Length < 3) { return; } //exit early on error conditions.
// if array is less than one do nothing
Arealbl.UseMnemonic = false; //only add this if you cannot guarantee it will be set.
Arealbl.Text = string.Format("{0}\n{1}\n{2}", area[0], area[1], area[2]);
well, I've been trying to create a new custom property in a shape and I somehow managed, however, when I try to change the name of the Label I can only write numbers. Could you provide me how to do it in C# or maybe in VB so I can get a hint?
My code is:
//First I create the row
shape.AddRow((short)VisSectionIndices.visSectionProp,(short) (iRow + 1), (short) VisRowTags.visTagDefault);
//And now I try to write the Label
shape.CellsSRC[(short)VisSectionIndices.visSectionProp, (short)(iRow + 1), (short)VisCellIndices.visCustPropsLabel].Result[VisUnitCodes.visNoCast] = 123456789
However, when the Result method only accepts boolean as input, and I don't know how to write a string overthere...
Thanks in advance!
I've also been looking into how to set the string value of a custom shape data property.
Just got it to work like this:
var newPropertyValue = "cool new value";
tstShape.Cells["Prop.SuperCustomPropertyName"].FormulaU = "\"" + newPropertyValue + "\"";
Disclaimer that I am no expert with Visio Automation, but it works in my circumstance.
I'm using visio 2010 and studio 2010
Hopefully it helps.
You can use the following code:
private void AddCustomProperty(Microsoft.Office.Interop.Visio.Shape shape, string PropertyName, String propertyValue)
{
try
{
short customProps = (short)VisSectionIndices.visSectionProp;
short rowNumber = shape.AddRow(customProps, (short)VisRowIndices.visRowLast, (short)VisRowTags.visTagDefault);
shape.CellsSRC[customProps, rowNumber, (short)VisCellIndices.visCustPropsLabel].FormulaU = "\"" + PropertyName + "\"";
shape.CellsSRC[customProps, rowNumber, (short)VisCellIndices.visCustPropsValue].FormulaU = "\"" + propertyValue + "\"";
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show("Error: " + e.Message);
}
}
The Result isn't meant to be read/write. What you want to do is set the FormulaU property of the cell to the label name. The Result property just calculates the formula for the cell and returns the result, which is why you have to provide a unit for the return value.
Also, the AddRow method returns the actual row number for the added row, which isn't necessarily the row you specified. For shapesheet sections with nameable rows, like the Custom Properties section, Visio may ignore the row you requested and just stick it at the bottom.
How to concatenate the contents of variable in the name of label and others variables?
//labels: lbl_01_temp, lbl_02_temp, lbl_03_temp
string XX;
double id_01_temp, id_02_temp, id_03_temp;
lbl_XX_temp.Text= "The Device " +XX+ "has" +id_XX_temp+" ℃";
The clean way to concatenate values is to use String.Format
lbl_XX_temp.Text= String.Format("The Device {0} has {1} ℃", XX, id_XX_temp);
See MSDN Doc: String.Format()
Maybe, I misunderstood the question. I think the OP wants to convert a string into a valid control right?
Web:
string lblSelected = String.Format("lbl_{0}_temp", XX);
Label lbl = (Label)this.FindControl(lblSelected);
lbl.Text = String.Format("The Device {0} has {1} ℃", XX, id_XX_temp);
WinForms:
string lblSelected = String.Format("lbl_{0}_temp", XX);
Control[] ctrl = this.Controls.Find(lblSelected, true);
Label lbl = ctrl[0] as Label;
lbl.Text = String.Format("The Device {0} has {1} ℃", XX, id_XX_temp);
String.Format is actually less performing than a straight string concatenation like your question poses. It's probably a matter of ticks and nanoseconds, but there is more overhead involved with String.Format than just saying a + b + c for string concatenation.
Personally, String.Format looks cleaner to me, but it's not faster.
I would use a pair of arrays or dictionaries (setting them up is left as the proverbial exercise for the reader):
labels[index].Text = "Device " + (index + 1) + " has temperature "
+ temperatures[index].ToString(formatString) + " ℃";
EDIT
From the code in your comment, it seems you want to identify a label by name, and that you will derive the name from an integer variable, like this:
for (int i=1; i<=6; i++)
{
Label label = GetLabelForIndex(i);
//do something with the label here
}
So the question is, how will you get the label for a given index? In other words, what is the implementation of GetLabelForIndex(int)? The answer to that question depends on what kind of Label we're talking about. What library does it come from? Is it WinForms? Silverlight? WPF? If it's WinForms, see Get a Windows Forms control by name in C#. If WPF or Silverlight, see Find WPF control by Name. The accepted answer here also proposes using a dictionary with a string key, which is similar to my suggestion above.
Unless you're doing this thousands of times a second, the performance benefit of using a dictionary is probably insignificant, in which case you should just use Find or FindName directly.
I need to know the command that I can print a sentence like "the item Peter at row 233 and column 1222 is not a number " .
I far as now I have made this:
string[] lineItems = (string[])List[]
if (!Regex.IsMatch(lineItems[0], (#"^\d*$")))
textBox2.Text += " The number ,lineItems[0], is bigger than
10 " + Environment.NewLine;
I want to print the array fields that have error. So if it finds something it will print it.
I made a code that correctly prints that there is an error on this line of the array, but I cant print the item of the array.
I need to have an Environment.NewLine because I will print many lines.
Thanks ,
George.
foreach (int lineNumber in lineItems)
{
if (lineNumber > 10)
textBox2.Text += "The number " + lineNumber + " is bigger than 10\n";
}
Something like this should work, (I have not checked the c# code, I am working on a mac at the moment)
TextBox2.Text="This is FirstLine\nThis is Second Line";
The code is not compilable absolutely, but I may be understand what you're asking about.
If you are asking about how to compose the string of text box, by adding new strings to it, based on some desicional condition (regex), you can do folowing, pseudocode:
StringBuilder sb = new StringBuidler();
if (!Regex.IsMatch(lineItems[i], (#"^\d*$")))
sb.Append(string.Format(The number ,{0}, is bigger than 10, lineItems[i]) + Environment.NewLine);
textBox2.Text = sb.ToString();
If this is not what you want, just leave the comment, cause it's not very clear from post.
Regards.