Vish,
It looks like you don't want to disable the form because this will change
the outlook of all controls.
I don't know it there is no better solution, but the one that comes to my
mind is to use messge filter and filter out keyboard and mouse events before
they reach the child controls.
Here is some sample code that blocks all keyboard messages, thus prevents
user of typing in the edit boxes.
Ofcourse this is only an idea. a start for really workable solution. For
example you want to block some messages send to some controls on the form,
but not to all controls; you wanna block mouse messeges to check boxes, but
not to the button that switches the readonly mode on and off.
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.Button button1;
private System.Windows. Forms.TextBox textBox1;
private System.Windows. Forms.CheckBox checkBox1;
private System.Componen tModel.IContain er components;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
}
MessageFilter msgFilter = new MessageFilter() ;
public Form1(int a)
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
Application.Add MessageFilter(m sgFilter);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.textBox1 = new System.Windows. Forms.TextBox() ;
this.checkBox1 = new System.Windows. Forms.CheckBox( );
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(120, 320);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// textBox1
//
this.textBox1.L ocation = new System.Drawing. Point(56, 48);
this.textBox1.N ame = "textBox1";
this.textBox1.T abIndex = 1;
this.textBox1.T ext = "textBox1";
//
// checkBox1
//
this.checkBox1. Location = new System.Drawing. Point(56, 88);
this.checkBox1. Name = "checkBox1" ;
this.checkBox1. TabIndex = 2;
this.checkBox1. Text = "checkBox1" ;
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.CausesVali dation = false;
this.ClientSize = new System.Drawing. Size(352, 374);
this.Controls.A dd(this.checkBo x1);
this.Controls.A dd(this.textBox 1);
this.Controls.A dd(this.button1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
#endregion
class MessageFilter:I MessageFilter
{
private const int WM_KEYFIRST = 0x0100;
private const int WM_KEYLAST = 0x0109;
public bool BlockKeyboard = true;
public bool PreFilterMessag e(ref Message m)
{
if(m.Msg >= WM_KEYFIRST && m.Msg <= WM_KEYLAST)
{
return this.BlockKeybo ard;
}
return false;
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1(1));
}
private void button1_Click(o bject sender, System.EventArg s e)
{
this.msgFilter. BlockKeyboard = !this.msgFilter .BlockKeyboard;
}
}
"Vish" <Vi**@discussio ns.microsoft.co m> wrote in message
news:93******** *************** ***********@mic rosoft.com...
Hi,
I amplanning on having a rea-only and edit states for my form. But it do
not
want my form and its controls to look different or disabled. I am planning
on
having a edit button that brings the form from read-only to a edit state.
Is
there any easy or built-in way of doing this in .NET?
My preimary purpose for doing it because the combobox controls on my form
take a while to load all their data from the database. Since the user uses
the form only to view information most of the time, i dont want the form
to
load the data eveytime. The data will only be loaded when the user clicks
on
the edit button.
Thank you,
Vish