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
}
}
Related
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);
}
{
}
}
{
}
}
}
}
Is there a way to create a class or something that can contain all these different textboxes. I just want to know if this is the most efficient way I can code this.
private void textBox1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
private void textBox2_Click(object sender, EventArgs e)
{
textBox2.Text = "";
}
private void textBox3_Click(object sender, EventArgs e)
{
textBox3.Text = "";
}
private void textBox4_Click(object sender, EventArgs e)
{
textBox4.Text = "";
}
private void textBox5_Click(object sender, EventArgs e)
{
textBox5.Text = "";
}
Don't create separated event handlers for each TextBoxes. Instead, create a combined event for all the TextBoxes: textBox1..textBox5.
Then
private void textBoxs_Click(object sender, EventArgs e)
{
if (sender is TextBox box) box.Text = "";
}
You can add the event handler to all the TextBoxes on load, eg
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
foreach(var tb in this.Controls.OfType<TextBox>())
{
tb.Click += (sender, eventArgs) => ((TextBox)sender).Text = "";
}
}
I am new to C#.
Just doing my first project.
This is my code.
I just want to fill the textbox once the progress bar finish loading.
But its not working now.
Can anyone tell me what I am doing worong.
My code is as below.
private void button1_Click(object sender, EventArgs e)
{
this.timer1.Start();
// int n = timer1.Interval;
int m = progressBar1.Value;
if(m==0)
{
textBox2.Text = "test";
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.progressBar1.Increment(1);
}
}
Try This:
int progressVal=0;
private void button1_Click(object sender, EventArgs e)
{
this.timer1.Start();
// int n = timer1.Interval;
}
private void timer1_Tick(object sender, EventArgs e)
{
this.progressBar1.Increment(1);
progressVal= progressBar1.Value;
if(progressVal==progressBar1.Maximum)
{
timer1.Stop();
textBox2.Text = "Loading done!";
}
}
I greatly appreciate any help. I have 20+ buttons, each with a word, or a space or period. Each time I click on a button, the pre-existing word is wiped out and replaced with the new word. I need each word and/or space to remain in place until I click the "Clear" button.
Maybe this has been previously asked/answered under different search terms? I tend to believe I need to identify a string variable, but have no idea how to begin.
==============
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 WindowsFormsApplication1
{
public partial class frmSentenceBuilder : Form
{
public frmSentenceBuilder()
{
InitializeComponent();
}
private void frmSentenceBuilder_Load(object sender, EventArgs e)
{
}
private void btnA_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnA.Text;
}
private void btn_a_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btn_a.Text;
}
private void btnAn_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnAn.Text;
}
private void btn_an_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btn_an.Text;
}
private void btnThe_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnThe.Text;
}
private void btn_the_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btn_the.Text;
}
private void btnman_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnman.Text;
}
private void btnwoman_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnwoman.Text;
}
private void btndog_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btndog.Text;
}
private void btncat_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btncat.Text;
}
private void btncar_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btncar.Text;
}
private void btnbicycle_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnbicycle.Text;
}
private void btnbeautiful_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnbeautiful.Text;
}
private void btnbig_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnbig.Text;
}
private void btnsmall_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnsmall.Text;
}
private void btnstrange_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnstrange.Text;
}
private void btnlookedat_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnlookedat.Text;
}
private void btnrode_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnrode.Text;
}
private void btnspoketo_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnspoketo.Text;
}
private void btnlaughedat_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnlaughedat.Text;
}
private void btndrove_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btndrove.Text;
}
private void btnSpace_Click(object sender, EventArgs e)
{
lblSentenceText.Text = " ";
}
private void btnperiod_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnperiod.Text;
}
private void btnexclam_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnexclam.Text;
}
private void btnClear_Click(object sender, EventArgs e)
{
lblSentenceText.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Use lb1SentenceText.Text+=whatever.Text;.
+= is equivalent to lbSentence.Text = lblSentenceText.Text + whatever.Text.
Basically, it appends or concatenates the right hand side string to the string on the left hand side. Hope it makes sense?
So:
string rhs="Hello " ;
string lhs = "World";
string rhs = rhs + lhs;//Hello World
Please be inspired. You have a lot of redundant code.
btnA.Click += AppendButtonText;
btn_a.Click += AppendButtonText;
...
private void AppendButtonText(object sender, EventArgs e)
{
var button = sender as Button;
if (button != null)
{
lblSentenceText.Text += button.Text;
}
}
On the button click events, change it to += instead of = (except in the clear button). This is equivalent to writing something = something + newValue;.
Try:
lb1SentenceText.Text = lb1SentenceText.Text + *something*.text
The += operator means add to, and the variable modified is equal to the added (Int, String) appended to the original value (x = 1; x += 3; x is now 4)
Try this:
button.Click += new System.EventHandler(ButtonClick);
button1.Click += new System.EventHandler(ButtonClick);
// And for each button, one of those.
private void ButtonClick(object sender, System.EventArgs e)
{
// Do whatever you want to do here, you can place the TEXT to be appended on the button, if so:
lb1SentenceText.Text += sender.Text;
}
//Simple.Create a global variable and within each button click event do this;
string yourStrVar = ""; //Must be global
yourStrVar+= ((Button)sender).Text
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;
}
..........
..........
}
}