Remove TextBox C Cursor

7

When a textBox takes the focus A flickering bar appears ... How can I make it so that it does not show up?

    
asked by Efrain Mejias C 13.04.2017 в 18:36
source

2 answers

10

Well, I've achieved it in some way with the code that I've put you in comment .

In the Form class that implements those TextBox controls, you do the following:

  • You make using System.Runtime.InteropServices; at the beginning of the file MiForm.cs (Where MiForm is the name of the code file corresponding to the Form you use.)

  • You define the external method HideCaret :

    [DllImport("user32.dll")]
    static extern bool HideCaret(IntPtr hWnd);
  • Then for each TextBox you set the event GotFocus in the following way:

    foreach (TextBox item in MiColeccionDeTextBoxes) {
        item.GotFocus += delegate { HideCaret(item.Handle); }
    }
    

    And with that you have what you need to complete the Form , below I leave the whole code of a Form1.cs that I made while trying:

    // Recuerda el using System.Runtime.InteropServices;
    
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        static extern bool HideCaret(IntPtr hWnd);
    
        public Form1()
        {
            InitializeComponent();
            TextBox[] texts = { txt1, txt2 };
            foreach (TextBox item in texts)
                item.GotFocus += delegate { HideCaret(item.Handle);  };
        }
    
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
    
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (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.txt1 = new System.Windows.Forms.TextBox();
            this.txt2 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // txt1
            // 
            this.txt1.Location = new System.Drawing.Point(12, 48);
            this.txt1.MaxLength = 1;
            this.txt1.Name = "txt1";
            this.txt1.Size = new System.Drawing.Size(100, 20);
            this.txt1.TabIndex = 0;
            // 
            // txt2
            // 
            this.txt2.Location = new System.Drawing.Point(12, 74);
            this.txt2.MaxLength = 1;
            this.txt2.Name = "txt2";
            this.txt2.Size = new System.Drawing.Size(100, 20);
            this.txt2.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Controls.Add(this.txt2);
            this.Controls.Add(this.txt1);
            this.Name = "Form1";
            this.Text = "Test Focus";
            this.ResumeLayout(false);
            this.PerformLayout();
    
        }
    
        #endregion
    
        private System.Windows.Forms.TextBox txt1;
        private System.Windows.Forms.TextBox txt2;
    }
    
        
    answered by 18.04.2017 / 04:02
    source
    3

    I would advise you not to use TextBox to represent each square, you could use a picturebox and the number you draw it

    How to draw text on picturebox?

    Drawing Graphics in C Sharp

    This way you will have the events but you control the actions, it will not be as direct as assigning the .Text, but you will not have any cursor on the control

    To make the number assignment simpler if you want to use the Paint event, you can see if with

    Graphics g = pictureBox1.CreateGraphics();
    

    or maybe something like being

    Bitmap bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    
    using (Graphics g = Graphics.FromImage(bm))
    {
        using (SolidBrush myBrush = new SolidBrush(Color.Black))
        {
            using (Font myFont = new Font("Times New Roman", 24))
            {
                g.DrawString("aqui numero", myFont, myBrush, 10, 10);
                pictureBox1.Image = bm;
            }
        }
    }
    

    When you want to show a number you draw it in an image and assign it to the picturebox, if you need to assign a background color you can always do it without problems

        
    answered by 18.04.2017 в 13:20