i'm making my own sudoku game but i don't know where to start.
I've created buttons but i don't know how to group them for creating a conditions
can anyone please help me. this is my project! thank you! I know this is a 9x9 set of boxes but i'm starting at first 9 boxes. this is my code or if anyone have they're own sudoku code please let me know so i can review it. thank you very much.
private void b11_Click(object sender, EventArgs e)
{
b11.Text = tbNum.Text;
Algo = '1';
}
private void b18_Click(object sender, EventArgs e)
{
b18.Text = tbNum.Text;
Algo = '8';
}
private void b17_Click(object sender, EventArgs e)
{
b17.Text = tbNum.Text;
Algo = '7';
}
private void b16_Click(object sender, EventArgs e)
{
b16.Text = tbNum.Text;
Algo = '6';
}
private void b15_Click(object sender, EventArgs e)
{
b15.Text = tbNum.Text;
Algo = '5';
}
private void b14_Click(object sender, EventArgs e)
{
b14.Text = tbNum.Text;
Algo = '4';
}
private void b13_Click(object sender, EventArgs e)
{
b13.Text = tbNum.Text;
Algo = '3';
}
private void b12_Click(object sender, EventArgs e)
{
b12.Text = tbNum.Text;
Algo = '2';
}
private void b19_Click(object sender, EventArgs e)
{
b19.Text = tbNum.Text;
Algo = '9';
}
private void Form1_Load(object sender, EventArgs e)
{
}
Related
I need help with program, where I have a listbox with names and I need to check if there is more than 3 same names in the listbox when I press the buttonControl, the MessegeBox is displayed and it says "There are too many of the same names in the list as allowed". The code is like this for now:
private void buttonVlozjmeno_Click(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
string add_name = textBoxName.Text;
if (index >= 0)
listBox1.Items.Insert(index, add_name);
else
listBox1.Items.Add(add_name);
textBoxJmeno.Text = null;
}
private void buttonADD_Click(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(index);
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("Adam");
listBox1.Items.Add("Adam");
listBox1.Items.Add("Adam");
listBox1.Items.Add("John");
listBox1.Items.Add("John");
listBox1.Items.Add("Eva");
listBox1.Items.Add("John");
listBox1.Items.Add("John");
}
private void buttonControl_Click(object sender, EventArgs e)
{
}
You can try querying the box with a help of Linq, e.g. for WinForms it can be
using System.Linq;
...
bool tooManySameNames = myBox
.Items
.OfType<String>()
.GroupBy(name => name)
.Any(group => group.Count() > 3);
I have seen a similar answer but it's not in c# so I decided to ask this question.
https://gyazo.com/3ff6efd90fa390cd1f071b693027fcd3 After it reaches that point I want a window to pop up which says "Successfully loaded...". The timer interval I have set is 50 if that helps.
This is the code:
private void button1_Click(object sender, EventArgs e)
{
this.timer1.Start();
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.progressBar1.Increment(1);
}
Consider changing:
private void timer1_Tick(object sender, EventArgs e)
{
this.progressBar1.Increment(1);
}
to:
private void timer1_Tick(object sender, EventArgs e)
{
var before = this.progressBar1.Value;
this.progressBar1.Increment(1);
var after = this.progressBar1.Value;
if (after > before && after == this.progressBar1.Maximum)
{
MessageBox.Show("Successfully loaded...");
}
}
By checking whether the value changed and that the current value is Maximum, you know that the progress bar has finished.
Simply check in your code if Value has reached Maximum after a step is performed:
progressBar.PerformStep();
if (progressBar.Value == progressBar.Maximum)
MessageBox.Show("Successfully loaded...");
Using a BackgroundWorker instance:
BackgroundWorker bgw = new BackgroudWorker();
bgw.DoWork += bgw_DoWork;
bgw.ProgressChanged += bgw_ProgressChanged;
bgw.RunWorkerCompleted += bgw_RunWorkerCompleted;
bgw.WorkerReportsProgress = true;
bgw.RunWorkerAsync();
private void bgw_DoWork(Object sender, DoWorkEventArgs e)
{
Int32 total = 147;
for (Int32 i = 0; i < total; ++i)
{
Int32 progress = (i * 100) / total;
bgw.ReportProgress(progress, i);
}
}
private void bgw_ProgressChanged(Object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
private void bgw_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Successfully loaded...");
}
EDIT
Since the question changed providing a code snipped, here is an updated answer (that, by the way, reflects my first proposal):
private void timer1_Tick(object sender, EventArgs e)
{
this.progressBar1.Increment(1);
if (this.progressBar1.Value == this.progressBar1.Maximum)
{
//...
MessageBox.Show("Successfully loaded...");
}
}
my problem is that i'm trying to have a textbox display the number of times that I have clicked on the screen. I'm having the issue of not being able to take the textbox's text and convert it to a int. Thanks!
{
public partial class Form1 : Form
{
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
button1.Location = new Point(rand.Next(0, 750), rand.Next(0, 750));
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
{
int mouseclick = 0;
textBox1.Text = Int32.Parse(mouseclick);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Try the following changes:
{
public partial class Form1 : Form
{
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
button1.Location = new Point(rand.Next(0, 750), rand.Next(0, 750));
}
int mouseclick = 0;
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
{
mouseclick++;
}
textBox1.Text = mouseclick.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
I have moved mouseclick out of the event so that it can maintain its value and i am only incrementing mouseclick if the user clicks with the left mouse button.
The textbox datatype is a string, but its assignment is a number being parsed as a number. Perhaps the following example would be helpful.
string mouseclick = "0";
int intMouseClick = Int32.Parse(mouseclick);
textBox1.Text = intMouseClick.ToString();
You tried to convert an int into string bei using Int32.Parse(...).
But this function does the reverse conversion. It converts a string to an int.
First off, you are telling the program to set mouseclicks to 0 every single time it is clicked. You might instead want the code to read:
if (e.Button != MouseButtons.Right)
{
int mouseclick = mouseclick + 1;
textBox1.Text = Int32.Parse(mouseclick);
}
or move the int out of the if statement and just do mouseclick++;
On top of that, if you are trying to set the text boxes text, you can do it much easier by just doing this:
textBox1.Text = mouseclick.ToString();
So I have been at this for a while now and still cannot get my calculator to do one final thing.
I got it thanks!
After I push the equal button or Tan, Sin, Cos or Mod buttons I want my calculator to take a new number. Basically as if nothing was in the textbox even though the answer is still there. Currently, all the numbers stay and the new number is added to the end. I do not want this to happen.
After the calculation, if I push a number button I want it to clear the screen and add the new number fresh. Below is my code that I have so far.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double FirstNumber;
string mathOperator = "";
private void AddButton_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
mathOperator = "+";
DisplayTextBox.Text = "";
DisplayTextBox.Focus();
}
private void EqualButton_Click(object sender, EventArgs e)
{
Double SecondNumber;
SecondNumber = Convert.ToDouble(DisplayTextBox.Text);
switch (mathOperator)
{
case "+":
DisplayTextBox.Text = (FirstNumber + SecondNumber).ToString();
break;
case "-":
DisplayTextBox.Text = (FirstNumber - SecondNumber).ToString();
break;
case "*":
DisplayTextBox.Text = (FirstNumber * SecondNumber).ToString();
break;
case "/":
DisplayTextBox.Text = (FirstNumber / SecondNumber).ToString();
break;
default:
break;
}
}
private void button12_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
FirstNumber *= -1;
DisplayTextBox.Text = FirstNumber.ToString();
}
private void ButtonMinus_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
mathOperator = "-";
DisplayTextBox.Text = "";
DisplayTextBox.Focus();
}
private void ButtonMultiply_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
mathOperator = "*";
DisplayTextBox.Text = "";
DisplayTextBox.Focus();
}
private void ButtonDivide_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
mathOperator = "/";
DisplayTextBox.Text = "";
DisplayTextBox.Focus();
}
private void ButtonMod_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
DisplayTextBox.Text = Math.Tan(FirstNumber).ToString();
}
private void Button1_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
private void Button2_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "2";
}
private void Button3_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "3";
}
private void Button4_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "4";
}
private void Button5_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "5";
}
private void Button6_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "6";
}
private void Button7_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "7";
}
private void Button8_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "8";
}
private void Button9_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "9";
}
private void ButtonClear_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = "";
mathOperator = "";
}
private void ButtonOff_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void ButtonTan_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
DisplayTextBox.Text = Math.Tan(FirstNumber).ToString();
}
private void ButtonSin_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
DisplayTextBox.Text = Math.Sin(FirstNumber).ToString();
}
private void ButtonCos_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
DisplayTextBox.Text = Math.Cos(FirstNumber).ToString();
}
private void Button0_Click(object sender, EventArgs e)
{
if (DisplayTextBox.Text.Length >= 1)
{
DisplayTextBox.Text = DisplayTextBox.Text + "0";
}
else
{
return;
}
}
private void DecimalButton_Click(object sender, EventArgs e)
{
if (DisplayTextBox.Text.Contains("."))
{
return;
}
else
{
DisplayTextBox.Text = DisplayTextBox.Text + ".";
}
}
}
Add a Boolean variable to your class that represents a flag of whether or not an operation has just completed or not, like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double FirstNumber;
string mathOperator = "";
bool operationJustCompleted = false;
Initially, the value will be false, because nothing has happened when the calculator is first created.
Now, at the end of the equal button event handler, set the flag to true, like this:
private void EqualButton_Click(object sender, EventArgs e)
{
// Logic for calculation
operationJustCompleted = true;
}
Finally, in the event handlers for the number buttons, check to see if the operationJustCompleted flag is true, if so then clear the text and reset the operation just completed flag back to false; like this:
private void Button1_Click(object sender, EventArgs e)
{
if(operationJustCompleted)
{
DisplayTextBox.Text = String.Empty;
operationJustCompleted = false;
}
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
Add a state variable to your class. When true, and a number is selected, clear the display and set it false before writing the number.
When "=" is clicked, set it true.
Based on a brief review, seems like you need to introduce a variable that will keep track of the calculator’s state, and then when “equal” button is pressed, set the state to calculated value. Then, in the numeric buttons (0-9) you need to check the state variable and either append to or replace the display text.
Create this global variable...
private bool equationComplete = false;
Add this to the end of the Equals button...
equationComplete = true;
Add this to the beginning of each number button click...
if (equationComplete) DisplayTextBox.Text = "";
Add this to the end of each number button click event handler...
equationComplete = false;
Just like David Arno says, something like this:
private void EqualButton_Click(object sender, EventArgs e)
{
ClearDisplayBeforeNextTextEntry = true;
}
private void Button1_Click(object sender, EventArgs e)
{
// New code
ClearText();
// Old code
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
// Same for all other number buttons as above
private void ClearText()
{
if (ClearDisplayBeforeNextTextEntry)
{
DisplayTextBox.Text = "";
ClearDisplayBeforeNextTextEntry = false;
}
}
So I'm working on a calculator application in C-Sharp and I want to prevent people from entering more than 1 period/point at once. So they can't type "....." or "1..1" or "1.1.1"
Really just stuff like that.... I would also like to prevent them from adding alphabetical characters in by typing them in with the keyboard, characters like "a, b, c".
I was told to use a MaskedTextBox, and I want to know if that is correct. Also, if it is correct, how would I implement it into my code? I'm a complete beginner when it comes to C#, so I would like some help (toned down for a beginner).
So far the code I have written is:
double total1 = 0;
double total2 = 0;
public Form1()
{
InitializeComponent();
}
private void btnOne_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnOne.Text;
}
private void btnTwo_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnTwo.Text;
}
private void btnThree_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnThree.Text;
}
private void btnFour_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnFour.Text;
}
private void btnFive_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnFive.Text;
}
private void btnSix_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnSix.Text;
}
private void btnSeven_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnSeven.Text;
}
private void btnEight_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnEight.Text;
}
private void btnNine_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnNine.Text;
}
private void btnZero_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnZero.Text;
}
private void btnClear_Click(object sender, EventArgs e)
{
txtDisplay.Clear();
}
private void btnPlus_Click(object sender, EventArgs e)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
}
private void btnEquals_Click(object sender, EventArgs e)
{
total2 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Text = total2.ToString();
total1 = 0;
}
private void btnPoint_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnPoint.Text;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void txtDisplay_TextChanged(object sender, EventArgs e)
{
}
}
So I ask... how/where do I add in this "MaskedTextBox" - if that is correct? How do I implement it? What makes it work?
Thanks!
You don't need a MaskedTextBox, since you're simulating the keypad with your buttons. Just put something like this in btnPoint_Click:
private void btnPoint_Click(object sender, EventArgs e)
{
if (!txtDisplay.Text.Contains("."))
{
txtDisplay.Text = txtDisplay.Text + btnPoint.Text;
}
}
In addition to MusiGenesis's snippet, you can use the KeyPress event of the text box to prevent non-digits and multiple periods.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(char.IsDigit(e.KeyChar) || ((e.KeyChar == '.' && textBox1.Text.IndexOf(".") < 0) ) )
{
textBox1.Text += e.KeyChar;
}
e.Handled = true;
}
or set ReadOnly property of the text box to true.
You can also save a big amount of code if you write the code of the click event just once and asign the click event handlers of every number buttons to it:
private void btnNumber_Click(object sender, EventArgs e)
{
if (sender is Button)
txtDisplay.Text = txtDisplay.Text + ((Button)sender).Text;
}
So you can save all of the following methods
private void btnZero_Click(object sender, EventArgs e) (...)
private void btnOne_Click(object sender, EventArgs e) (...)
.
.
.
private void btnNine_Click(object sender, EventArgs e) (...)