Related
I am new to c# (or coding in general) and I guess this question is really stupid and confusing (I know I'm doing it a hard way) but please help me.
I'm trying to make a minesweeper with form app. I made a 10 x 10 of buttons and if you click it, number of mines around it will be revealed. If a mine is there "F" (the first letter of "False") will appear.
There's a constructor that contains the button, x and y position, list of surrounding blocks, number of mines around it, and a boolean that indicates if there's a mine or not.
What I tried to do was to make the 8 surrounding blocks (from the list) cleared when the player clicked a block with no mine around it and if the block surrounding that block also doesn't have any mine around it, these blocks that surrounding that block will also be cleared. The method uses foreach to reveal and check the number of mines around that block. If there's no mines, same method will be applied to that block (calling the method recursively). The problem is that I keep getting System.StackOverflowException.
I somehow understand why it's happening but I just can't come up with the other way.
//scroll to the bottom for the method with the problem
private void Form1_Load(object sender, EventArgs e)
{
Random random = new Random();
Button[,] buttons = new Button[10, 10]
{
{ r0c0, r0c1, r0c2, r0c3, r0c4, r0c5, r0c6, r0c7, r0c8, r0c9 },
{ r1c0, r1c1, r1c2, r1c3, r1c4, r1c5, r1c6, r1c7, r1c8, r1c9 },
{ r2c0, r2c1, r2c2, r2c3, r2c4, r2c5, r2c6, r2c7, r2c8, r2c9 },
{ r3c0, r3c1, r3c2, r3c3, r3c4, r3c5, r3c6, r3c7, r3c8, r3c9 },
{ r4c0, r4c1, r4c2, r4c3, r4c4, r4c5, r4c6, r4c7, r4c8, r4c9 },
{ r5c0, r5c1, r5c2, r5c3, r5c4, r5c5, r5c6, r5c7, r5c8, r5c9 },
{ r6c0, r6c1, r6c2, r6c3, r6c4, r6c5, r6c6, r6c7, r6c8, r6c9 },
{ r7c0, r7c1, r7c2, r7c3, r7c4, r7c5, r7c6, r7c7, r7c8, r7c9 },
{ r8c0, r8c1, r8c2, r8c3, r8c4, r8c5, r8c6, r8c7, r8c8, r8c9 },
{ r9c0, r9c1, r9c2, r9c3, r9c4, r9c5, r9c6, r9c7, r9c8, r9c9 }
};
Square[,] squares = new Square[10, 10];
for (int i = 0, ii = 0, iii = 0; i < 100; i++, ii++)
{
if (ii == 10)
{
ii = 0;
iii++;
}
squares[ii, iii] = new Square(i, buttons[ii, iii], ii, iii, 0, true);
}
List<int> randoms = new List<int>();
for (int i = 0; i < 10; i++)
{
int ii = random.Next(100);
if (!randoms.Contains(ii))
{
squares[ii % 10, ii / 10].setSafe(false);
}
else
{
i--;
}
randoms.Add(ii);
}
for (int i = 0; i < 10; i++)
{
for (int ii = 0; ii < 10; ii++)
{
for (int iii = -1; iii < 2; iii++)
{
for (int iiii = -1; iiii < 2; iiii++)
{
try
{
if (squares[i + iii, ii + iiii].getSafe() == false)
squares[i, ii].addNumber();
}
catch (System.IndexOutOfRangeException)
{
}
}
//if (squares[i, ii].getSafe() == false) squares[i, ii].getButton().Text = squares[i, ii].getSafe().ToString();
//else squares[i, ii].getButton().Text = squares[i, ii].getNumber().ToString();
}
}
}
for (int i = 0; i < 10; i++)
{
for (int ii = 0; ii < 10; ii++)
{
for (int iii = -1; iii < 2; iii++)
{
for (int iiii = -1; iiii < 2; iiii++)
{
try
{
squares[i, ii].addList(squares[i + iii, ii + iiii]);
}
catch (System.IndexOutOfRangeException)
{
}
}
}
}
}
}
Here's the Square class:
public class Square
{
int id;
Button button;
int x;
int y;
int number;
bool safe;
List<Square> list = new List<Square>();
public Square(int id, Button button, int x, int y, int number, bool safe)
{
this.id = id;
this.button = button;
this.x = x;
this.y = y;
this.number = number;
this.safe = safe;
button.Text = "";
button.Click += button_Click;
}
public int getId()
{
return id;
}
public void setId(int i)
{
id = i;
}
public Button getButton()
{
return button;
}
public void setButton(Button b)
{
button = b;
}
public int getX()
{
return x;
}
public void setX(int i)
{
x = i;
}
public int getY()
{
return y;
}
public void setY(int i)
{
y = i;
}
public int getNumber()
{
return number;
}
public void setNumber(int i)
{
number = i;
}
public void addNumber()
{
number++;
}
public bool getSafe()
{
return safe;
}
public void setSafe(bool b)
{
safe = b;
}
private void button_Click(object sender, EventArgs e)
{
if (getSafe() == false) button.Text = getSafe().ToString();
else button.Text = getNumber().ToString();
if (getNumber() == 0) zeroReveal();
}
//---------------------------------------------------
// this is the method that reveals surrounding blocks
//---------------------------------------------------
private void zeroReveal()
{
foreach (Square s in list)
{
//revealing the blocks
s.getButton().Text = s.getNumber().ToString();
//call the same method if there's no mine
//this is the line that keeps giving me exception
if (s.getNumber() == 0) s.zeroReveal();
}
}
//-----------------------------------------------------
public List<Square> getList()
{
return list;
}
public void setList(List<Square> sl)
{
list = sl;
}
public void addList(Square s)
{
list.Add(s);
}
}
I am new to c# (or coding in general) and I guess this question is really stupid and confusing (I know I'm doing it a hard way)
This topic confuses many a new developer; don't stress out about it!
If there's no mines, same method will be applied to that block (calling the method recursively).
Recursive methods can be confusing but if you design them using the standard pattern, you will avoid SO exceptions. You have not designed yours using the standard pattern.
The standard pattern for successful recursive methods is:
Am I in a case that requires no recursion?
If yes, do the necessary computations to produce the desired effect and return. The problem is now solved.
If no, then we're going to recurse.
Break the current problem down into some number of smaller problems.
Solve each smaller problem by recursing.
Combine the solutions of the smaller problem to solve the current problem.
The problem is now solved, so return.
The most important thing about designing a recursive method is that each recursion must be solving a smaller problem, and the sequence of smaller problems must bottom out at a case that does not require recursion. If those two conditions are not met, then you will get a stack overflow.
Internalize that pattern, and every time you write a recursive method, actually write it out:
int Frob(int blah)
{
if (I am in the base case)
{
solve the base case
return the result
}
else
{
find smaller problems
solve them
combine their solutions
return the result
}
}
Fill in that template with your real code, and you will be much more likely to avoid stack overflows. I've been writing recursive methods for decades, and I still follow this pattern.
Now, in your example, what is the case that does not require recursion? There must be one, so write down what it is. Next, how will you guarantee that the recursion solves a smaller problem? That is often the hard step! Give it some thought.
The stack overflow is occurring because zeroReveal is recursively calling itself forever. To fix this we need to find ways where we do not need it to make further calls to itself.
The name of the method gives us a clue. If the square has already been revealed, then surely the method does not need to do anything, since it has already been revealed.
It looks like the button's Text property is an empty string if it has not yet been revealed. So change the foreach so that it doesn't process squares that have already been revealed:
foreach (Square s in list)
{
if (s.getButton().Text == ""))
{
// existing code in the foreach loop goes here
}
}
I'm trying to find instances of a string in a WPF RichTextBox. What I have now almost works, but it highlights the wrong section of the document.
private int curSearchLocation;
private void FindNext_Click(object sender, RoutedEventArgs e)
{
TextRange text = new TextRange(RichEditor.Document.ContentStart, RichEditor.Document.ContentEnd);
var location = text.Text.IndexOf(SearchBox.Text, curSearchLocation, StringComparison.CurrentCultureIgnoreCase);
if (location < 0)
{
location = text.Text.IndexOf(SearchBox.Text, StringComparison.CurrentCultureIgnoreCase);
}
if (location >= 0)
{
curSearchLocation = location + 1;
RichEditor.Selection.Select(text.Start.GetPositionAtOffset(location), text.Start.GetPositionAtOffset(location + SearchBox.Text.Length));
}
else
{
curSearchLocation = 0;
MessageBox.Show("Not found");
}
RichEditor.Focus();
}
This is what happens when I search for "document":
This is because GetPositionAtOffset includes non-text elements such as opening and closing tags in its offset, which is not what I want. I couldn't find a way to ignore these elements, and I also couldn't find a way to directly get a TextPointer to the text I want, which would also solve the problem.
How can I get it to highlight the correct text?
Unfortunately the TextRange.Text strips out non-text characters, so in this case the offset computed by IndexOf will be slightly too low. That is the main problem.
I tried to solve your problem and found working solution that works fine even when we have formatted text in many paragraphs.
A lot of help is taken from this CodeProject Article. So also read that article.
int curSearchLocation;
private void FindNext_Click(object sender, RoutedEventArgs e)
{
TextRange text = new TextRange(RichEditor.Document.ContentStart, RichEditor.Document.ContentEnd);
var location = text.Text.IndexOf(SearchBox.Text, curSearchLocation, StringComparison.CurrentCultureIgnoreCase);
if (location < 0)
{
location = text.Text.IndexOf(SearchBox.Text, StringComparison.CurrentCultureIgnoreCase);
}
if (location >= 0)
{
curSearchLocation = location + 1;
Select(location, SearchBox.Text.Length);
}
else
{
curSearchLocation = 0;
MessageBox.Show("Not found");
}
RichEditor.Focus();
}
public void Select(int start, int length)
{
TextPointer tp = RichEditor.Document.ContentStart;
TextPointer tpLeft = GetPositionAtOffset(tp, start, LogicalDirection.Forward);
TextPointer tpRight = GetPositionAtOffset(tp, start + length, LogicalDirection.Forward);
RichEditor.Selection.Select(tpLeft, tpRight);
}
private TextPointer GetPositionAtOffset(TextPointer startingPoint, int offset, LogicalDirection direction)
{
TextPointer binarySearchPoint1 = null;
TextPointer binarySearchPoint2 = null;
// setup arguments appropriately
if (direction == LogicalDirection.Forward)
{
binarySearchPoint2 = this.RichEditor.Document.ContentEnd;
if (offset < 0)
{
offset = Math.Abs(offset);
}
}
if (direction == LogicalDirection.Backward)
{
binarySearchPoint2 = this.RichEditor.Document.ContentStart;
if (offset > 0)
{
offset = -offset;
}
}
// setup for binary search
bool isFound = false;
TextPointer resultTextPointer = null;
int offset2 = Math.Abs(GetOffsetInTextLength(startingPoint, binarySearchPoint2));
int halfOffset = direction == LogicalDirection.Backward ? -(offset2 / 2) : offset2 / 2;
binarySearchPoint1 = startingPoint.GetPositionAtOffset(halfOffset, direction);
int offset1 = Math.Abs(GetOffsetInTextLength(startingPoint, binarySearchPoint1));
// binary search loop
while (isFound == false)
{
if (Math.Abs(offset1) == Math.Abs(offset))
{
isFound = true;
resultTextPointer = binarySearchPoint1;
}
else
if (Math.Abs(offset2) == Math.Abs(offset))
{
isFound = true;
resultTextPointer = binarySearchPoint2;
}
else
{
if (Math.Abs(offset) < Math.Abs(offset1))
{
// this is simple case when we search in the 1st half
binarySearchPoint2 = binarySearchPoint1;
offset2 = offset1;
halfOffset = direction == LogicalDirection.Backward ? -(offset2 / 2) : offset2 / 2;
binarySearchPoint1 = startingPoint.GetPositionAtOffset(halfOffset, direction);
offset1 = Math.Abs(GetOffsetInTextLength(startingPoint, binarySearchPoint1));
}
else
{
// this is more complex case when we search in the 2nd half
int rtfOffset1 = startingPoint.GetOffsetToPosition(binarySearchPoint1);
int rtfOffset2 = startingPoint.GetOffsetToPosition(binarySearchPoint2);
int rtfOffsetMiddle = (Math.Abs(rtfOffset1) + Math.Abs(rtfOffset2)) / 2;
if (direction == LogicalDirection.Backward)
{
rtfOffsetMiddle = -rtfOffsetMiddle;
}
TextPointer binarySearchPointMiddle = startingPoint.GetPositionAtOffset(rtfOffsetMiddle, direction);
int offsetMiddle = GetOffsetInTextLength(startingPoint, binarySearchPointMiddle);
// two cases possible
if (Math.Abs(offset) < Math.Abs(offsetMiddle))
{
// 3rd quarter of search domain
binarySearchPoint2 = binarySearchPointMiddle;
offset2 = offsetMiddle;
}
else
{
// 4th quarter of the search domain
binarySearchPoint1 = binarySearchPointMiddle;
offset1 = offsetMiddle;
}
}
}
}
return resultTextPointer;
}
int GetOffsetInTextLength(TextPointer pointer1, TextPointer pointer2)
{
if (pointer1 == null || pointer2 == null)
return 0;
TextRange tr = new TextRange(pointer1, pointer2);
return tr.Text.Length;
}
Hope so this code will work for your case.
I'm trying to make 'find/find next' function in my windows store application.
Word which I want to search and select is in textBox named 'tboxFind'.
Textbox 'EditorWindow' contains all my text.
My function works good only if there is one line of text in 'editorWindow'.
Otherwise, selection is moved forwards by number of new lines.
How to fix it?
Is there any simple way to create find next function?
private void btnFind_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
if ((tmpPos) == pos && tmpWord == tboxFind.Text && !String.IsNullOrEmpty(editorWindow.Text))
{
string tmpString = editorWindow.Text.Substring(pos + tboxFind.Text.Length);
tmpPos = tmpString.ToLower().IndexOf(tboxFind.Text.ToLower());
if (tmpPos != -1)
{
editorWindow.Focus(Windows.UI.Xaml.FocusState.Keyboard);
editorWindow.SelectionStart = pos + tmpPos + tboxFind.Text.Length;
editorWindow.SelectionLength = tboxFind.Text.Length;
pos = pos + tmpPos + tboxFind.Text.Length;
}
}
tmpWord = tboxFind.Text;
tmpPos = pos;
}
// EDIT:
I found a different way to create that function. Here is my code:
private void btnFind_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
numOfNewLines = 0;
pos = (tmpWord == tboxFind.Text) ? editorWindow.Text.ToLower().IndexOf(tboxFind.Text, pos + tboxFind.Text.Length)
: editorWindow.Text.ToLower().IndexOf(tboxFind.Text);
if (pos != -1)
{
foreach (char s in editorWindow.Text.Substring(0, pos))
{
if (s == '\n')
{
numOfNewLines++;
}
}
pos -= numOfNewLines;
editorWindow.Focus(Windows.UI.Xaml.FocusState.Keyboard);
//tmpPos = editorWindow.Text.ToLower().IndexOf(tboxFind.Text);
editorWindow.Select(pos, tboxFind.Text.Length);
pos += numOfNewLines;
}
tmpWord = tboxFind.Text;
}
I'm not 100% sure what's wrong with your code because I can't fully replicate it, but consider the following SSCCE in a basic Windows application:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
foreach (var i in FindIndicies("text"))
{
this.textBox1.SelectionStart = i;
this.textBox1.SelectionLength = "text".Length;
var result = MessageBox.Show(
"Move to the next index?",
"Next?",
MessageBoxButtons.YesNo);
if (result == System.Windows.Forms.DialogResult.No) { break; }
}
}
private List<int> FindIndicies(string textToFind)
{
var indicies = new List<int>();
var offset = 0;
var i = 0;
while ((i = this.textBox1.Text.IndexOf(
textToFind,
offset,
StringComparison.CurrentCultureIgnoreCase)) > 0)
{
indicies.Add(i);
offset = (i + textToFind.Length);
}
return indicies;
}
}
given that textBox1 has a set text value of:
Here is a set of text
and I'm going to find the word text
Even when there are multiple lines of text.
It finds each index properly, and selects them properly.
I would consider finding all indicies up front with the method I wrote and then simply iterate through them on demand. In my case I'm using a message box to determine when I want to move to the next index, but you'll use something different.
I'm working on storing last used settings and then when program starts retrieve them back. Everything works fine but I just don't know how to deal with ListBox items.
To point out one thing is that I'm already using one delimiter to store my settings. I'm getting quite confused when dealing with this problem.
This is how I store my settings:
private void btnStart_Click(object sender, EventArgs e)
{
int interval = 0;
int plusMinus = 0;
int pause = 0;
int delay = 0;
int randomLine = 0;
if (cbPause.Checked == true) pause = 1;
if (cbDelay.Checked == true) delay = 1;
if (cbRandomLine.Checked == true) randomLine = 1;
interval = int.Parse(nudInterval.Value.ToString());
plusMinus = int.Parse(nudPlusMinus.Value.ToString());
lastUsed.Text =
interval + splitString +
plusMinus + splitString +
pause + splitString +
delay + splitString +
randomLine;
if (nudPlusMinus.Value == 0)
{
tmrInterval.Interval = int.Parse(nudInterval.Value.ToString());
}
else
{
Random random = new Random();
tmrInterval.Interval = random.Next(int.Parse(nudInterval.Value.ToString()) - int.Parse(nudPlusMinus.Value.ToString()), int.Parse(nudInterval.Value.ToString()) + int.Parse(nudPlusMinus.Value.ToString()));
}
WhenStarted();
tmrInterval.Start();
}
This is how I retrieve them at the program start up:
public AutoTyper()
{
InitializeComponent();
tmrInterval.Tick += new EventHandler(Interval);
tmrDelay.Tick += new EventHandler(Delay);
tmrSpace.Tick += new EventHandler(Space);
lbMessage.SelectedIndexChanged += new EventHandler(lbMessage_SelectedIndexChanged);
txtMessage.TextChanged += new EventHandler(txtMessage_TextChanged);
SetInterval();
if (!lastUsed.EmptyFile())
{
string[] allSettings = lastUsed.Text.Split(splitChar, StringSplitOptions.None);
int settingCount = 0;
int settingNumber = 0;
foreach (string setting in allSettings) settingNumber++;
if (settingNumber == 5)
{
foreach (string setting in allSettings)
{
settingCount++;
if (settingCount == 1) nudInterval.Value = int.Parse(setting);
else if (settingCount == 2) nudPlusMinus.Value = int.Parse(setting);
else if (settingCount == 3) { if (setting == "1") cbPause.Checked = true; }
else if (settingCount == 4) { if (setting == "1") cbDelay.Checked = true; }
else if (settingCount == 5) { if (setting == "1") cbRandomLine.Checked = true; }
}
}
}
}
Just retrieve/set SelectedIndex after adding all values (unless that happens at design time already).
But in general, I'd rewrite that settings handling. You should store your settings using keys and values. Otherwise you'll run into tons issue if you ever want to add, remove or change the order of some settings.
I built a little program that calculates the average of 15 numbers or less. There are 15 text-boxes, each one's default value is '0'. The program knows to get the sum of all typed numbers and divide it by numbers of text boxes that don't return '0'. But if the user deletes in mistake one of the '0'os in one of the text boxs.. run-time error.
Originally I solved this problam by writing this "if statement" 15 times(one for each text-box):
if (t1.Text == "") { tr1 = 0; }
else
{
tr1 = Double.Parse(t1.Text);
}
this code checks if there isn't a thing in text box(for example, named t1), if true, the program is giving the double 'tr1'(don't confuse with 't1'), the value of '0', if false, the code gives the double 'tr1' the text of 't1'.
i had to write this 'if' 15 times. i wanted to know if i can write the same code with arrays and a for loop, and how?
here is the whole code (sorry for var names are not similar to var's use.):
private void goyouidiot_Click(object sender, EventArgs e)
{
double tr1;
double tr2;
double tr3;
double tr4;
double tr5;
double tr6;
double tr7;
double tr8;
double tr9;
double tr10;
double tr11;
double tr12;
double tr13;
double tr14;
double tr15;
if (t1.Text == "") { tr1 = 0; }
else
{
tr1 = Double.Parse(t1.Text);
}
if (t2.Text == "") { tr2 = 0; }
else
{
tr2 = Double.Parse(t2.Text);
}
if (t3.Text == "") { tr3 = 0; }
else
{
tr3 = Double.Parse(t3.Text);
}
if (t4.Text == "") { tr4 = 0; }
else
{
tr4 = Double.Parse(t4.Text);
}
if (t5.Text == "") { tr5 = 0; }
else
{
tr5 = Double.Parse(t5.Text);
}
if (t6.Text == "") { tr6 = 0; }
else
{
tr6 = Double.Parse(t6.Text);
}
if (t7.Text == "") { tr7 = 0; }
else
{
tr7 = Double.Parse(t7.Text);
}
if (t8.Text == "") { tr8 = 0; }
else
{
tr8 = Double.Parse(t8.Text);
}
if (t9.Text == "") { tr9 = 0; }
else
{
tr9 = Double.Parse(t9.Text);
}
if (t10.Text == "") { tr10 = 0; }
else
{
tr10 = Double.Parse(t10.Text);
}
if (t11.Text == "") { tr11 = 0; }
else
{
tr11 = Double.Parse(t11.Text);
}
if (t12.Text == "") { tr12 = 0; }
else
{
tr12 = Double.Parse(t12.Text);
}
if (t13.Text == "") { tr13 = 0; }
else
{
tr13 = Double.Parse(t13.Text);
}
if (t14.Text == "") { tr14 = 0; }
else
{
tr14 = Double.Parse(t14.Text);
}
if (t15.Text == "") { tr15 = 0; }
else
{
tr15 = Double.Parse(t15.Text);
}
double[] sch = { tr1, tr2, tr3, tr4, tr5, tr6, tr7, tr8, tr9, tr10, tr11, tr12, tr13, tr14, tr15 };
double total = 0;
double sorf = 0;
for (int i = 0; i != 14; i++)
{
sorf = sorf + sch[i];
if (sch[i] > 0)
{ total++; }
}
double totalic = sorf / total;
string glass = totalic.ToString();
result.Text = ("your score: " + glass);
}
Double.TryParse(t1.Text.Trim(), out tr1);
will set tr1 to the text box's numeric value, or 0.0 if it failed to convert it for some reason. It'll also return true if the conversion succeeded or false if it failed, but you don't care about the return value if the default is 0.0.
Added bonus: it won't throw an exception if someone decides to put "This is not a number." into a text box. It'll just see the value as 0.
To do this in an array...
TextBox t[] = { t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15 };
double tr[] = new double[t.Length];
for (int i = 0; i < t.Length; ++i)
{
Double.TryParse(t[i].Text.Trim(), out tr[i]);
}
UPDATE:
Note, it's perfectly reasonable to expect to be able to compute an average of numbers that includes 0. In order to do this:
TextBox t[] = { t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15 };
double tr[] = new double[t.Length];
int valid_count = 0;
for (int i = 0; i < t.Length; ++i)
{
if (Double.TryParse(t[i].Text.Trim(), out tr[i])) ++valid_count;
}
Set your TextBoxes' default values to blank (""), and then you'll know how many were legitimately 0's entered by the user and how many were blank. Divide the sum by valid_count to get an accurate average. (But be sure valid_count > 0, or you'll likely get a divide-by-zero exception.)
Sure, make a double tr[15] and a corresponding array of text fields.
Then just use:
for (int i = 0; i < 15; i++) {
if (t[i].Text == "") {
tr[i] = 0;
} else {
tr[i] = Double.Parse(t[i].Text);
}
}
If it's just the large amount of source code taken up with your if statements, you could opt for something like:
tr1 = ( t1.Text == "") ? 0 : Double.Parse( t1.Text);
tr2 = ( t2.Text == "") ? 0 : Double.Parse( t2.Text);
:
tr15 = (t15.Text == "") ? 0 : Double.Parse(t15.Text);
This is nice and neat, doesn't take up a lot of screen real-estate and it's fairly easy to see the intent.
Or, better yet, something like:
tr1 = 0; try { tr1 = Double.Parse( t1.Text); } catch (Exception e) {};
tr2 = 0; try { tr2 = Double.Parse( t2.Text); } catch (Exception e) {};
:
tr15 = 0; try { tr15 = Double.Parse(t15.Text); } catch (Exception e) {};
because the fields could be invalid and non-blank.
You can do the same thing with arrays and a for loop if you structure your data and controls differently but it may not be necessary for only fifteen items. Certainly if you were to add more, I'd give serious consideration to that option.
And you may want to load the values directly into an array so that you don't need sch:
double tr[15];
:
tr[ 0] = 0; try { tr[ 0] = Double.Parse( t1.Text); } catch (Exception e) {};
tr[ 1] = 0; try { tr[ 1] = Double.Parse( t2.Text); } catch (Exception e) {};
:
tr[14] = 0; try { tr[14] = Double.Parse(t15.Text); } catch (Exception e) {};
:
double total = 0;
double sorf = 0;
for (int i = 0; i < 15; i++) {
if (tr[i] > 0) {
sorf = sorf + tr[i];
total++;
}
}
:
For a minimal code solution, you can also create an array of the text boxes that you're pulling the information from. Something like (untested):
TextBox t[] = {t1, t2, t3, ..., t15};
double tr[t.length];
:
for (int i = 0; i < t.length; i++) {
tr[i] = 0; try { tr[i] = Double.Parse(t[i].Text); } catch (Exception e) {};
}
:
double total = 0;
double sorf = 0;
for (int i = 0; i < tr.length; i++) {
if (tr[i] > 0) {
sorf = sorf + tr[i];
total++;
}
}
:
Write a function that converts a textbox value to a double, something like:
private static double ConvertTextboxValueToDouble(string value)
{
double result;
Double.TryParse(value, out result);
return result;
}
Then create an array from your textboxes, converting their values to doubles:
double[] values =
{
ConvertTextboxValueToDouble(t1.text),
ConvertTextboxValueToDouble(t2.text),
ConvertTextboxValueToDouble(t3.text),
...
ConvertTextboxValueToDouble(t15.text)
}
Have you considered of using a NumericUpDown instead of an TextBox?
Also instead of writing something fifteen times you should really refactor your code and try one of the following ways:
Register for all your entry boxes an event, where all using the same code
public void ValueChanged(Object sender, EventArgs e)
{
var numericUpDown = sender as NumericUpDown;
if(numericUpDown == null)
return;
//ToDo: Put some check code here
}
Use some List<T> where you put all your boxes into and iterate over it to check all settings
var myList = new List<NumericUpDown>();
//ToDo: Put all your boxes into it
myList.Add(numericUpDown1);
myList.Add(numericUpDown2);
//or get the list from somewhere else
myList.AddRange(this.Controls.OfType<NumericUpDown>())
//OnButtonClick
foreach(var numericUpDown in myList)
{
//ToDo: Do some checking
}
Put a .Trim() when retrieving the values from the TextBox
tr3 = Double.Parse(t3.Text.Trim());
For this situations and thinking to accomplish the job in a little portion of code, i use a dirty little trick: put the controls into a panel.
If your panel is only containing desired controls (in this case, textboxes), this will be enough to store the values in a list of doubles:
private void button1_Click(object sender, EventArgs e)
{
List<double> doubleList = new List<double>();
foreach (TextBox t in panel1.Controls)
doubleList.Add(this.checkTextBox(t));
}
private double checkTextBox(TextBox t)
{
return (t.Text != string.Empty) ? Double.Parse(t.Text.Trim()) : 0;
}
If you can't have a panel only for textboxes and the design force you to mix controls into, you will have to do an extra check/conversion:
private void button1_Click(object sender, EventArgs e)
{
List<double> doubleList = new List<double>();
foreach (Control t in panel1.Controls)
if(t is TextBox)
doubleList.Add(this.checkTextBox((TextBox)t));
}
private double checkTextBox(TextBox t)
{
return (t.Text != string.Empty) ? Double.Parse(t.Text.Trim()) : 0;
}
Greetings!