20 December, 2014

How to use FontDailongBox in Winform c#

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = fontDialog1.ShowDialog();
            // See if OK was pressed.
            if (result == DialogResult.OK)
            {
                // Get Font.
                Font font = fontDialog1.Font;
                // Set TextBox properties.
                this.lblLabName.Text = string.Format("Font is­: {0}", font.Name);
                this.lblLabName.Font = font;
            }
        }

ColorDailogBox

private void ForegroundButton_Click(object sender, EventArgs e)
{
    ColorDialog colorDlg = new ColorDialog();
    colorDlg.AllowFullOpen = false;
    colorDlg.AnyColor = true;
    colorDlg.SolidColorOnly = false;
    colorDlg.Color = Color.Red;
                    
    if (colorDlg.ShowDialog() == DialogResult.OK)
    {
        textBox1.ForeColor = colorDlg.Color;
        listBox1.ForeColor = colorDlg.Color;
        button3.ForeColor = colorDlg.Color;
    }
}

private void BackgroundButton_Click(object sender, EventArgs e)
{
    ColorDialog colorDlg = new ColorDialog();
    if (colorDlg.ShowDialog() == DialogResult.OK)
    {
        textBox1.BackColor = colorDlg.Color;
        listBox1.BackColor = colorDlg.Color;
        button3.BackColor = colorDlg.Color;
    }
}

19 December, 2014

ContextMenuStrip in winform c#

ContextMenuStrip
1) Drag & Drop 'ContextMenuStrip' on winform [name it as my "subMenuStrip"]
2) Edit items on it.
[ for now i adding items like 'Menu1' & 'Menu2']

Button [drag any winform item : e.g. I use button in it]
1) In 'btnShow' property Select ContextMenuStrip and select  subMenuStrip 
2) You can also set some other properties in it... [i.e. font, backColor]

Run the application and see the result...

Event for "ContextMenuStrip"

On 'subMenuStrip' ItemClick event

private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
                //if I select 'Menu1' it will show selected item name in my MessegeBox.
                MessageBox.Show(""+e.ClickedItem+"");
                [ write your code here]
        }

18 December, 2014

DatagridView as View-detail

If we want to save data from WinForm; we just design our form, drag some labels & text-boxes on it and finally part of coding....
We have to code for Insert Data / Update Data / Delete Data...
for each work we design or code a individual part of coding...

But, here is the simplest way to do all that work without any coding and on a one single WinForm page...

Let's see :

1) Open  your WinForm project & add new winform on it.
2) Go to Data --> Click on Show Data Source


[it will show DataSource window just left side of screen]






3) Click on 'Add New Data Source' 

















4 ) Data Configuration window will be appeared.
     Select 'Database' & click 'Next'
     Select 'Dataset' click 'Next'
     Your connection string may selected on ConnectionString ; if not click on 'New Connection' and select which database you want to select
     Click 'Next'
5) Select which table you want to choose & enter unique and meaningful DataSet Name and Click 'Finish'

6)  Select dataSet name which we recently added ...


7) After selected 'Details' view select  Just 'Drag & Drop' Profile detail view on form .. it will see like below

8) that's it...
    You can now 'Add/Delete/Update' data from just one single Winform page without doing any coding...
   You can also see all your records from navigator buttons.

Thnak you.



Working with Crystal Report with c#

Monthly rpt = new Monthly();
            using (SqlConnection con = new SqlConnection(CS))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("spReport", con);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.ExecuteNonQuery();

                SqlDataAdapter da = new SqlDataAdapter(cmd);

                DataSet dsTemp = new DataSet();
                da.Fill(dsTemp, "tb_DentcoLab");

                DataTable dt = new DataTable();
                da.Fill(dt);

                dt = dsTemp.Tables["tb_DentcoLab"];

                
                 if (dt.Rows.Count > 0)
                {
                    ReportDocument repDoc = new ReportDocument();

// To show any value from Form1.cs to CrystalReport 



                    TextObject txtDateToFrom = (TextObject)rpt.ReportDefinition.ReportObjects["txtDateToFrom"];
                    txtDateToFrom.Text = "" + DateTime.Now.ToString("dd MMM yyyy") + "";

                    TextObject txtDocName = (TextObject)rpt.ReportDefinition.ReportObjects["txtDocName"];
                    txtDocName.Text = "" + txtDoctorName.Text + "";

                    TextObject txtAddress = (TextObject)rpt.ReportDefinition.ReportObjects["txtAddress"];
                    txtAddress.Text = "" + _DocAddress + "";

                    TextObject txtInvoiceNo = (TextObject)rpt.ReportDefinition.ReportObjects["txtInvoiceNo"];
                    txtInvoiceNo.Text = "" + txtInvoice.Text + "";
                    
                    rpt.SetDataSource(dsTemp);
                    RV.ReportSource = rpt;
                    RV.Zoom(1);
                }
                else
                {
                    MessageBox.Show("No Data found");
                }
            }


Note : 
TextObject of CrystalReport
["txtDateToFrom"],
["txtDocName"],
["txtAddress"],
["txtInvoiceNo"]  

RV is the name of CrystalReportViewer 
RV.Zoom(1) means zoom level set to whole page
RV.Zoom(2) means zoom level set to Page width

TextBox AutoSuggest from database in c#



private void DocNamePopUp()
        {
            AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
            getData(DataCollection);
            txtDocName.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtDocName.AutoCompleteCustomSource = DataCollection;
        }

        private void getData(AutoCompleteStringCollection dataCollection)
        {
            string connetionString = null;
            SqlConnection connection;
            SqlCommand command;
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();


            using(SqlConnection connection = new SqlConnection(CS));
            try
            {
                connection.Open();
                command = new SqlCommand("spGetDoctorNames", connection);
                command.CommandType = CommandType.StoredProcedure;
                adapter.SelectCommand = command;
                adapter.Fill(ds);
                adapter.Dispose();
                command.Dispose();
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    dataCollection.Add(row[0].ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }
        }


How to pass value from one Winform to another Winform in c#

Suppose we have to pass value from LoginPage.cs to HomePage.cs like Username & Role (whatever as per requirement)

Here the simplest code for it :

1) LoginPage.cs

on btnLogin Click :

[ write your code to connect the user with database and if user is valid ... ]

private void btnLogin_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(CS))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("Select * from Users where Username='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "'", con);
                SqlDataReader dr = cmd.ExecuteReader();

                if (dr.Read())
                {
                    this.Hide();
                    HomePage frm = new HomePage();
                    frm.UserName = txtUsername.Text;
                    frm.Show();
                }
                else
                {
                    MessageBox.Show("Username or Password incorrect", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }

2) HomePage.cs

You have to declare / initialize some public string ... like

public HomePage()
        {
            InitializeComponent();
        }

        public string UserName;
        private void HomePage_Load(object sender, EventArgs e)
        {
           
            lblUserName.Text = UserName;
        }


that's it;
on LoginPage's btnLogin button click it will save/pass values to HomePage.cs public string... and that values will be send to HomePage.cs [call it on HomePage.cs Load Event].

17 December, 2014

How to use "switch case" for DialogResult?

using (SqlConnection con = new SqlConnection(CS))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("Select Distinct DoctorName from DoctorNames where DoctorName='"+txtDocName.Text+"'", con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (!dr.Read())
                {
                    DialogResult result = MessageBox.Show("You entered new Dontor's Name, do you want to add it in database?", "Dentco Dental Lab", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    switch(result)
                    {
                        case DialogResult.Yes:

                            break;

                        case DialogResult.No:
                            break;
                    }
                }
            }