Archive for March, 2009

C# User Controls

March 2, 2009
[1] You can ad a User Control class directly to a Windows Form application.
It doesn’t have to be in a separate Control Class project.
 
[2] Properties and Methods
Generally you add public properties and public methods to the Control Class so as to make the class usable.
(If accessing sub controls (controls within the control), you will need to use the InvokeRequired methodology.)
============================
        private bool _enabled = false;
        public bool _Enabled { get { return _enabled; } set { _enabled = value; } }
        private void chkEnabled_CheckedChanged(object sender, EventArgs e)
        {
            if (chkEnabled.Checked) _Enabled = true; else _Enabled = false;
        }
============================
 
[3] Events/
Typical scenario: A button on the control needs to fire a handler as part of the code in which an instance of the control exists. eg:
============================
//Within control
        public delegate void SubmitClickedHandler();
        [Category("Action")]
        [Description("Fires when the Submit button is clicked.")]
        public event SubmitClickedHandler SubmitClicked;
        protected virtual void OnSubmitClicked()
        {
            if (SubmitClicked != null)
            {
                SubmitClicked();  // Notify Subscribers
            }
        }
        private void cmdSubmit_Click(object sender, EventArgs e)
        {
            OnSubmitClicked();
        }
============================
//Within code that has an instance of the control:
        private void UsrControl_SubmitClicked()
        {

           ………………….

           …………………
        }
============================

C# RadioButtons

March 2, 2009
When radio buttons are added to the same Groupbox, onlyone can be selected (checked) at a time.
To have a common handler, set the CheckedChanged event handler to be the same for all such RadioButtons:
– Add a new Groupbox
– Add RadioButtons to the GroupBox
– Set the default button to chcked
– Create the common handler method (Copy its name).
– Select all of the RadioButtons in Design Mode.
– On Property Page (Event Handler Mode: Press on Lightening) paste the Handler Method name.
WALA
 
============================
        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked) bitslider1.Show(); else bitslider1.Hide();
            if (radioButton2.Checked) bitslider2.Show(); else bitslider2.Hide();
            if (radioButton3.Checked) bitslider3.Show(); else bitslider3.Hide();
        }
============================
This code shows 1 or 3 controls depending upon which RadioButton is checked.

C# Newline for Textbox, Serial Port Receive

March 2, 2009
[1] Textbox newlines/Serial received data
I needed to take serial received text data (SerialPort Control) and display in multiline textbox.  Here are some hints for doing this:
– Make the target a multiline textbox with vertical scrollbar.
– Trap received data event and read line by line.  (Initially tried Read all but had some line ending problems).
– Write each line with "\r\n" appended.
– Need the ..InvokeRequired multithread handling.
– So that it autoscrolls add <Textbox>.ScrollToCarat
=========================       
       delegate void MyDelegate(string dat);
        private void writeSerial (string dat) {
            if (textBox1.InvokeRequired)
            {
                BeginInvoke(
                    new MyDelegate(writeSerial), new object[] { dat });
            }else  {
                textBox1.Text += dat;
                textBox1.ScrollToCaret();
            }
        }
       
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            if (e.EventType == System.IO.Ports.SerialData.Chars)
            {
                try
                {
                    while (serialPort1.BytesToRead != 0)
                    {
                        try
                        {
                            string strn = serialPort1.ReadLine();
                            writeSerial(strn + "\r\n");
                        }
                        catch
                        {
                        }
                    }
                }
                catch { }
            }
        }
 
=========================
Could improve upon the error handling.