code improvement - naming textboxes generated form numericupdown & button click - c#

I have a problem with a code that is regarding adding controls with numericUpDown ( for example- if numericUpDown value equals to 3, user recievs 3 textboxes).
Thanks to stackoverflow users I improved my code.
Before improvement it looked like this:
private void button1_Click(object sender, EventArgs e)
{
if (numericUpDown1.Value == 1)
{
txtbx1.AutoSize = true;
Controls.Add(txtbx1);
txtbx1.Location = new Point(70, 100);
}
else if (numericUpDown1.Value == 2)
{
txtbx1.AutoSize = true;
Controls.Add(txtbx1);
txtbx1.Location = new Point(70, 100);
txtbx2.AutoSize = true;
Controls.Add(txtbx2);
txtbx2.Location = new Point(70, 130);
}
else if (numericUpDown1.Value == 3)
{
txtbx1.AutoSize = true;
Controls.Add(txtbx1);
txtbx1.Location = new Point(70, 100);
txtbx2.AutoSize = true;
Controls.Add(txtbx2);
txtbx2.Location = new Point(70, 130);
txtx3.AutoSize = true;
Controls.Add(txtbx3);
txtbx3.Location = new Point(70, 160);
}
}
After improvement:
private void button1_Click(object sender, EventArgs e)
{
int y = 100;
int x = 70;
for (int i = 0; i < numericUpDown1.Value; i++)
{
var txtbx = new TextBox();
txtbx.AutoSize = true;
Controls.Add(txtbx);
txtbx.Location = new Point(x, y);
// Increase the y-position for next textbox.
y += 30;
}
}
Now the problem is that I don't know how assign names to genarated textboxes.
(before the improvement I could name them - txtbx1, txtbx2, txtbx3...)
Code to improve:
private void button3_Click(object sender, EventArgs e)
{
try
{
double a, b, c, sum;
a = double.Parse(txtbx1.Text);
b = double.Parse(txtbx2.Text);
c = double.Parse(txtbx3.Text);
sum = a + b + c;
label1.Text = sum.ToString();
}
catch (Exception ex)
{
MessageBox.Show("error");
}
Please note that I'm a beginner, learning c# by watching youtube tutorials ;) I do realize that my question might be silly but I couldn't handle this problem by myself.
In advance thank you for your time and help.

If you need to access them afterwards, you have some options.
I'll guess that your objective is to set label1's text to the sum of the values contained in the specified textbox(es).
On the ValueChanged event of your NumericUpDown, check the delta and consequentely add or remove the required number of TextBoxes to your Form's Controls. To obtain the delta, you'll need to store the previous value of the NumericUpDown, and then subtract it from the current value. (If it was 5, and now it's 4, 4 - 5 = -1. A textbox has been removed).
private int _oldNUDvalue = 0; //or assign it to the default value
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
int delta = (int)numericUpDown1.Value - _oldNUDvalue;
if (delta < 0)
{
for (int i = -delta; i > 0; i--)
{
var tbox = Controls.Find("ntextBox" + (_oldNUDvalue - i), false)[0];
Controls.Remove(tbox);
}
}
else if (delta > 0)
{
for (int i = _oldNUDvalue; i < _oldNUDvalue + delta; i++)
{
var tbox = new TextBox();
tbox.Location = new Point(15, 55 + (30 * i));
tbox.Name = "ntextBox" + i;
Controls.Add(tbox);
}
}
_oldNUDvalue = (int)numericUpDown1.Value;
}
If, however, you only have a maximum number of 3, you could take a slightly different approach. My solution works with n-textboxes.
Finally, to get the TextBoxes' values from code, you have three approaches:
Loop through your Form's controls, check for TextBoxes with their name starting with "ntextBox", and add their values together
Use LINQ to do the same
Access them singularly via "Controls.Find("ntextBoxX", false)", where X is the number of course.
I'll show the LINQ approach as I like it better.
int sum = Controls.Cast<Control>().Sum(c => c.Name.StartsWith("ntextBox") ? int.Parse(c.Text) : 0);
I haven't tested the code, but it should work. Tell me if there are any problems.
EDIT: Tested and it works. For the sake of completeness, I'll add some event logic to the TextBox-adding loop, to make sure that their input is actually numeric.
tbox.TextChanged += HandleNTextBoxInput; // add this line
And elsewhere, add this method:
void HandleNTextBoxInput(object sender, EventArgs e)
{
string text = ((TextBox)sender).Text;
if (!Regex.IsMatch(text, "^[1-9][0-9]*$")) //Text is NOT numeric. Remove [1-9] if you want to keep leading zeros.
{
//Set the Text to 0, for example. Or show a message box. Or whatever.
((TextBox)sender).Text = "0";
}
}

As I mentioned in a comment- this code seems to be maybe too advanced for me.
I have no problem with adding the controls, bu still there is a problem how to get the sum from a button click to a textbox.
I probably made some simple mistakes, or something is missing but I really don't know how to fix this problem.
My code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace testprogram
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) // I'm guessing something is missing over here
{
int sum = Controls.Cast<Control>().Sum(c => c.Name.StartsWith("ntextBox") ? int.Parse(c.Text) : 0);
}
void HandleNTextBoxInput(object sender, EventArgs e)
{
string text = ((TextBox)sender).Text;
if (!Regex.IsMatch(text, "^[1-9][0-9]*$")) //Text is NOT numeric. Remove [1-9] if you want to keep leading zeros.
{
//Set the Text to 0, for example. Or show a message box. Or whatever.
((TextBox)sender).Text = "0";
}
}
private int _oldNUDvalue = 0; //or assign it to the default value
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
{
int delta = (int)numericUpDown1.Value - _oldNUDvalue;
if (delta < 0)
{
for (int i = -delta; i > 0; i--)
{
var tbox = Controls.Find("ntextBox" + (_oldNUDvalue - i), false)[0];
Controls.Remove(tbox);
}
}
else if (delta > 0)
{
for (int i = _oldNUDvalue; i < _oldNUDvalue + delta; i++)
{
var tbox = new TextBox();
tbox.Location = new Point(15, 55 + (30 * i));
tbox.Name = "ntextBox" + i;
tbox.TextChanged += HandleNTextBoxInput;
Controls.Add(tbox);
}
}
_oldNUDvalue = (int)numericUpDown1.Value;
}
}
}
}

Related

How to hide all items in a private void method you've created?

so I created my own private void that can create buttons
private void addButtonsToForm()
{
for (int i = 0; i < 26; i++)
{
Button currentNewButton = new Button();
currentNewButton.Size = new Size(20, 30);
currentNewButton.Location = new Point(20 + 25 * i, 420);
currentNewButton.Text = ((char)(65 + i)).ToString();
currentNewButton.Click += LetterClicked;
letters[i] = currentNewButton;
this.Controls.Add(letters[i]);
}
}
The buttons are alphabets and will be accessed when the user wants to choose a letter ... but the problem is I'm trying to figure out how to go back when the user clicked or selected a button..
Originally I wanted to do was i could just hide all the buttons created and just make the previous button visible but for some reason it only hides the only button that is clicked
//this is under private void LetterClicked(object sender, EventArgs e)
Button selectedLetter = (Button)sender;
selectedLetter.Enabled = false;
i thought of stupid codes like
addbuttonstoform().visible = false; but of course that won't work.. but you might get an idea to where i want to go.... it's a bit confusing to explain... I'm new in c# and i'm creating a guess the word game so help could be great..
Here is a working solution for your problem. See below:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Alphabets
{
public partial class Form1 : Form
{
private Button[] _letters;
public Form1()
{
InitializeComponent();
AddButtonsToForm();
}
private void AddButtonsToForm()
{
_letters = new Button[26];
for (int i = 0; i < 26; i++)
{
Button currentNewButton = new Button
{
Name = "BtnLetter"+ ((char)(65 + i)),
Size = new Size(20, 30),
Location = new Point(20 + 25 * i, 420),
Text = ((char) (65 + i)).ToString()
};
currentNewButton.Click += LetterClicked;
_letters[i] = currentNewButton;
this.Controls.Add(_letters[i]);
}
}
private void LetterClicked(object sender, EventArgs e)
{
var selectedLetter = (Button) sender;
//hide all other buttons
foreach (var letter in _letters)
{
if (letter.Text != selectedLetter.Text)
{
var buttons = this.Controls.Find("BtnLetter" + letter.Text, true);
buttons[0].Enabled = false;
}
}
}
}
}

Creating a game of Checkers

I am creating this in regards to an issue I just ran in with. I am trying to create a primitive game of checkers using only buttons and so far I am just testing how to get the program to recognize selecting a button and moving the piece.
My Code:
private void Checkers_Load(object sender, EventArgs e)
{
}
string selectedChecker = "";
string currentButton = "";
int blankSpace = 0;
int[] Board = new int[64];
private void gameBoard()
{
foreach(var control in Controls)
{
var button = control as Button;
if(button != null)
{
if(button.Name == currentButton)
{
button.Image = System.Drawing.Image.FromFile("Red Checker.png");
}
else
{
MessageBox.Show("Dead.");
}
}
else
{
MessageBox.Show("Dead.");
}
}
}
private void attemptMove()
{
string substringChecker = null;
substringChecker = selectedChecker.Substring(0,2);
int selectedCheckerNumber = Convert.ToInt32(substringChecker);
string substringButton = null;
substringButton = currentButton.Substring(0,2);
int currentButtonNumber = Convert.ToInt32(substringButton);
if((selectedCheckerNumber + 3 == currentButtonNumber) || (selectedCheckerNumber + 4 == currentButtonNumber))
{
Board[currentButtonNumber] = Board[selectedCheckerNumber];
Board[selectedCheckerNumber] = blankSpace;
gameBoard();
}
}
private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
{
int w = pictureBox1.Size.Width;
int h = pictureBox1.Size.Height;
int count = 8;
Bitmap b = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
w /= count;
h /= count;
Graphics g = Graphics.FromImage(b);
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count; j++)
{
Color c = (i + j) % 2 == 0 ? Color.Red : Color.Black;
Brush br = new SolidBrush(c);
g.FillRectangle(br, i * w, j * h, w, h);
br.Dispose();
}
}
g.Dispose();
pictureBox1.Image = b;
pictureBox1.Refresh();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
//Close form
this.Close();
}
private void howToPlayToolStripMenuItem_Click(object sender, EventArgs e)
{
//MessageBox.Show("How to play a game of Checkers: Step 1 - Don't play.");
}
private void informationToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void checkerSpace18_Click(object sender, EventArgs e)
{
}
private void checkerSpace17_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
string buttonClicked = btn.Name;
if (selectedChecker == "")
{
selectedChecker = buttonClicked;
}
else
{
currentButton = buttonClicked;
attemptMove();
}
}
}
My question goes with regards to my attemptMove() method. I run into a runtime error:
"An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll. Additional information: Input string was not in a correct format."
It seems to occur whenever I go to move the piece to the other cell adjacent to the piece after moving it once. It may be because my program is not updating the pictures of the buttons when the buttons switch, but I wanted to see if any guidance could be shed upon this issue.
You are setting the "selectedChecker" to the clicked button .Name property. Then you are using substring(0,2) to get the first 2 characters and try to convert that to an integer.
Since a Name property can't start with numbers, you are attempting to convert some random text to an integer property.
A better approach would be to use the Tag property to hold information about the number, this can be any value. You can then use the int.Parse to parse the Tag value of the button.
What is your naming convention for the buttons? Seems like you're trying to get a number from the name, but the conversion is failing.
If the button names are button00 through button63, then you should be able to get the number using Substring, starting at Name.Length - 2. Note that you must have all button names use a two digit number at the end (00, 01, 02, etc).
For example:
// Get the last two characters of the button name
string substringChecker = selectedChecker.Substring(selectedChecker.Length - 2);
// Convert the characters to an integer
int selectedCheckerNumber = Convert.ToInt32(substringChecker);

Array of buttons: change property

I have an array of buttons, like this:
int x = 0, y = 0;
butt2 = new Button[100];
for (int i = 0; i < 100; i++)
{
butt2[i] = new Button();
int names = i;
butt2[i].Name = "b2" + names.ToString();
butt2[i].Location = new Point(525 + (x * 31), 70 + (y * 21));
butt2[i].Visible = true;
butt2[i].Size = new Size(30, 20);
butt2[i].Click += new EventHandler(butt2_2_Click); //problem lies here (1)
this.Controls.Add(butt2[i]);
}
private void butt2_2_Click(object sender, EventArgs e)
{
// want code here
}
I want to change the back color of the button when clicked. I was thinking of passing i to be able to do this:
butt2[i].BackColor = Color.Green;
This should do the trick:
private void butt2_2_Click(object sender, EventArgs e)
{
Button pushedBtn = sender as Button;
if(pushedBtn != null)
{
pushedBtn.BackColor = Color.Green;
}
}
And this holds for most UI events, the 'object sender' parameter refers to the control that 'sent'/'fired' the event.
To learn more about C# event handling, I would start here.
Also, here is a SO question about GUI event handling, answered nicely by Juliet (accepted answer).
Hope this helps.

Form Updating while Calculation Runs

Started learning to code.
Have a console writing a 10 by ten grid of numbers randomly increasing in value.
Tried to do same in a form (DataGridView).
It works fine but there is no window until calcs are finished (I had to put a limit in - instead of an infinite loop).
I came here and read about refresh and doevent - but they dramatically slow everything down. But at least I can see the calcs happening.
I've tried to get my head around backgroundworker and I'm afraid I can't. If I'm in a loop to calculate - how do I separate those calcs from a screen update.
EDIT : Followed Nico's help(Thanks!!) and got this. Much better, but still laggy with big numbers. But a rocket with small numbers.
Any help to make faster?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace stackoverflowTest
{
public partial class Form1 : Form
{
Random rnd = new Random((Int32)DateTime.Now.Ticks);
private static int numSides = 8;
int numDice=2;
private int numRolls = 100000000;
private int max = 1;
private int min = 0;
private int diff = 0;
private double pdiff = 0d;
int[] array = new int[numSides *numSides];
public Form1()
{
InitializeComponent();
}
private void Form1Load(object sender, EventArgs e)
{
SetupDataForm1();
SetupDataForm2();
var bw = new BackgroundWorker();
bw.DoWork += BwDoWork;
bw.RunWorkerAsync(); //Start the worker
}
private void BwDoWork(object sender, DoWorkEventArgs e)
{
for (int rolls = 1; rolls < numRolls+1; rolls++)
{
// Roll two dice and increase that slot in the table
int y = Dice.Roll(numSides, rnd);
int x = Dice.Roll(numSides, rnd);
int k = Convert.ToInt32(dataGridView1.Rows[x].Cells[y].Value);
dataGridView1.Rows[x].Cells[y].Value = k + 1;
//Enter table into an array to work out max/min etc later
for (int i = 0; i < (numSides * numSides); i++)
{
int row = i / numSides;
int col = i % numSides;
array[i] = Convert.ToInt32(dataGridView1.Rows[row].Cells[col].Value);
}
max = array.Max();
min = array.Min();
diff = max - min;
if (max > 0) pdiff = (((double)diff / (max)) * 100);
dataGridView2.Rows[0].Cells[0].Value = rolls;
dataGridView2.Rows[1].Cells[0].Value = max;
dataGridView2.Rows[2].Cells[0].Value = min;
dataGridView2.Rows[3].Cells[0].Value = diff;
dataGridView2.Rows[4].Cells[0].Value = pdiff.ToString("0.000");
dataGridView2.Rows[5].Cells[0].Value = (array.Average()).ToString("0");
dataGridView2.Rows[6].Cells[0].Value = ((array.Average()/rolls)*100).ToString(("0.0000000"));
}
}
private void SetupDataForm1()
{
dataGridView1.Font = new Font("Microsoft Sans Serif", 6F);
dataGridView1.RowTemplate.Height = 11;
//Add Columns
for (int i = 0; i < numSides; i++)
{
dataGridView1.Columns.Add(i.ToString(), (i+1).ToString());
dataGridView1.Columns[i].Width = 35;
}
// Add Rows
for (int i = 0; i < numSides; i++)
{
dataGridView1.Rows.Add();
if (i % 2 != 0)
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightGray;
}
dataGridView1.Rows[i].HeaderCell.Value = ((i+1)*10).ToString();
}
}
private void SetupDataForm2()
{
dataGridView2.Font = new Font("Microsoft Sans Serif", 8F);
dataGridView2.RowTemplate.Height = 16;
//Add Columns
for (int i = 0; i < 1; i++)
{
dataGridView2.Columns.Add(i.ToString(), "");
dataGridView2.Columns[i].Width = 65;
}
// Add Rows
for (int i = 0; i < numSides; i++)
{
dataGridView2.Rows.Add();
if (i % 2 != 0)
{
dataGridView2.Rows[i].DefaultCellStyle.BackColor = Color.LightGray;
}
}
dataGridView2.Rows[0].HeaderCell.Value = "Rolls";
dataGridView2.Rows[1].HeaderCell.Value = "Max";
dataGridView2.Rows[2].HeaderCell.Value = "Min";
dataGridView2.Rows[3].HeaderCell.Value = "Diff";
dataGridView2.Rows[4].HeaderCell.Value = "%";
dataGridView2.Rows[5].HeaderCell.Value = "Avg";
dataGridView2.Rows[6].HeaderCell.Value = "Av%";
}
public class Dice
{
public static int Roll(int numberOfSides, Random rnd)
{
return rnd.Next(0, numberOfSides);
}
}
}
}
If there is no window, you perform the calculations in the form's constructor. To make the form visible before starting the calculation, put the code in the form's Load event. Therefore double click the event in the form's properties window and a method will be created. Put your code into this method.
If you want to use a background worker, the procedure is similar. However, you need to create the Backgroundworker. E.g. in code:
private void Form1_Load(object sender, EventArgs e)
{
var bw = new BackgroundWorker();
bw.DoWork +=
If you start typing that, Visual Studio suggests to create a method for the event. Press Tab twice to generate it and you'll basically get the following code:
private void Form1_Load(object sender, EventArgs e)
{
var bw = new BackgroundWorker();
bw.DoWork += bw_DoWork;
bw.RunWorkerAsync(); //Start the worker
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
throw new NotImplementedException(); //remove this
}
Put your calculation code in the bw_DoWork method an it will be executed in the background without affecting the user interface.

displaying line number in rich text box c#

I have a Multiline richtextbox control into which i want to integrate the feature of adding a line number. i have considered many approaches
Add a label and updating the line numbers as the line count changes
Add a picturebox along with to draw string on it.
Add another textbox along with and show line numbers on it
Add listbox along and display line numbers in it.
I got two doubts.
The richtextbox which i'm using is a custom made control and derieves from RichTextBox class. How can i add multiple controls to it.
What is the best approach to show line numbers for the multiline text in c#
My own example. All is fine, but wordwrap must be disabled :(
int maxLC = 1; //maxLineCount - should be public
private void rTB_KeyUp(object sender, KeyEventArgs e)
{
int linecount = rTB.GetLineFromCharIndex( rTB.TextLength ) + 1;
if (linecount != maxLC)
{
tB_line.Clear();
for (int i = 1; i < linecount+1; i++)
{
tB_line.AppendText(Convert.ToString(i) + "\n");
}
maxLC = linecount;
}
}
where rTB is my richtextbox and tB is textBox next to rTB
J.T. jr
this code helped me thank you, needed to convert visual basic but could:
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
Dim maxlc As Integer = 1
Dim linecount As Integer = TextBox1.GetLineFromCharIndex(TextBox1.Height) + 1
If linecount <> maxlc Then
TextBox2.Clear()
For i = 0 To linecount - 1 Step 1
TextBox2.AppendText(Convert.ToString(i) + vbNewLine)
Next i
maxlc = linecount
End If
End Sub
public int getWidth()
{
int w = 25;
// get total lines of richTextBox1
int line = richTextBox1.Lines.Length;
if (line <= 99)
{
w = 20 + (int)richTextBox1.Font.Size;
}
else if (line <= 999)
{
w = 30 + (int)richTextBox1.Font.Size;
}
else
{
w = 50 + (int)richTextBox1.Font.Size;
}
return w;
}
public void AddLineNumbers()
{
// create & set Point pt to (0,0)
Point pt = new Point(0, 0);
// get First Index & First Line from richTextBox1
int First_Index = richTextBox1.GetCharIndexFromPosition(pt);
int First_Line = richTextBox1.GetLineFromCharIndex(First_Index);
// set X & Y coordinates of Point pt to ClientRectangle Width & Height respectively
pt.X = ClientRectangle.Width;
pt.Y = ClientRectangle.Height;
// get Last Index & Last Line from richTextBox1
int Last_Index = richTextBox1.GetCharIndexFromPosition(pt);
int Last_Line = richTextBox1.GetLineFromCharIndex(Last_Index);
// set Center alignment to LineNumberTextBox
LineNumberTextBox.SelectionAlignment = HorizontalAlignment.Center;
// set LineNumberTextBox text to null & width to getWidth() function value
LineNumberTextBox.Text = "";
LineNumberTextBox.Width = getWidth();
// now add each line number to LineNumberTextBox upto last line
for (int i = First_Line; i <= Last_Line + 2; i++)
{
LineNumberTextBox.Text += i + 1 + "\n";
}
}
private void Form1_Load(object sender, EventArgs e)
{
LineNumberTextBox.Font = richTextBox1.Font;
richTextBox1.Select();
AddLineNumbers();
}
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
Point pt = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart);
if (pt.X == 1)
{
AddLineNumbers();
}
}
private void richTextBox1_VScroll(object sender, EventArgs e)
{
LineNumberTextBox.Text = "";
AddLineNumbers();
LineNumberTextBox.Invalidate();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
if (richTextBox1.Text == "")
{
AddLineNumbers();
}
}
private void richTextBox1_FontChanged(object sender, EventArgs e)
{
LineNumberTextBox.Font = richTextBox1.Font;
richTextBox1.Select();
AddLineNumbers();
}
private void LineNumberTextBox_MouseDown(object sender, MouseEventArgs e)
{
richTextBox1.Select();
LineNumberTextBox.DeselectAll();
}
private void Form1_Resize(object sender, EventArgs e)
{
AddLineNumbers();
}
WORKS 100%!!! But you need to add richTextBox2 for line numbers, if you want change it to other
form like listbox, anyway it served me well.
private void richTextBox1_keyDown(object sender, KeyEventArgs e)
{
for (int i = 0; i <= richTextBox1.Lines.Count(); i++)
{
if (!(e.KeyCode == Keys.Back))
{
if (!richTextBox2.Text.Contains(i.ToString()))
{
richTextBox2.Text += i.ToString() + "\n";
}
}
else
{
richTextBox2.Clear();
}
}
}

Categories

Resources