Você está na página 1de 10

MessageBox:

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;

namespace message_box_plus
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
MessageBox.Show("Hello\nWorld");
MessageBox.Show("\nHello!!!", "Message", MessageBoxButtons.OKCancel,
MessageBoxIcon.Question);
}
}
}

GET TEXT FFROM TEXT BOXES

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
string firstname, lastname;

firstname = textBox1.Text;
lastname = textBox2.Text;
MessageBox.Show(firstname + lastname);

}
private void textBox2_TextChanged(object sender, EventArgs e)
{

private void textBox1_TextChanged(object sender, EventArgs e)


{

}
}
}

GET integer FFROM TEXT BOXES


CALCULATOR IN WINDOW FORM
private void button1_Click(object sender, EventArgs e)
{
int firstvalue = int.Parse(textBox1.Text);

int lastvalue = int.Parse(textBox2.Text);


int add = firstvalue + lastvalue;
MessageBox.Show(add.ToString());
}

private void textBox2_TextChanged(object sender, EventArgs e)


{

private void textBox1_TextChanged(object sender, EventArgs e)


{

private void button2_Click(object sender, EventArgs e)


{
int firstvalue = int.Parse(textBox1.Text);

int lastvalue = int.Parse(textBox2.Text);


int SUBTRACT = firstvalue - lastvalue;
MessageBox.Show(SUBTRACT.ToString());
}

private void button3_Click(object sender, EventArgs e)


{
int firstvalue = int.Parse(textBox1.Text);
int lastvalue = int.Parse(textBox2.Text);
int MULTIPLY = firstvalue*lastvalue;
MessageBox.Show(MULTIPLY.ToString());
}

private void button4_Click(object sender, EventArgs e)


{
int firstvalue = int.Parse(textBox1.Text);

int lastvalue = int.Parse(textBox2.Text);

int DIVISION = firstvalue/lastvalue;

MessageBox.Show(DIVISION.ToString());

private void button5_Click(object sender, EventArgs e)


{
int firstvalue = int.Parse(textBox1.Text);

int lastvalue = int.Parse(textBox2.Text);


int REMINDER = firstvalue % lastvalue;
MessageBox.Show(REMAINDER.ToString());
}
}
}

http://www.homeandlearn.co.uk/csharp/csharp_s4p2.html
CALCULATOR
Calculator:
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;

namespace Calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Plusbtn_Click(object sender, EventArgs e)


{
textBox3.Text = (double.Parse(textBox1.Text) + double.Parse(textBox2.Text)).ToString();

private void Subbtn_Click(object sender, EventArgs e)


{
textBox3.Text = (double.Parse(textBox1.Text) - double.Parse(textBox2.Text)).ToString();
}

private void Mulbtn_Click(object sender, EventArgs e)


{
textBox3.Text = (double.Parse(textBox1.Text) * double.Parse(textBox2.Text)).ToString();
}

private void Divibtn_Click(object sender, EventArgs e)


{

textBox3.Text = (double.Parse(textBox1.Text) / double.Parse(textBox2.Text)).ToString();

private void Modbtn_Click(object sender, EventArgs e)


{
textBox3.Text = (double.Parse(textBox1.Text) % double.Parse(textBox2.Text)).ToString();
}
}
}

SQL Connection and Manipulation


(Different Sql
Commands/Queries):
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.Data.SqlClient; //.OleDb for ms access instead of .sqlclient
namespace SQL_DataBase_Manipulation
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP-
PFDOLD6\\SQLEXPRESS;Initial Catalog=StudentDB;Integrated Security=True");
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

private void Insert_btn_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO [StudentTable] ([S_Name],
[S_Address]) VALUES ('"+textBox2.Text+"','"+textBox3.Text+"')",con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Inserted Successfully!!!");

private void View_btn_Click(object sender, EventArgs e)


{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [StudentTable]",con);
//DataTable dt = new DataTable();
//da.Fill(dt);

DataSet ds = new DataSet();

da.Fill(ds, "s");

dataGridView1.DataSource = ds.Tables["s"];
// dataGridView1.DataBind();
con.Close();

private void Delete_btn_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand com = new SqlCommand("DELETE FROM [StudentTable] WHERE (S_Name=
'"+textBox2.Text+"')",con);
com.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Deleted Successfully!!!");

private void Update_btn_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd = new SqlCommand("UPDATE [StudentTable] SET
[S_Address]='"+textBox3.Text+"' WHERE ([S_Name]='"+textBox2.Text+"')",con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Updated Successfully!!!");
}

private void Search_btn_Click(object sender, EventArgs e)


{ con.Open();
SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM [StudentTable] WHERE
([S_Name] LIKE '%"+textBox2.Text+"%') OR ([S_Address] LIKE '%"+textBox3.Text+"%')",con);
DataSet ds = new DataSet();
da.Fill(ds,"s");
dataGridView1.DataSource = ds.Tables["s"]
con.Close() } } }
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.Data.SqlClient;
//using system .Data.OleDb; for MS ACCESS
namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
// TODO: This line of code loads data into the 'eMSDataSet.EMP' table. You can move, or
remove it, as needed.
this.eMPTableAdapter.Fill(this.eMSDataSet.EMP);

private void INSERT_Click(object sender, EventArgs e)


{
SqlConnection con = new SqlConnection("Data Source=WCC-PC;Initial
Catalog=EMS;Integrated Security=True;");
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO EMP VALUES ('" + textBox1.Text +
"','" + textBox2.Text +"','" +textBox3.Text+"','"+textBox4.Text+"')", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Inserted Successfully!!!");

/* SqlConnection con = new SqlConnection("Data Source=WCC-PC;Initial


Catalog=EMS;Integrated Security=True;");
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO EMP VALUES
(@ID,@NAME,@ADDRESS,@SALARY)", con);
cmd.Parameters.AddWithValue("@ID", textBox1.Text);
cmd.Parameters.AddWithValue("@NAME", textBox2.Text);
cmd.Parameters.AddWithValue("@ADDRESS", textBox3.Text);
cmd.Parameters.AddWithValue("@SALARY", textBox4.Text);
MessageBox.Show("Inserted Successfully!!!");
con.Close();*/

private void button5_Click(object sender, EventArgs e)


{
SqlConnection con = new SqlConnection("Data Source=WCC-PC;Initial
Catalog=EMS;Integrated Security=True;");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [EMP]", con);
//DataTable dt = new DataTable();
//da.Fill(dt);

DataSet ds = new DataSet();

da.Fill(ds, "s");

dataGridView1.DataSource = ds.Tables["s"];
// dataGridView1.DataBind();
con.Close();

private void DELETE_Click(object sender, EventArgs e)


{
SqlConnection con = new SqlConnection("Data Source=WCC-PC;Initial
Catalog=EMS;Integrated Security=True;");
con.Open();
SqlCommand com = new SqlCommand("DELETE FROM [EMP] WHERE (ID= '" +
textBox1.Text + "')", con);
com.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Deleted Successfully!!!");

private void UPDATE_Click(object sender, EventArgs e)


{
SqlConnection con = new SqlConnection("Data Source=WCC-PC;Initial
Catalog=EMS;Integrated Security=True;");
con.Open();
SqlCommand cmd = new SqlCommand("UPDATE [EMP] SET [ID]='" + textBox1.Text + "',
[NAME]='" + textBox2.Text + "',[ADDRESS]='" + textBox3.Text + "',[SALARY]='" + textBox4.Text
+ "' WHERE ([ID]='" + textBox1.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Updated Successfully!!!");

private void VIEW_Click(object sender, EventArgs e)


{
//it is used for search in database
SqlConnection con = new SqlConnection("Data Source=WCC-PC;Initial Catalog=EMS;Integrated
Security=True;");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [EMP] WHERE ([ID] LIKE '%" +
textBox1.Text + "%') OR ([NAME] LIKE '%" + textBox2.Text + "%') OR ([ADDRESS] LIKE '%" +
textBox3.Text + "%') OR ([SALARY] LIKE '%" + textBox4.Text + "%')", con);
DataSet ds = new DataSet();
da.Fill(ds, "s");
dataGridView1.DataSource = ds.Tables["s"];
con.Close();
}
}
}

Você também pode gostar