readonly richtextbox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • blah

    #1

    readonly richtextbox

    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


  • Jeffrey Tan[MSFT]

    #2
    RE: readonly richtextbox


    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.Collecti ons;
    using System.Componen tModel;
    using System.Windows. Forms;
    using System.Data;
    using System.Runtime. InteropServices ;

    namespace readonlyrichtex tbox
    {
    /// <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.RichTextB ox
    richTextBox1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.Componen tModel.Containe r components = null;

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

    //
    // TODO: Add any constructor code after InitializeCompo nent call
    //
    }

    /// <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.userContro l11 = new nobeeprichtextb ox.UserControl1 ();
    this.SuspendLay out();
    //
    // button1
    //
    this.button1.Lo cation = new System.Drawing. Point(104, 200);
    this.button1.Na me = "button1";
    this.button1.Si ze = new System.Drawing. Size(88, 40);
    this.button1.Ta bIndex = 1;
    this.button1.Te xt = "button1";
    this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
    //
    // userControl11
    //
    this.userContro l11.Location = new System.Drawing. Point(24, 8);
    this.userContro l11.Name = "userControl11" ;
    this.userContro l11.ReadOnly = true;
    this.userContro l11.Size = new System.Drawing. Size(240, 152);
    this.userContro l11.TabIndex = 3;
    this.userContro l11.Text = "userControl11" ;
    //
    // Form1
    //
    this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
    this.ClientSize = new System.Drawing. Size(292, 266);
    this.Controls.A dd(this.userCon trol11);
    this.Controls.A dd(this.button1 );
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHan dler(this.Form1 _Load);
    this.ResumeLayo ut(false);

    }
    #endregion

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

    //Import for SetWindowsHookE x function.
    //Use this function to install thread-specific hook.
    [DllImport("user 32.dll",CharSet =CharSet.Auto,
    CallingConventi on=CallingConve ntion.StdCall)]
    public static extern int SetWindowsHookE x(int idHook, HookProc lpfn,
    IntPtr hInstance, int threadId);

    //Import for UnhookWindowsHo okEx.
    //Call this function to uninstall the hook.
    [DllImport("user 32.dll",CharSet =CharSet.Auto,
    CallingConventi on=CallingConve ntion.StdCall)]
    public static extern bool UnhookWindowsHo okEx(int idHook);

    //Import for CallNextHookEx.
    //Use this function to pass the hook information to next hook procedure
    in chain.
    [DllImport("user 32.dll",CharSet =CharSet.Auto,
    CallingConventi on=CallingConve ntion.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 KeyboardHookPro cedure;
    public int WH_KEYBOARD = 2;
    public static int HC_ACTION = 3;

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




    public static int KeyboardHookPro c(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(o bject sender, System.EventArg s e)
    {
    if(hHook == 0)
    {
    // Create an instance of HookProc.
    KeyboardHookPro cedure = new HookProc(Form1. KeyboardHookPro c );

    hHook = SetWindowsHookE x(WH_KEYBOARD,
    KeyboardHookPro cedure,
    (IntPtr)0,
    AppDomain.GetCu rrentThreadId() );
    //If SetWindowsHookE x fails.
    if(hHook == 0 )
    {
    MessageBox.Show ("SetWindowsHoo kEx Failed");
    return;
    }
    button1.Text = "UnHook Windows Hook";
    }
    else
    {
    bool ret = UnhookWindowsHo okEx(hHook);
    //If UnhookWindowsHo okEx fails.
    if(ret == false )
    {
    MessageBox.Show ("UnhookWindows HookEx Failed");
    return;
    }
    hHook = 0;
    button1.Text = "Set Windows Hook";
    this.Text = "Keyboard Hook";
    }

    }

    private void Form1_Load(obje ct sender, System.EventArg s e)
    {
    wnd_hook=this.r ichTextBox1.Han dle ;
    }


    }
    }

    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...b;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" <blah@blah.co m>
    | Newsgroups: microsoft.publi c.dotnet.langua ges.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$Ms 2.26305@fed1rea d03>
    | Date: Sat, 25 Oct 2003 13:52:21 -0700
    | NNTP-Posting-Host: 68.98.3.94
    | X-Complaints-To: abuse@cox.net
    | 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.ph x.gbl!TK2MSFTNG P08.phx.gbl!new s-out.cwix.com!ne wsfeed.cwix.co
    m!prodigy.com!i n.100proofnews. com!in.100proof news.com!cox.ne t!news-xfer.cox.
    net!p01!fed1rea d03.POSTED!not-for-mail
    | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1940 59
    | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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
    |
    |
    |

    Comment

    Working...