Windows Forms Drawing Problem

G

Guest

The problem I am having is that under certain circumstances,
text fails to draw in some controls and dialogs. The problem
occurs primarily in the drawing of ListControls and any Dialogs
generated via MessageBox.Show. In the ComboBox, the items are
in the dropdown, and can be moused-over, highlighted and selected.
When an item is selected, the text is visible in the textbox portion
of the ComboBox control. I am not doing any sort of custom or owner
drawing and have not interfered with the firing of events at all.

The problem appears to be localized to a few machines. The text fails
to draw on two of the =seven machines I've tested this application on.

Suspecting an environment issue, I have uninstalled vstudio & .net
and re-installed, to no avail. (I am using .Net 1.1 SP1 & VS2003)

I am able to work around this, using a hack involving
calls to Invoke on a worker thread (The full source is at the end of the
post):

/// <code>
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

#region HACK
// Ensure the handle has been created so the thread can safely call Invoke.
this.CreateHandle();
System.Threading.Thread thread = new System.Threading.Thread( new
System.Threading.ThreadStart( ThreadProc ) );
thread.Start();
#endregion
}

#region HACK
private delegate void voidfunc();
private void ThreadProc()
{
this.Invoke( new voidfunc( EmptyFunc ) );
}

private void EmptyFunc()
{
// Do nothing
}
#endregion
/// </code>


For clarity, this is how I refer to my threads:
"control thread": The thread on which the control/form's constructor was
called.
"worker thread": Any thread that is not the control thread.

Calling Invoke (or BeginInvoke) from the control thread has no effect
on the drawing issue. Making the call from a worker thread is the key to
the hack.

I first suspected that the fix had to do with calling CreateHandle, but it
doesn't.
CreateHandle alone also does not work. It does, however, allow me to safely
call
Invoke from the worker thread without having to worry about timing issues.
(In this
sample code, I first just used Thread.Sleep(1) to pause just long enough for
the
handle to be created elsewhere).

I am quite perplexed by this, and am at a loss to explain the effect of
Invoke.

Any help/suggestions as to a fix or explanation are appreciated.

Thanks,
Nathan Ernst
ernsnat{at)iit(dot}edu



/// <code>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DropDownTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

#region HACK
// Ensure the handle has been created so the thread can safely call Invoke.
this.CreateHandle();
this.Invoke( new voidfunc( EmptyFunc ) );
#endregion
}

#region HACK
private delegate void voidfunc();
private void ThreadProc()
{
this.Invoke( new voidfunc( EmptyFunc ) );
}

private void EmptyFunc()
{
// Do nothing
}
#endregion


/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// comboBox1
//
// this.comboBox1.Font = new System.Drawing.Font("Tahoma", 9.75F,
System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
this.comboBox1.ForeColor = System.Drawing.Color.Black;
this.comboBox1.Items.AddRange(new object[] {
"Test1",
"Test2",
"Test3"});
this.comboBox1.Location = new System.Drawing.Point(40, 40);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 24);
this.comboBox1.TabIndex = 0;
this.comboBox1.Text = "comboBox1";
this.comboBox1.SelectedIndexChanged += new
System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.comboBox1);
// this.Font = new System.Drawing.Font("Tahoma", 8.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{

}

private void comboBox1_DropDown(object sender, EventArgs e)
{

}
}
}
/// </code>
 
G

Guest

Thanks! This definitely looks like the problem. Unfortunately, I don't have
priveleges to remove or disable the scan. So, I've got our PC group looking
into patch the virus scan.

-Nathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top