16 January, 2015

OpenFileDialogBox in Winform (C#))

OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Title = "Attach Document file (PDF)";

            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;

            openFileDialog1.DefaultExt = "txt";
            openFileDialog1.Filter = "PDF file (*.pdf)|*.pdf";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            openFileDialog1.ReadOnlyChecked = true;
            openFileDialog1.ShowReadOnly = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFileAddress.Text = openFileDialog1.FileName;
            }

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 ! ");
            }
        }