Showing posts with label Window Application. Show all posts
Showing posts with label Window Application. Show all posts

Sunday, September 20, 2009

Cross-thread operation not valid.

In window application , when you need to use Threading and access control inside that thread, then you get this error “Cross-thread operation not valid.” The reason behind this control is working under main thread and you create another thread in which you are trying to access control.

There are two ways to solve this error.

1. Set property in Form constructor.
You need to set static property CheckForIllegalCrossThreadCalls  of control class to false value. This will disable cross thread checking.
       
       public Form1()
       {
         InitializeComponent();
           Control.CheckForIllegalCrossThreadCalls = false;
       }

Sample Code (C#):

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Test));
            th.Start(1000);           
        }

        public void Test(object o)
        {
            for (int i = 0; i < 10; i++)
            {
                label1.Text = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString() + " --- " + i.ToString();
                System.Threading.Thread.Sleep(int.Parse(o.ToString()));
            }
        }
    }



2. Use delegate and event.

Sample Code (C#) :

public partial class Form1 : Form
    {
        delegate void SetLabelText(string str);
        SetLabelText setLabel1Text = null;
        public Form1()
        {
            InitializeComponent();
            setLabel1Text = new SetLabelText(Label1Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Test));
            th.Start(1000);           
        }

        public void Test(object o)
        {
            for (int i = 0; i < 10; i++)
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(setLabel1Text, System.Threading.Thread.CurrentThread.ManagedThreadId.ToString() + " --- " + i.ToString());
                }
                System.Threading.Thread.Sleep(int.Parse(o.ToString()));
            }
        }

        public void Label1Text(string str)
        {
            label1.Text = str;
        }
    }

Please give your comment on this.

Sunday, June 14, 2009

Remove Focus Rectangle From Button

Article is about to remove focus rectangle or say cues from Button in window form application.

First let discuss what default focus rectangle. Please look at following image , in that image OK button has default focus so it display cues rectangle.

image 
(FlatStyle == System)

When we want to set background image of button to make button attractive in that case this default rectangle create problem with look and feel.

image
(FlatStyle = Standard)
 
 image
(FlatStyle = Flat)

In above two image need to set FlatApperance.BorderStyle =0. You can visualize that in all above case default focus rectangle make button look ugly with background image.

To remove this cues ( Default focus rectangle ) you need to set ShowFocusCues property of Button to false but this property does not directly available to Button. This property available in ButtonBase class with protected access specifier. In order to set this property to false we need to create class that inherits from Button or ButtonBase and set this property explicitly false.

class CustomButton : System.Windows.Forms.Button
    {
        protected override bool ShowFocusCues
        {
            get
            {
                return false;
            }
        }
}

image
Now see that default focus rectangle is removed.

More generic class for Button.

class CustomButton : System.Windows.Forms.Button
   {
       private bool _DisplayFocusCues = true;
       protected override bool ShowFocusCues
       {
           get
           {
               return _DisplayFocusCues;
           }
       }

       public bool DisplayFocusCues
       {
           get
           {
               return _DisplayFocusCues;
           }
           set
           {
               _DisplayFocusCues = value;
           }
       }
   }

Using this class you can set DisplayFocusCues at design time so CustomButton work with any case. ( Want to display focus rectangle or not).

image 

Hope this solution is help you to create button without cues.

Your suggestion is always invited.