I am having a little problem with validation on my calculator, I would like when I press enter or all other characters except numbers ,it would notify me of an error.
Basically, I would like that when I type a letter or special character with the keyboard I get an error returned
I am using windows form, I attach below my code:
double num1;
double num2;
double result;
String oper = "+";
private void button_minus_Click(object sender, EventArgs e)
{
// label1.Text += minus_click.Text;
num1 = double.Parse(label1.Text);
oper = "-";
label1.Text += oper;
label1.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text += button15.Text;
}
private void button16_Click(object sender, EventArgs e)
{
label1.Text += button16.Text;
}
private void button5_Click(object sender, EventArgs e)
{
label1.Text += button5.Text;
}
private void button4_Click(object sender, EventArgs e)
{
label1.Text += button4.Text;
}
private void button6_Click(object sender, EventArgs e)
{
label1.Text += division_click.Text;
}
private void button4_Click_1(object sender, EventArgs e)
{
}
private void button10_Click(object sender, EventArgs e)
{
}
private void button6_Click_1(object sender, EventArgs e)
{
}
private void button7_Click(object sender, EventArgs e)
{
label1.Text += button7.Text;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button_AC_Click(object sender, EventArgs e)
{
label1.Text = String.Empty;
label3.Text = String.Empty;
}
private void button2_Click_1(object sender, EventArgs e)
{
label1.Text += button16.Text;
}
private void button3_Click_1(object sender, EventArgs e)
{
label1.Text += button17.Text;
}
private void button7_Click_1(object sender, EventArgs e)
{
label1.Text += button7.Text;
}
private void button8_Click(object sender, EventArgs e)
{
label1.Text += button8.Text;
}
private void button9_Click(object sender, EventArgs e)
{
label1.Text += button9.Text;
}
private void button11_Click_1(object sender, EventArgs e)
{
label1.Text += button11.Text;
}
private void button12_Click(object sender, EventArgs e)
{
label1.Text += button12.Text;
}
private void button13_Click(object sender, EventArgs e)
{
label1.Text += button6.Text;
}
private void buttonPoint_Click(object sender, EventArgs e)
{
label1.Text += buttonPoint.Text;
num1 = (double)Convert.ToDecimal(label1.Text);
num2 = (double)Convert.ToDecimal(label1.Text);
}
private void button10_Click_1(object sender, EventArgs e)
{
label1.Text += moltiplication_click.Text;
}
private void button18_Click(object sender, EventArgs e)
{
label1.Text += addiction_click.Text;
}
private void button3_Click(object sender, EventArgs e)
{
label1.Text += button3.Text;
}
private void button5_Click_1(object sender, EventArgs e)
{
label1.Text += button5.Text;
}
private void Form1_Load_1(object sender, EventArgs e)
{
}
private void panel1_Paint_1(object sender, PaintEventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void addiction_click_Click(object sender, EventArgs e)
{
//label1.Text += addiction_click.Text;
num1 = double.Parse(label1.Text);
oper = "+";
label1.Text = string.Empty;
label3.Text = String.Empty;
}
private void moltiplication_click_Click(object sender, EventArgs e)
{
//label1.Text += moltiplication_click.Text;
num1 = double.Parse(label1.Text);
oper = "*";
label1.Text += oper;
label1.Text = "";
label3.Text = String.Empty;
}
private void division_click_Click(object sender, EventArgs e)
{
num1 = double.Parse(label1.Text);
oper = "/";
label1.Text += oper;
label1.Text = "";
label3.Text = String.Empty;
}
private void buttonPIĆ¹OMENO_Click_2(object sender, EventArgs e)
{
num1 = double.Parse(label1.Text);
}
private void buttonPERCENTUAL_Click_2(object sender, EventArgs e)
{
//num1 = (double)Convert.ToDecimal(label1.Text);
oper = "%";
label1.Text += oper;
label3.Text = String.Empty;
}
private void button3_Click_0(object sender, EventArgs e)
{
label1.Text += button3.Text;
}
private void equals_click_Click(object sender, EventArgs e)
{
num2 = Convert.ToInt32(label1.Text);
label1.Text = label1.Text;
switch (oper)
{
case "+":
result = num1 + num2;
label3.Text = Convert.ToString(result);
break;
case "-":
result = num1 - num2;
label3.Text = Convert.ToString(result);
break;
case "*":
result = num1 * num2;
label3.Text = Convert.ToString(result);
break;
case "/":
result = num1 / num2;
label3.Text = Convert.ToString(result);
break;
case "%":
num2 = (double)Convert.ToDecimal(label3.Text);
result = num1 % num2;
label3.Text = result.ToString();
break;
}
label3.Text = result.ToString();
}
private void label1_Click_1(object sender, EventArgs e)
{
}
public bool ValidationEmptyNumber(String s)
{
bool isNumber = true;
while (isNumber)
{
label1.Text = num1.ToString();
label1.Text = num2.ToString();
return isNumber = true;
if (label1.Text == String.Empty)
{
MessageBox.Show("please enter a number");
return isNumber = false;
}
}
}
public bool ValidationOnlyNumbers(String s) {
bool isNumber;
Regex regex = new Regex("[a-zA-Z]+");
Match match = regex.Match("impossible to insert letters or special characters");
while (isNumber = true)
{
label1.Text = num1.ToString();
label1.Text = num2.ToString();
return isNumber;
if (match.Success)
{
label1.Text = match.Value;
MessageBox.Show(match.Value);
}
{
}
}
{
}
}
}
}
Related
I have problem, which consists in aesthetic sense, correctly - There is textBox to which i apply true condition of UseSystemPasswordChar.. It's work! But i get bold points. Try to change font size - decreases textbox's field. Below is the code (although why is it here?). Can anyone help, thank you in advance)
public partial class frmRegistr : Form
{
public frmRegistr()
{
InitializeComponent();
}
int counter = 0;
int a = 0;
string b;
private void frmRegistr_Load(object sender, EventArgs e)
{
b = label1.Text;
a = b.Length;
label1.Text = "";
timer1.Start();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (counter < a)
{
counter++;
label1.Text = b.Substring(0, counter);
}
else
{
timer1.Stop();
}
}
private void label4_Click(object sender, EventArgs e)
{
timer3.Start();
}
private void label4_MouseHover(object sender, EventArgs e)
{
//if this.MouseLeave
label4.BackColor = Color.FromArgb(((int)(((byte)(154)))), ((int)(((byte)(181)))), ((int)(((byte)(101)))));
}
private void timer2_Tick(object sender, EventArgs e)
{
if (Opacity == 1)
{
timer2.Stop();
}
Opacity += .2;
}
private void timer3_Tick(object sender, EventArgs e)
{
if (Opacity <= 0)
{
this.Close();
}
Opacity -= .2;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox2.UseSystemPasswordChar = true;
}
}
}
If you want to define your own password character, use property TextBox.PasswordChar. If you want this in a certain font, use Control.Font
As you only have to do this once, do this in the constructor:
public MyForm : Form
{
InitializeComponents(),
this.textBox1.PasswordChar = '_';
this.textBox11.Font = new Font(...)
};
You can also decide to do this using the visual studio designer.
You can setup this in VisualStudio designer, but this is code:
textBox1.PasswordChar = '*';
//* = password character
Is there a way to cast a string to an interpolation object?
example: $"{912+8/2}"
I've tried to put the raw text from my textbox into the interpolation $"{results.Text}" (in the calculator you can type only numbers and .,/,*,-,+).
Is there a way to let the system make the math?
/// <summary>
/// Logica di interazione per MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//define onclick listener for the numbers
private void b0_Click(object sender, RoutedEventArgs e)
{
results.Text = results.Text + "0";
}
private void b1_Click(object sender, RoutedEventArgs e)
{
results.Text = results.Text + "1";
}
private void b2_Click(object sender, RoutedEventArgs e)
{
results.Text = results.Text + "2";
}
private void b3_Click(object sender, RoutedEventArgs e)
{
results.Text = results.Text + "3";
}
private void b4_Click(object sender, RoutedEventArgs e)
{
results.Text = results.Text + "4";
}
private void b5_Click(object sender, RoutedEventArgs e)
{
results.Text = results.Text + "5";
}
private void b6_Click(object sender, RoutedEventArgs e)
{
results.Text = results.Text + "6";
}
private void b7_Click(object sender, RoutedEventArgs e)
{
results.Text = results.Text + "7";
}
private void b8_Click(object sender, RoutedEventArgs e)
{
results.Text = results.Text + "8";
}
private void b9_Click(object sender, RoutedEventArgs e)
{
results.Text = results.Text + "9";
}
//define onclick listener for the operations and special char
private void ba_Click(object sender, RoutedEventArgs e)
{
results.Text = results.Text + "+";
}
private void bs_Click(object sender, RoutedEventArgs e)
{
results.Text = results.Text + "-";
}
private void bm_Click(object sender, RoutedEventArgs e)
{
results.Text = results.Text + "*";
}
private void bf_Click(object sender, RoutedEventArgs e)
{
results.Text = results.Text + "/";
}
private void bd_Click(object sender, RoutedEventArgs e)
{
results.Text = results.Text + ".";
}
private void bc_Click(object sender, RoutedEventArgs e)
{
results.Text = "";
}
private void be_Click(object sender, RoutedEventArgs e)
{
results.Text = $"{results.Text}"; //<-- Problem here
}
}
I have used Backgroundworkerclass to update my user interface element (Label). Please! Check my code below. It is working fine. But I am trying to replace Backgroundworkerclass and use simple worker thread to achieve the same goal. How can I achieve that? Any suggestion will be appreciated. Thanks
public void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = e.ProgressPercentage.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
if (!backgroundWorker1.IsBusy)
backgroundWorker1.RunWorkerAsync();
else
label1.Text = "Busy Processing";
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 100; i++)
{
Thread.Sleep(100);
backgroundWorker1.ReportProgress(i);
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
backgroundWorker1.ReportProgress(0);
return;
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = e.ProgressPercentage.ToString();
}
delegate void DelegateDoWork(int number);
private void button1_Click(object sender, EventArgs e)
{
new Thread(DoWork).Start();
}
public void ProgressBar(int i)
{
if (label1.InvokeRequired)
{
var d = new DelegateDoWork(ProgressBar);
this.Invoke(d, i);
}
else
label1.Text = i.ToString();
}
public void DoWork()
{
for (int i = 0; i <= 100; i++)
{
Thread.Sleep(100);
ProgressBar(i);
}
}
I tried to put a label1.text = progressbar1.value.ToString(); and it would not read out how much it has progressed. This is what I have so far with some help.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
progressBar1.Value = s_Checks * (progressBar1.Maximum / TOTAL_CHECKBOXES);
label1.Text = progressBar1.Value.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private const Int32 TOTAL_CHECKBOXES = 4;
private static Int32 s_Checks = 0;
private void OnCheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
++s_Checks;
else
--s_Checks;
progressBar1.Value = s_Checks * (progressBar1.Maximum / TOTAL_CHECKBOXES);
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
++s_Checks;
else
--s_Checks;
progressBar1.Value = s_Checks * (progressBar1.Maximum / TOTAL_CHECKBOXES);
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
++s_Checks;
else
--s_Checks;
progressBar1.Value = s_Checks * (progressBar1.Maximum / TOTAL_CHECKBOXES);
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
++s_Checks;
else
--s_Checks;
progressBar1.Value = s_Checks * (progressBar1.Maximum / TOTAL_CHECKBOXES);
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
++s_Checks;
else
--s_Checks;
progressBar1.Value = s_Checks * (progressBar1.Maximum / TOTAL_CHECKBOXES);
}
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked || checkBox2.Checked || checkBox3.Checked || checkBox4.Checked == true)
{
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
checkBox4.Checked = false;
}
}
private void clearCheckBoxToolStripMenuItem_Click(object sender, EventArgs e)
{
if (checkBox1.Checked || checkBox2.Checked || checkBox3.Checked || checkBox4.Checked == true)
{
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
checkBox4.Checked = false;
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = progressBar1.Value.ToString();
}
}
}
public Form1()
{
InitializeComponent();
progressBar1.Value = s_Checks * (progressBar1.Maximum / TOTAL_CHECKBOXES);
label1.Text = progressBar1.Value.ToString();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Start();
}
I have a simple bitmap editor for small monochrome bitmaps. And I have a menus which allows to choose thickness of the pen and robber. The problem is that I don't like the following code. Is there any way to make it more compact?
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 3;
}
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 2;
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 1;
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 4;
}
private void toolStripMenuItem6_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 5;
}
private void toolStripMenuItem7_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 6;
}
private void toolStripMenuItem8_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 7;
}
private void toolStripMenuItem9_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 8;
}
private void toolStripButton3_Click_1(object sender, EventArgs e)
{
CurrentPanel.EditMode = EditMode.Clear;
}
private void toolStripDropDownButton2_Click(object sender, EventArgs e)
{
CurrentPanel.EditMode = EditMode.FreeHand;
}
private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
{
CurrentPanel.EditMode = EditMode.Clear;
}
private void toolStripMenuItem10_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 1;
}
private void toolStripMenuItem11_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 2;
}
private void toolStripMenuItem12_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 3;
}
private void toolStripMenuItem13_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 4;
}
private void toolStripMenuItem14_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 5;
}
private void toolStripMenuItem15_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 6;
}
private void toolStripMenuItem16_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 7;
}
private void toolStripMenuItem17_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 8;
}
private void toolStripMenuItem18_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 16;
}
private void toolStripMenuItem19_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 32;
}
private void toolStripMenuItem21_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 32;
}
private void toolStripMenuItem20_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 16;
}
private void toolStripMenuItemCommon_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem) sender;
int thickness = (int)item.Tag;
CurrentPanel.PenThickness = thickness ;
}
Of course you need to initialize Tag of each ToolStripMenuItem and set toolStripMenuItemCommon_Click as a handler for Click event. You can do it in a for cycle for example:
for(int i = 1; i < 8; i++)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Text = "Set thickness: " + i;
item.Click += toolStripMenuItemCommon_Click;
item.Tag = i;
// add item to strip container
}
Yes, there is:
private void SetPenThickness(int thickness)
{
CurrentPanel.PenThickness = thickness;
}
toolStripMenuItem1.Click += (s,e) => SetPenThickness(1);
toolStripMenuItem2.Click += (s,e) => SetPenThickness(2);
toolStripMenuItem3.Click += (s,e) => SetPenThickness(3);
toolStripMenuItem4.Click += (s,e) => SetPenThickness(4);
// ...
Try this:
private void toolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem) sender;
switch (toolStripMenuItem.ID)
{
case "toolStripMenuItem4":
{
CurrentPanel.PenThickness = 3;
break;
}
..........
..........
}
}