how to find text on a result textbox? - c#

hi everyone i have an c# windows phone 8.1 app i have a result text box, i want to search in the result text box for text that will be print on him after a for loop start that's mean if my result TextBox have the value already i don't want him to print it again and if not print it my app calculate the pythagorean number i don't know how to do it plz help my faild try is :
if (from.Text == "0")
start = start + 1;
for (int i = start; i <= end; i++) {
for (int j = start+1; j <= end; j++) {
for (int k = start; k <= end; k++) {
if (i * i + j * j == k*k) {
string now = j + "+" + i ;
result.FindName(now);
result.Text += Environment.NewLine + (i + " + " + j + " = " + k) + Environment.NewLine;
c++;
}
}
}
}
i search in google for it but it look like there's no answer ! plz help me !!!!

the real answer is this:
result.Text.Contains(someString)

Related

c# I have a very confusing InvalidCastException error

I'm having an error that says "invalid cast exception" whenever I try to open one specific form in a program i'm making. The code here takes data from text files to put it into a data grid view. It also makes calculations to put averages in the data grid view. I'm getting an error that seems to involve a variable called "skiTime", which is an average of 3 other variables: "skiTime1" "skiTime2" and "skiTime3". The numbers are decimals, e.g. 23.22, or 54.32, etc.
When I try to load this form, the program closes and Visual studio highlights this line:
decimal skiTimeDecimal = Convert.ToDecimal(skiTime[i]);
I don't know what part of the code the error is in, it could literally be anywhere since my code is so messy, I basically just pulled from random tutorials, and answers to questions I asked here because I need it done for school and don't know c#. Here's the full part of the code that I think might relate to this error.
using (StreamWriter sw = new StreamWriter("groups.txt", false))
{
for (int i = 0; i < skiTimeID.Count; i++)
{
for (int j = 0; j < quizID.Count; j++)
{
for (int l = 0; l < pupilDetailsID.Count; l++)
{
if (skiTimeID[i] == quizID[j] && quizID[j] == pupilDetailsID[l])
{
string fullPupilRecord = pupilDetailsID[l] + "~" + pupilDetailsFirstName[l] + "~" + pupilDetailsSurname[l] + "~" + pupilDetailsClass[l]
+ "~" + pupilDetailsNumber[l] + "~" + skiTime[i] + "~" + quizScore[j] + "~" + quizPercentage[j];
int quizScoreNumber = Convert.ToInt32(quizScore[j]);
decimal skiTimeDecimal = Convert.ToDecimal(skiTime[i]);
if (quizScoreNumber >= 3 && skiTimeDecimal <= 40)
{
groupLevel = "Advanced";
}
else if (quizScoreNumber >= 2 && skiTimeDecimal <= 50)
{
groupLevel = "Intermediate";
}
else
{
groupLevel = "Beginner";
}
fullPupilRecord = fullPupilRecord + "~" + groupLevel;
sw.WriteLine(fullPupilRecord);
fullPupilRecord = "";
}
}
}
}
If it helps, here is the code that finds the average between the variables for skiTime:
public string CalculateAverage(List<string> skiTime1, List<string> skiTime2, List<string> skiTime3)
{
List<string> allValues = new List<string>();
allValues.AddRange(skiTime1);
allValues.AddRange(skiTime2);
allValues.AddRange(skiTime3);
decimal totalcount = 0;
decimal average = 0;
foreach (var value in allValues)
{
totalcount = totalcount + decimal.Parse(value);
}
average = totalcount / allValues.Count();
return average.ToString();
}

Filling a Matrix in windows form from user input

So the problem is that I should be able to first write a value and then it should be passed but, its executing all the code at once and not giving me time to write the value i want to give it
public void FillMatrix(string [,] v) {
for (int i = 2; i <= 4; i+=2) {
for (int j = 2; j <= 4; j++) {
lblFute.Text = "Write the number you want to put on index " + i + " " + j;
v[i, j] = txtVlera.Text;
if (j == 3 ) {
lblFute.Text = "Write the number you want to put on index " + j + " " + i;
v[j, i] = txtVlera.Text;
}
}
}
}

Unity Light Probe Coefficients == 0?

I am trying to save light probe data to a file.
However, nothing is [Serializable] (LightProbes or ShericalHarmonicsL2) so I am attempting to do it manually)
The problem is that all of my Unity Light Probe Coefficients print as 0?
Any idea what I am doing wrong?
for (int c = 0; c < LightmapSettings.lightProbes.count; c++)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 9; j++)
{
Debug.Log(c + ","+ i + "," + j + " = " + LightmapSettings.lightProbes.bakedProbes[c][i, j]);
}
}
}

alternative method of creating string from list

Have such function, which creates string from List of ids
I try to run it in 50 threads, and after 30 seconds it hangs app
public string genText(List<string> list)
{
string text = #"{""text"":""";
for (int i = 0; i < list.Count; i++)
{
if (i < list.Count - 1)
text += "!" + " ";
else
text += "!" + #""", ";
}
text += #"""start"":[";
for (int i = 0, lim = -2; i < list.Count; i++)
{
lim += 2;
if (i < list.Count - 1)
text += #"""" + lim + #""",";
else
text += #"""" + lim + #"""], ";
}
text += #"""end"":[";
for (int i = 1, lim = -1; i < list.Count + 1; i++)
{
lim += 2;
if (i < list.Count)
text += #"""" + lim + #""",";
else
text += #"""" + lim + #"""], ";
}
text += #"""type"":[";
for (int i = 0; i < list.Count; i++)
{
if (i < list.Count - 1)
text += #"""USER"",";
else
text += #"""USER""], ";
}
text += #"""objectId"":[";
for (int i = 0; i < list.Count; i++)
{
if (i < list.Count - 1)
text += #"""" + list[i] + #""",";
else
text += #"""" + list[i] + #"""]}";
}
return text;
}
What i­­­­­­­­­­­­­­s the best (alternative) way of writing such functio­­­­­­­­­­­­­­­­­­­­­­­n?
Expected result: {"text":"! ! ! ! ! ! ! ! ! ! ! ! ! ! !", "start":["0","2","4","6","8","10","12","14","16","18","20","22","24","26","28"], "end":["1","3","5","7","9","11","13","15","17","19","21","23","25","27","29"], "type":["USER","USER","USER","USER","USER","USER","USER","USER","USER","USER","USER","USER","USER","USER","USER"], "objectId":["524231066205","363249609235","509321690322","551832845250","273337925148","553378679618","552270073142","256641407","545454406232","548096729194","555315805314","553271555117","573655339037","518779453704","486606264340"]}
Instead of concatenating strings it is much more efficient to use the StringBuilder class.
public string genText(List<string> list)
{
StringBuilder text = new StringBuilder();
text.Append(#"{""text"":""");
for (int i = 0; i < list.Count; i++)
{
if (i < list.Count - 1)
text.Append("!" + " ");
...
return text.ToString();
}
but as other have mentioned in the comments, use a Json library if you want to create Json data.

Get the index while looping

Is it possible to get the current index while looping?
for (int i = 0; i < DGV.Rows.Count - 2; i++)
{
myValue = DGV.CurrentRow.Index + " " + DGV.Rows[i].Cells[1].Value.ToString();
}
But I have in output :
0 First
0 Second
0 ...
I want to get :
1 First
2 Second
3 ...
Thanks.
for (int i = 0; i < DGV.Rows.Count - 2; i++)
{
myValue = (i + 1).ToString() + " " + DGV.Rows[i].Cells[1].Value.ToString();
}
BTW: I'd prefer:
for (int i = 0; i < DGV.Rows.Count - 2; i++)
{
myValue = String.Format("{0} {1}", i + 1, DGV.Rows[i].Cells[1].Value);
}
It's the index just i+1 in this case?
Try:
myValue = (i+1) + " " + DGV.Rows[i].Cells[1].Value.ToString();
you can do this way also
for (int i = 1; i < DGV.Rows.Count - 1; i++)
{
myValue = i.ToString() + " " + DGV.Rows[i-1].Cells[1].Value.ToString();
}

Categories

Resources