Showing posts with label Winforms. Show all posts
Showing posts with label Winforms. Show all posts

Monday, 22 April 2019

Speaking the Text written in Notepad C#

In this article we are going to see how to make a text to speech in computer using the speech synthesizer in C#.

Virtual Human : Click here to download EXE

Browse the text file or type the text in textbox then hit speak button , system starts to speak ur content in your preferred voice mode male or female voice by the selection of radio button.

If you want to save the speech as wav you can save the speech by hit save , save options only enable when you stop the speech

Output:

Load the Text File.


Save the audio by hit save.

Code :

         SpeechSynthesizer speak = new SpeechSynthesizer();
         speak.Volume = 100;
         //speak.Rate = 5
         if(radioButton1.Checked)
             speak.SelectVoiceByHints(VoiceGender.Male);
         else if(radioButton2.Checked)                          
              speak.SelectVoiceByHints(VoiceGender.Female);
         else
             speak.SelectVoiceByHints(VoiceGender.Neutral);
               

           speak.SpeakAsync(Content.Text);

I Hope this article will give you detailed about speech class in c#.

Saturday, 4 June 2016

Call Winforms Forms application from console application and pass the input parameters

In this post we are going to see how to call a winforms application from a console application and pass the input paramteres to the winforms

First we develop a Winforms application with one form where it will have two textbox which is a username and password.

Winforms Design:
******************





Winforms Code :
******************

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            var data = Environment.GetCommandLineArgs();

            if (data.Length == 3)
            {
                textBox1.Text = data[1];
                maskedTextBox1.Text = data[2];
            }
        }
    }

In the above you can see that i am assigning the index 1 and 2 , because on 0 index CommandLingArgs will have the path of the Executable in winforms 



Console Application :
******************
Create a new project in console application, then create the following code , which will invoke the instance of a process.

            Process pro = new Process();
            pro.StartInfo.FileName = @"C:\amApp\ParamApp\bin\Debug\ParamApp.exe";
            pro.StartInfo.Arguments = "Rajesh Password";
            pro.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            pro.Start();
            pro.WaitForExit();


Output:
***********





In this post we can see how to call a winforms application from the Console application

Sunday, 2 August 2015

Create a select all checkbox column Header in DataGridView Winforms

In this post we are going to see how to create a checkbox header column in the datagridview to get a feature like select all option in Datagridview.

First we have to understand some few basics things, normally gridviewcolumn header seems like empty, if we want something display in header then we have to customize it.Here we take a simple example like showing a employee details where we want to select all employee records in single click.

To Achieve this we have to do following steps.
1. Drag and Drop a DataGridView from Toolbox.
2. Create a First column as CheckBox 
3. Then bind the data with the DataGridView.

            DataGridViewCheckBoxColumn col1 = new DataGridViewCheckBoxColumn();           
            dataGridView1.Columns.Add(col1);

            dataGridView1.Rows.Add(4);

            dataGridView1.DataSource = new List<Employee>() {
            new Employee(){ FirstName = "Rajesh",LastName = "G",Id=1},
            new Employee(){ FirstName = "Suresh",LastName="G",Id=2}
            };
            

After this step if we run the application, it looks like following one.



Now actually we need to have an Header of a First column should be checkbox, and when select the checkbox it should trigger an event is the requirement.

We have to add a template for HeaderCell for the first column.So create a class which is derived from the DataGridViewColumnHeaderCell, now we can start implement our logic


1. Our header must have a default value with uncheck.
2. It should be present in the center
3. We have to create a checkbox in header.
4. Create a custom event.
4. Create a Event for mouse click.
5. check whether is click above the checkbox, if so trigger the event

    public class CheckBoxHeaderCellEventArgs:EventArgs
    {
        private bool _isChecked;
        public bool IsChecked {
            get { return _isChecked; }
        }

        public CheckBoxHeaderCellEventArgs(bool _checked)
        {
            _isChecked = _checked;

        }

    }




  public delegate void CheckBoxHeaderClickHandler(CheckBoxHeaderCellEventArgs e);

    class CheckBoxHeaderCell:DataGridViewColumnHeaderCell
    {
        Size checkboxsize;
        bool ischecked;
        Point location;
        Point cellboundsLocation;
        CheckBoxState state = CheckBoxState.UncheckedNormal;

        public event CheckBoxHeaderClickHandler OnCheckBoxHeaderClick;

        public CheckBoxHeaderCell()
        {
            location = new Point();
            cellboundsLocation = new Point();
            ischecked = false;
        }
      
        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
        {
         /* Make a condition to check whether the click is fired inside a checkbox region */
            Point clickpoint = new Point(e.X + cellboundsLocation.X, e.Y +                                                  cellboundsLocation.Y);

            if ( (clickpoint.X > location.X && clickpoint.X < (location.X +                                       checkboxsize.Width)) && (clickpoint.Y > location.Y && clickpoint.Y <                         (location.Y+checkboxsize.Height)))
            {
                ischecked = !ischecked;
                if (OnCheckBoxHeaderClick != null)
                {
                    OnCheckBoxHeaderClick(new CheckBoxHeaderCellEventArgs(ischecked));
                    this.DataGridView.InvalidateCell(this);
                }
            }
            base.OnMouseClick(e);
        }

        protected override void Paint(Graphics graphics, Rectangle clipBounds, 
             Rectangle cellBounds, int rowIndex, DataGridViewElementStates                                 dataGridViewElementState,object value, object formattedValue, string errorText, 
            DataGridViewCellStyle cellStyle,DataGridViewAdvancedBorderStyle     
            advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
           
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, 
           value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

            checkboxsize = CheckBoxRenderer.GetGlyphSize(graphics,                                                               CheckBoxState.UncheckedNormal);
            location.X = cellBounds.X + (cellBounds.Width/2 - checkboxsize.Width/2);
            location.Y = cellBounds.Y + (cellBounds.Height/2 - checkboxsize.Height/2);
            cellboundsLocation = cellBounds.Location;
          
            if (ischecked)
                state = CheckBoxState.CheckedNormal;
            else
                state = CheckBoxState.UncheckedNormal;

            CheckBoxRenderer.DrawCheckBox(graphics, location, state);


        }
    } 


After that  assign the cell template to the gridview column like this.

            DataGridViewCheckBoxColumn col1 = new DataGridViewCheckBoxColumn();
            var checkheader = new CheckBoxHeaderCell();
            checkheader.OnCheckBoxHeaderClick += checkheader_OnCheckBoxHeaderClick;
            col1.HeaderCell = checkheader;
           
            dataGridView1.Columns.Add(col1);

            dataGridView1.Rows.Add(4);

            dataGridView1.DataSource = new List<Employee>() {
            new Employee(){ FirstName = "Rajesh",LastName = "G",Id=1},
            new Employee(){ FirstName = "Suresh",LastName="G",Id=2}
            };

             

When ever click the header check box it will trigger an event , we already hooked an handler for that

        void checkheader_OnCheckBoxHeaderClick(CheckBoxHeaderCellEventArgs e)
        {
            dataGridView1.BeginEdit(true);
            foreach (DataGridViewRow item in dataGridView1.Rows)
            {
                item.Cells[0].Value = e.IsChecked;               
            }
            dataGridView1.EndEdit();

        } 

We can see the exact output what we required.
Output:


From this we can learn how to create a column with checkbox as a header in DataGridView winforms.