Read-Only and edit states for a from

G

Guest

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
 
S

Stoitcho Goutsev \(100\)

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.ComponentModel.IContainer components;

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


}

MessageFilter msgFilter = new MessageFilter();
public Form1(int a)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
Application.AddMessageFilter(msgFilter);

}

/// <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.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(120, 320);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(56, 48);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "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.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CausesValidation = false;
this.ClientSize = new System.Drawing.Size(352, 374);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

class MessageFilter:IMessageFilter
{
private const int WM_KEYFIRST = 0x0100;
private const int WM_KEYLAST = 0x0109;
public bool BlockKeyboard = true;

public bool PreFilterMessage(ref Message m)
{
if(m.Msg >= WM_KEYFIRST && m.Msg <= WM_KEYLAST)
{
return this.BlockKeyboard;
}

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

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

this.msgFilter.BlockKeyboard = !this.msgFilter.BlockKeyboard;

}

}
 
G

Guest

Hi,

I appreciate your suggestion. But my form is in a DLL that is called by
other applications. So i do not have access to the Application class. Also
this form is only a small part of a bigger application and i do not want the
messages to other windows being blocked. Any ideas?

Thank You,
Vish



Stoitcho Goutsev (100) said:
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.ComponentModel.IContainer components;

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


}

MessageFilter msgFilter = new MessageFilter();
public Form1(int a)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
Application.AddMessageFilter(msgFilter);

}

/// <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.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(120, 320);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(56, 48);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "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.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CausesValidation = false;
this.ClientSize = new System.Drawing.Size(352, 374);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

class MessageFilter:IMessageFilter
{
private const int WM_KEYFIRST = 0x0100;
private const int WM_KEYLAST = 0x0109;
public bool BlockKeyboard = true;

public bool PreFilterMessage(ref Message m)
{
if(m.Msg >= WM_KEYFIRST && m.Msg <= WM_KEYLAST)
{
return this.BlockKeyboard;
}

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

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

this.msgFilter.BlockKeyboard = !this.msgFilter.BlockKeyboard;

}

}


Vish said:
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
 

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