I'm new in c#, I want to make a small translator with some words.
private void button1_Click(object sender, EventArgs e)
{
string i;
i = textBox1.Text;
if (textBox1.Text == bonjour) ;
{
label1.Text = "Hello";
}
if (textBox1.Text == Hello) ;
{
label1.Text = "bonjour";
}
}
But label always "bonjour". Where did I go wrong?
This works with some changes.
string i;
i = textBox1.Text;
if (textBox1.Text == "bonjour") //Remove the ";" and put quotes around string
{
label1.Text = "Hello";
}
if (textBox1.Text == "Hello")
{
label1.Text = "bonjour";
}
I would also suggest, if case does not matter, the following:
string i;
i = textBox1.Text;
if (textBox1.Text.ToLower() == "bonjour")
{
label1.Text = "Hello";
}
if (textBox1.Text.ToLower() == "hello")
{
label1.Text = "bonjour";
}
private void button1_Click(object sender, EventArgs e)
{
string i;
i = textBox1.Text;
if (textBox1.Text == "bonjour")
{
label1.Text = "Hello";
}
if (textBox1.Text == "Hello")
{
label1.Text = "bonjour";
}
}
You don't want semicolons at the end of tests.
Also, you need double quotes "" around the strings you're testing for.
With the way you've set this up, you could also do this:
private void button1_Click(object sender, EventArgs e)
{
string i;
i = textBox1.Text;
if (i == "bonjour")
{
label1.Text = "Hello";
}
if (i == "Hello")
{
label1.Text = "bonjour";
}
}
Furthermore, you have no way of testing case, so use .ToLower(), as suggested by Matt Cullinan.
private void button1_Click(object sender, EventArgs e)
{
string i;
i = textBox1.Text;
if(textBox1.Text == "bonjour");
{
label1.Text = "Hello";
}
else if(textBox1.Text == "Hello");
{
label1.Text = "bonjour";
}
}
Related
Currently i am having trouble updating a row in my listview that contains both text and a timer that counts down. So far I've tried updating the entire row as well as trying to update just the text and i get an error in each instance.
System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'index
I am also having a slight problem with the delete button. It deletes the row as planned but it appears the information is still running in the background and haven't come across any info on how to delete the session. I pray for your guidance.
private void Confirm_Click(object sender, EventArgs e)
{
CSession newSession = new CSession();
if(PasswordText.Text == "")
{
MessageBox.Show("Password not entered");
return;
}
newSession.password = PasswordText.Text;
newSession.purchased_time = workingTimeSpan;
newSession.remaining_time = workingTimeSpan;
newSession.status = "Online";
sessionlist.Add(newSession);
PasswordText.Text = "";
TimerLabel.Text = "";
workingTimeSpan = new TimeSpan();
}
private void DisplayAllSessions()
{
listView1.Items.Clear();
foreach(CSession c in sessionlist)
{
string[] row = { c.password, c.purchased_time.ToString(), c.remaining_time.ToString(), c.status };
ListViewItem i = new ListViewItem(row);
listView1.Items.Add(i);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
foreach(CSession c in sessionlist)
{
if (c.remaining_time.TotalMinutes == 5 && !c.MessageDisplayed)
{
c.MessageDisplayed = true;
MessageBox.Show("Time almost up for client.");
}
if (c.remaining_time.TotalSeconds < 1)
{
c.status = "Offline";
}
if(c.status == "Online")
{
c.remaining_time -= oneSecond;
}
}
DisplayAllSessions();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void Reset_Click(object sender, EventArgs e)
{
}
private void updatebutton()
{
listView1.SelectedItems[0].SubItems[0].Text = PasswordText.Text;
PasswordText.Text = "";
}
private void Update_Click(object sender, EventArgs e)
{
updatebutton();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
PasswordText.Text = listView1.SelectedItems[0].Text;
TimerLabel.Text = listView1.SelectedItems[0].SubItems[1].Text;
}
private void Delete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you wish to delete.", "Removing User", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
listView1.Items.Clear();
timer1.Stop();
}
}
This is what i used for the update function and delete row function.
private void updatebutton()
{
CSession updatedsession = new CSession();
int updateindex = 0;
int index = 0;
foreach (CSession lookup in sessionlist)
{
if(lookup.password == PasswordText.Text)
{
if (MessageBox.Show("Do you wish to add time.", "Adding Time",
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
updatedsession = lookup;
updateindex = index;
}
break;
}
index++;
}
updatedsession.purchased_time = workingTimeSpan +
updatedsession.purchased_time;
updatedsession.remaining_time = workingTimeSpan +
updatedsession.remaining_time;
sessionlist[updateindex] = updatedsession;
//sortedSessionList[PasswordText.Text] = updatedsession;
PasswordText.Text = "";
TimerLabel.Text = "";
workingTimeSpan = new TimeSpan();
}
private void Update_Click(object sender, EventArgs e)
{
updatebutton();
}
private void Delete_Click(object sender, EventArgs e)
{
CSession cSession = new CSession();
if (MessageBox.Show("Do you wish to delete.", "Removing User",
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
int index = 0;
foreach (CSession c in sessionlist)
{
if(c.password == PasswordText.Text)
{
break;
}
index++;
}
sessionlist.RemoveAt(index);
}
}
Hey I was trying to create a method like...
private void btnSubmit_Click(object sender, EventArgs e)
{
FillIn();
}
private void FillIn()
{
if (txtName.Text == "")
{
txtName.Text = "Bob Frank";
}
if (txtAddress.Text == "")
{
txtAddress.Text = "4111 N Pensyvania Ave.";
}
if (txtCity.Text == "")
{
txtCity.Text = "Longbeach";
}
if (txtState.Text == "")
{
txtState.Text = "CA";
}
if(txtZip.Text == "")
{
txtZip = "90210";
}
}
this code works great but when I try to add parameters to it like this..
private void btnSubmit_Click(object sender, EventArgs e)
{
FillIn(txtName.Text, txtStreetAddress.Text, txtCity.Text, txtState.Text, txtZip.Text);
}
private void FillIn(string name, string address, string city, string state, string zip)
{
if (name == "")
{
name = "Bob Frank";
}
if (address == "")
{
address = "4111 N Pensyvania Ave.";
}
if (city == "")
{
city = "Longbeach";
}
if (state == "")
{
state = "CA";
}
if(zip == "")
{
zip = "99210";
}
}
it stops working and the text boxes won't fill back in and won't error out, how can I fix this?
You need to pass the actual controls. If you try and pass txtName.Text it just reads the value in the property and you can't update it.
private void btnSubmit_Click(object sender, EventArgs e)
{
FillIn(txtName, txtStreetAddress, txtCity, txtState, txtZip);
}
private void FillIn(TextBox name, TextBox address, TextBox city, TextBox state, TextBox zip)
{
if (name.Text == "")
{
name.Text = "Bob Frank";
}
if (address.Text == "")
{
address.Text = "4111 N Pensyvania Ave.";
}
if (city.Text == "")
{
city.Text = "Longbeach";
}
if (state.Text == "")
{
state.Text = "CA";
}
if(zip.Text == "")
{
zip.Text = "99210";
}
}
Is there anyway to change the search function for the AutoCompleteStringCollectionclass for the TextBox class? Right now, I have it on the default search function where the beginning letters of my suggestions match the letters of the user input? My suggestions come from a custom source.
You can make your own AutoComplete search - by using TextChange
Here is answer: C# winforms combobox dynamic autocomplete
Code:
string[] data = new string[] {
"Absecon","Abstracta","Abundantia","Academia","Acadiau","Acamas",
"Ackerman","Ackley","Ackworth","Acomita","Aconcagua","Acton","Acushnet",
"Acworth","Ada","Ada","Adair","Adairs","Adair","Adak","Adalberta","Adamkrafft",
"Adams"
};
public Form1()
{
InitializeComponent();
}
private void comboBox1_TextChanged(object sender, EventArgs e)
{
HandleTextChanged();
}
private void HandleTextChanged()
{
var txt = comboBox1.Text;
var list = from d in data
where d.ToUpper().StartsWith(comboBox1.Text.ToUpper())
select d;
if (list.Count() > 0)
{
comboBox1.DataSource = list.ToList();
//comboBox1.SelectedIndex = 0;
var sText = comboBox1.Items[0].ToString();
comboBox1.SelectionStart = txt.Length;
comboBox1.SelectionLength = sText.Length - txt.Length;
comboBox1.DroppedDown = true;
return;
}
else
{
comboBox1.DroppedDown = false;
comboBox1.SelectionStart = txt.Length;
}
}
private void comboBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back)
{
int sStart = comboBox1.SelectionStart;
if (sStart > 0)
{
sStart--;
if (sStart == 0)
{
comboBox1.Text = "";
}
else
{
comboBox1.Text = comboBox1.Text.Substring(0, sStart);
}
}
e.Handled = true;
}
}
I have a calciulator. it works fine example enter 10-5 in textbox1 and result shown on textbox2. but i want to calculate more. example 8-5*3-1 like this. or 7-2+3 and so on.
how it will be ?
here is my code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
float ricxvi1, pasuxi;
int datvla;
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 7;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox1.Text = textBox1.Text + 0;
textBox2.Clear();
textBox2.Text = textBox2.Text + 0;
datvla = 0;
}
private void button6_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 4;
}
private void button17_Click(object sender, EventArgs e)
{
ricxvi1 = ricxvi1 = float.Parse(textBox1.Text);
datvla = 1;
textBox1.Text += "-";
}
private void button10_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 1;
}
private void button11_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 2;
}
private void button12_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 3;
}
private void mimateba_Click_1(object sender, EventArgs e)
{
ricxvi1 = float.Parse(textBox1.Text);
datvla = 2;
textBox1.Text += "+";
}
private void button7_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 5;
}
private void button8_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 6;
}
private void button5_Click(object sender, EventArgs e)
{
ricxvi1 = float.Parse(textBox1.Text);
datvla = 3;
textBox1.Text += "*";
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 8;
}
private void button4_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + 9;
}
private void button9_Click(object sender, EventArgs e)
{
ricxvi1 = float.Parse(textBox1.Text);
datvla = 4;
textBox1.Text += "/";
}
private void button14_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + 0;
}
private void button16_Click_1(object sender, EventArgs e)
{
switch (datvla)
{
case 1:
pasuxi = ricxvi1 - float.Parse(textBox1.Text.Substring(textBox1.Text.Length - 1));
textBox2.Text = pasuxi.ToString();
break;
case 2:
pasuxi = ricxvi1 + float.Parse(textBox1.Text.Substring(textBox1.Text.Length - 1));
textBox2.Text = pasuxi.ToString();
break;
case 3:
pasuxi = ricxvi1 * float.Parse(textBox1.Text.Substring(textBox1.Text.Length - 1));
textBox2.Text = pasuxi.ToString();
break;
case 4:
pasuxi = ricxvi1 / float.Parse(textBox1.Text.Substring(textBox1.Text.Length - 1));
textBox2.Text = pasuxi.ToString();
break;
default:
break;
}
}
private void button15_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Clear();
textBox1.Text = textBox1.Text + ".";
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
You have to build a parser to understand the calculations done. Regex or manually parsing is very hard, probably harder than using a parser.
A good parser is ANTLR, which also has a sample grammar for a calculator.
You'll most likely want to create a method which takes in as many decimal values as are entered. Such as my example below, with this you can pass in the entered digits to the method. Then inside the method call calculation methods based on the make up. My point is that you don't want business or data logic handled in the UI and as you have a common task of checking, clearing and assigning to the textbox go for a method that'll handle that for you.Multiple input method
I am new to c#. I built a simple calculator. But it does not have keyboard input. How to enable it?
I want to get 5 in the text box when number 5 s pressed from keyboard.
Also i want to hide mouse cursor in text field. Now when the application stars, mouse cursor appears in the text field.
This is my code:
namespace MyCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//textBox1.Text = "0";
}
double num1=0, num2, result;
string op;
private void button_plus_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = String.Empty;
op = "+";
}
private void button_minus_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = String.Empty;
op = "-";
}
private void button_mul_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = String.Empty;
op = "*";
}
private void button_div_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = String.Empty;
op = "/";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "1";
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "2";
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "3";
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "4";
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "5";
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "6";
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "7";
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "8";
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "9";
}
private void button0_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "0";
}
private void button00_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "00";
}
private void button_point_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ".";
}
private void button_clear_Click(object sender, EventArgs e)
{
textBox1.Text = String.Empty;
}
private void button_result_Click(object sender, EventArgs e)
{
calculate(op);
}
public void calculate( string op)
{
num2 = Convert.ToDouble(textBox1.Text);
switch(op)
{
case "+" : result=num1+num2;
textBox1.Text = result.ToString(); break;
case "-": result = num1 - num2;
textBox1.Text = result.ToString(); break;
case "*": result = num1 * num2;
textBox1.Text = result.ToString(); break;
case "/": result = num1 / num2;
textBox1.Text = result.ToString(); break;
}
num1 = 0; num2 = 0;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Font = new Font("Arial",12, FontStyle.Bold);
textBox1.Cursor = Cursors.Arrow;
}
}
}
To capture keyboard KeyDown event you have to first enable KeyPreview of that form. Select the form and go to properties and set KeyPreview = true.
Use KeyDown event to capture keyboard events,
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.NumPad5))
{
//Assuming button5 will set the value 5
button5.PerformClick();
}
}
Hiding Cursor
I assume you are expecting the same behaviour as Windows calculator. Rather than hiding the cursor simply you can disable the textbox. Set the textbox Enabled = false.