readonly richtextbox

  • Thread starter Thread starter blah
  • Start date Start date
B

blah

if i change my rtb to readonly then when a user presses a keyboad key it
'bings'. how do i stop the noise evertime a user presses a key?

thanks,
Rob
 
Hi Blah,

I think you should use keyboard hook to get this done.
In .Net, if you want to use keyboard hook, you must P/invoke some Win32 API.
Sample code lists below:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices ;

namespace readonlyrichtextbox
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{

public const int WM_KEYDOWN = 0x100;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox
richTextBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

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

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <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.userControl11 = new nobeeprichtextbox.UserControl1();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 200);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 40);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// userControl11
//
this.userControl11.Location = new System.Drawing.Point(24, 8);
this.userControl11.Name = "userControl11";
this.userControl11.ReadOnly = true;
this.userControl11.Size = new System.Drawing.Size(240, 152);
this.userControl11.TabIndex = 3;
this.userControl11.Text = "userControl11";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.userControl11);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

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

//Import for SetWindowsHookEx function.
//Use this function to install thread-specific hook.
[DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
IntPtr hInstance, int threadId);

//Import for UnhookWindowsHookEx.
//Call this function to uninstall the hook.
[DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);

//Import for CallNextHookEx.
//Use this function to pass the hook information to next hook procedure
in chain.
[DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode,
IntPtr wParam, IntPtr lParam);

public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
static int hHook = 0;
HookProc KeyboardHookProcedure;
public int WH_KEYBOARD = 2;
public static int HC_ACTION = 3;

[DllImport("user32.dll")]
public static extern IntPtr GetFocus();




public static int KeyboardHookProc(int nCode, IntPtr wParam, IntPtr
lParam)
{
if (nCode < 0)
{
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
else
{
if(nCode == HC_ACTION)
{
if(GetFocus()==wnd_hook)
{
return 1;
}
}

return CallNextHookEx(hHook, nCode, wParam, lParam);
}
}

private void button1_Click(object sender, System.EventArgs e)
{
if(hHook == 0)
{
// Create an instance of HookProc.
KeyboardHookProcedure = new HookProc(Form1.KeyboardHookProc );

hHook = SetWindowsHookEx(WH_KEYBOARD,
KeyboardHookProcedure,
(IntPtr)0,
AppDomain.GetCurrentThreadId());
//If SetWindowsHookEx fails.
if(hHook == 0 )
{
MessageBox.Show("SetWindowsHookEx Failed");
return;
}
button1.Text = "UnHook Windows Hook";
}
else
{
bool ret = UnhookWindowsHookEx(hHook);
//If UnhookWindowsHookEx fails.
if(ret == false )
{
MessageBox.Show("UnhookWindowsHookEx Failed");
return;
}
hHook = 0;
button1.Text = "Set Windows Hook";
this.Text = "Keyboard Hook";
}

}

private void Form1_Load(object sender, System.EventArgs e)
{
wnd_hook=this.richTextBox1.Handle ;
}


}
}

It works well on my machine, if you still have anything unclear, please
feel free to let me know.
For how to set windows hook in .Net, please refer to:
http://support.microsoft.com/default.aspx?scid=kb;en-us;318804

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "blah" <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Subject: readonly richtextbox
| Lines: 7
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <hoBmb.84876$Ms2.26305@fed1read03>
| Date: Sat, 25 Oct 2003 13:52:21 -0700
| NNTP-Posting-Host: 68.98.3.94
| X-Complaints-To: (e-mail address removed)
| X-Trace: fed1read03 1067115149 68.98.3.94 (Sat, 25 Oct 2003 16:52:29 EDT)
| NNTP-Posting-Date: Sat, 25 Oct 2003 16:52:29 EDT
| Organization: Cox Communications
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!news-out.cwix.com!newsfeed.cwix.co
m!prodigy.com!in.100proofnews.com!in.100proofnews.com!cox.net!news-xfer.cox.
net!p01!fed1read03.POSTED!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:194059
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| if i change my rtb to readonly then when a user presses a keyboad key it
| 'bings'. how do i stop the noise evertime a user presses a key?
|
| thanks,
| Rob
|
|
|
 
Back
Top