if statement with string and numerical fields - c#

i have texblocks wn01, wn02, wn03 .... wn15. Every field has text="0"
If field wn01.text 0 or "" i want to enter value of przerzucanie.
If field wn01.text other value, than check next textblock.text
But if statement doesnt enter,
i=1,s=01, string.operator==returned false.
i tried ("wn"+s).Text but is not recognized
private void przerzucanie_wyniku()
{
for (int i = 1; i < 16; i++)
{
string s = i.ToString("00");
if ("wn"+s==null||"wn"+s=="0")
{
"wn"+s.Text = przerzucanie;
break;
}
}
}

"wn" + something else can't ever be null or not start with wn.
What you are doing I think is trying to find the textbox with a specific name instead of concatenating strings. If that is what you need, you have to walk the visual tree to find it. Here an example how to do that in WPF.

This will never be true:
if ("wn"+s==null||"wn"+s=="0")
This will always show "wn*".

You can try:
string w = "wn" + s;
if(w==null||w=="0"){}

answear is:
for (int i = 0; i < 15; i++)
{
TextBlock tb = (TextBlock)s7.Children[i];
if (tb.Text == "0")
{
tb.Text = przerzucanie.ToString();
break;
}

Related

Can I loop through TextBox id's to check / change values in asp.net c#?

I'm working on a project and I'm a beginner and I'm having a little trouble with this. I'm trying to check if a textbox is empty and if it is to change the value to N/A so that I can input n/a into a database instead of it not working.
Here is the code that I thought would work but didn't because the .Text property isn't near the ID anymore:
for(int i = 1; i<=17; i++)
{
if(!("tb" + i).Text)
"tb" + i.Text = "n/a";
}
I wasn't sure if the true/false would work but I never got to find out because it doesn't compile to begin with. I have 17 textboxes on my design page all with ID 'tb + i' e.g tb1, tb2
thx
If you want to loop through the textbox ids, you have to find the control on the page first using the FindControl method.
Then you can create your loop like this:
TextBox txt;
for(int i = 1; i<=17; i++)
{
txt = (TextBox)Page.FindControl("tb" + i);
if(string.IsNullOrEmpty(txt.Text))
txt.Text = "n/a";
}
You can use FindControl method
for(int i = 1; i<=17; i++)
{
var textBox = Page.FindControl("tb" + i) as TextBox;
if(textBox != null && textBox.Text == "") { ... }
}

Reference a sequence of labels in C#

I have message box that asks the user whether they want to restart.
if (MessageBox.Show("Restart?",
"Restart and Clear",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
labels.Text="0"
}
Instead of "labels" I want to use label1, label2, label3, etc. all the way to label60. I could do label1.Text=label2.Text .... = 0 but that would take a while. Is there an easy way to do this, possibly using a for statement?
I believe what you are trying to do is reset every label's text to 0 if so
foreach(Label l in this.Controls.OfType<Label>())
l.Text = "0";
On a side note, you should try to give your controls meaningful names to help when maintaining your code
Edit
A more robust approach but to make sure you don't reset text that you don't wish to change you could make a subclass of label and then use this class for labels that can be reset
class ResettableLabel : Label
foreach(ResettableLabel l in this.Controls.OfType<ResettableLabel >())...
I'd say you could use Control.ControlCollection.Find
for (int i = 1; i <= 60; i++)
{
label = Controls.Find("label" + i.ToString()) as Label;
label.Text="0"
}
Not tested but I think this Linq version should do the same
Enumerable
.Range(1, 60)
.ToList()
.ForEach(i => (Controls.Find("label" + i.ToString()) as Label).Text = "0");
You could add all of your labels to a collection or a list. Then you could easily ForEach the list.
var allLabels = new List<Label>( { label1, label2, label3, ... , label60});
allLabels.ForEach(l=> l.Text = "0");
Yes, you could use reflection like this:
for (int i = 1; i <= 60; i++)
{
var fi = this.GetType().GetField(string.Format("label{0}", i));
if (fi == null) { continue; }
fi.SetValue(this, "0");
}
Another approach would be to use the Find method:
for (int i = 1; i <= 60; i++)
{
var c = this.Controls.Find(string.Format("label{0}", i), true);
if (c == null) { continue; }
((Label)c).Text = "0";
}
In short, there are a lot of ways to skin this cat. Out of the two I provided, the second is going to be more efficient and would probably be my choice.
Use reflection:
// using System.Reflection;
for (int i = 1; i <= 60; i++)
((Label)this.GetType().GetField("label" + i, BindingFlags.NonPublic | BindingFlags.Instance).GetValue(this)).Text = "0";

Concatenate strings to make Picturebox name

i have lot of pictureboxes named this way: PBr1_1, PBr1_2, ... PBr1_9
I'd like to make loop
for (int i = 0; i < 10; i++)
{
if (Textbox.Text[i].ToString() == "1"){ "PBr1_"+"i".Tag = "cb.png";}
}
so for i=0 => PBr1_0, i=10 => Pbr1_10.
Example i have value in textbox: 0001011101 - then if value in textbox is "1" then i'd like to change picturebox tag.
How to automate this process, using for example loop "for"?
I suppose your controls are on a WinForm (this) and the ones with that name are all pictureboxes.
If so, that's the way ----
for (int i = 0; i < 10; i++)
{
if (Textbox.Text[i].ToString() == "1")
{
Control[] c = this.Controls.Find("PBr1_" + i.ToString(), true);
if(c != null && c.Length > 0) c[0].Tag = "cb.png";
}
}
You can put the picture boxes in to a List<PicutreBox> and iterate over the list.
var pictures = new List<PictureBox>();
pictures.Add(pic1);
pictures.Add(pic2);
//...
for (int i = 0; i < 10; i++)
{
if (Textbox.Text[i].ToString() == "1")
pictures[i].Tag = "cb.png";
}
Dynamic variable names as in your example are not supported.
Create an array (or list) that contain the picture boxes and use those within the for loop.
You can also use reflection but in my opinion it is best not to use that in this case.
If you are using WinForm you can use Control.Find method to locate a control by name
Once you have got the control you can easily change any property

C# For Loop with LIST using LINQ

I'm using LINQ and returning a list to my Business Logic Layer. I'mtrying to change one of the values in the list (changing the 'star' rating to an image with the number of stars).
Although the counter (i) appears to be working, the FOR loop is not working correctly. The first time through it stops at the correct IF but then it pops out at the ELSE statement for everything and all values end up with "star0.png." It appears as though I'm not cycling through the list??? Thanks in advance!
for (int i = 0; i < ReviewList.Count; i++)
{
string serviceCode = ReviewList[i].SERVICE.SERVICE_DESC;
if (serviceCode == "*")
{
ReviewList[i].SERVICE.SERVICE_DESC = "star1.png";
}
else if (serviceCode == "**")
{
ReviewList[i].SERVICE.SERVICE_DESC = "star2.png";
}
else if (serviceCode == "***")
{
ReviewList[i].SERVICE.SERVICE_DESC = "star3.png";
}
else if (serviceCode == "****")
{
ReviewList[i].SERVICE.SERVICE_DESC = "star4.png";
}
else
{
ReviewList[i].SERVICE.SERVICE_DESC = "star0.png";
}
}
If all values end up at star0.png, then you are cycling through the list. The fact that the else statement is the only code being executed for each element suggests a logical error -- did you perhaps mean to do something like this?
string serviceCode = ReviewList[i].SERVICE.SERVICE_CODE;
I dont think its an issue of the for loop working properly... your syntax is good and as written will iterate ReviewList.Count # of times.
I would step through and verify the contents of ReviewList first.
Let me know what you find
If you know each item will consist of a number of stars, why not do this?:
for (int i = 0; i < ReviewList.Count; i++)
{
string serviceCode = ReviewList[i].SERVICE.SERVICE_DESC;
ReviewList[i].SERVICE.SERVICE_DESC = "star" + serviceCode.Length + ".png";
}
Protection on double pass and with else condition
for (int i = 0; i < ReviewList.Count; i++)
{
string serviceCode = ReviewList[i].SERVICE.SERVICE_DESC;
if(!serviceCode.Contains(".png")) { // once name set should not be modified
if(serviceCode.Contains("*"))
ReviewList[i].SERVICE.SERVICE_DESC = "star" + serviceCode.Length + ".png";
else
ReviewList[i].SERVICE.SERVICE_DESC = "star0.png";
}
}
alternate LINQ approach
ReviewList.ForEach(rs=>if(!rs.SERVICE.SERVICE_DESC.Contains(".png"))
{ rs.SERVICE.SERVICE_DESC =
"star" + rs.SERVICE.SERVICE_DESC.Length + ".png"});

Finding and replacing text in c#

I need to add functionality in my program so that any file imported it will find the text within the "" of the addTestingPageContentText method as seen below. The two values on each line will then be added to a datagridview which has 2 columns so first text in first column then second in the 2nd column. How would i go about Finding the "sometext" ?
addTestingPageContentText("Sometext", "Sometext");
addTestingPageContentText("Sometext2", "Sometext2");
... continues n number of times.
Neither fast nor efficient, but it's easier to understand for those new to regular expressions:
while (!endOfFile)
{
//get the next line of the file
string line = file.readLine();
EDIT: //Trim WhiteSpaces at start
line = line.Trim();
//check for your string
if (line.StartsWith("addTestingPageContentText"))
{
int start1;
int start2;
//get the first something by finding a "
for (start1 = 0; start1 < line.Length; start1++)
{
if (line.Substring(start1, 1) == '"'.ToString())
{
start1++;
break;
}
}
//get the end of the first something
for (start2 = start1; start2 < line.Length; start2++)
{
if (line.Substring(start2, 1) == '"'.ToString())
{
start2--;
break;
}
}
string sometext1 = line.Substring(start1, start2 - start1);
//get the second something by finding a "
for (start1 = start2 + 2; start1 < line.Length; start1++)
{
if (line.Substring(start1, 1) == '"'.ToString())
{
start1++;
break;
}
}
//get the end of the second something
for (start2 = start1; start2 < line.Length; start2++)
{
if (line.Substring(start2, 1) == '"'.ToString())
{
start2--;
break;
}
}
string sometext2 = line.Substring(start1, start2 - start1);
}
}
However I would seriously recommend going through some of the great tutorials out there on the internet. This is quite a good one
The expression "\"[^"]*\"" would find each...

Categories