Here I will post problems I and my colleagues met and solutions we found.

Showing posts with label WinForms. Show all posts
Showing posts with label WinForms. Show all posts

Tuesday, February 17, 2009

Flickering in WinForms

I used to know these things long time ago. But later, with .NET programming they faded in my memory and it took me more than an hour (I will not tell you how much more) of experimenting to discover it again.

When your screen is flickering, check if DoubleBuffered is on. It may help.

Thursday, August 07, 2008

InkEdit recognize in WinForms

In our recent project we used InkEdit control, and we did it in WinForms. Doing that found there are some tricky things there.

Here is the problem. Let's say I need to close modal window with this control. Before doing that I would like to call Recognize to get latest data from control. However, the procedure is not synchronized. It works this way.

Thread A: call to method recognize returns immediately.
Thread B: event Recognition is raised, if there are data to recognize.

So, I created EventWaitHandle object in thread A and set signal in thread B. Then I waited for WaitHanlde for up to 1 second. If I got event signal - there was text to get from control and if I didn't, there were just nothing new to recognize.

However, it didn't work. And the reason it didn't work was that for data to get into control, it had to get some time in main thread, which is thread A. So, I just got kind of deadlock here.

Unfortunately, I couldn't resolve the problem without call to Application.DoEvents(), which I always try to avoid.

Final Result.

Thread A:
Recognize with WaitHandle
Wait for WaitHandle signal.
If signal received - call ApplicationDoEvents.
Get value from control.

Thread B:
Event handler for Recognition set signal for EventWaitHandle.

Thursday, January 17, 2008

Application.ThreadException Event is static

Today I had problems with handling exceptions in new application. Instead of showing message my application just terminated, and I couldn't find where.

The reason was that Application.ThreadException Event is static event, but handler I assigned to it was not static method. I didn't get any warnings, it just didn't work.

Wednesday, March 02, 2005

DesignMode and LicenseManager.UsageMode in constructor and Event handlers

Today I found why my forms behaved weirdly in design-time.

I have two forms, one is parent in terms of inheritance, another is child one. Example:

public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// DesignMode = false if you open ChildForm
MessageBox.Show("Form1 constructor DesignMode = " + DesignMode.ToString());
//Form1 constructor LicenseManager.UsageMode == DesignMode
MessageBox.Show("Form1 constructor LicenseManager.UsageMode = " + LicenseManager.UsageMode.ToString());}


private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
}


private void Form1_Load(object sender, System.EventArgs e)
{
// DesignMode = true if you open ChildForm
MessageBox.Show("Form1_Load DesignMode = " + DesignMode.ToString());
//Form1 constructor LicenseManager.UsageMode == RunTime
MessageBox.Show("Form1_Load constructor LicenseManager.UsageMode = " + LicenseManager.UsageMode.ToString());}

}


public class ChildForm : DesignModeExample.Form1
{
public ChildForm()
{
// This call is required by the Windows Form Designer.
InitializeComponent();
}}

If you open Child form in design time you will see two dialog. One will say that DesignMode in Form1 constructor = false. Another that DesignMode in Load event = true. LicenseManager.UsageMode works opposite. It works properly in constructor but lies in Load event handler.

Why is it so? I don't know, but I just tested it. It works this way.

Friday, February 25, 2005

TabControl with hidden page buttons

Sometime we used TPageControl in Delphi to switch between views in the same form. We did not want to show any tabs or buttons it this scenario.

To apply same technique in .NET, different set of properties should be used. Here they are:
TabControl.SizeMode = Fixed
TabControl.ItemSize = (0, 1);
TabConrol.Appearence = Buttons;