Hi everyone
I put a RadGridView in Form with enabled its edit mode (AllowEditRow = true), and has "id" and "name" columns, and three rows of data. Also I created a custom user control that has a TextBox and a Button (named it ccTest). Then I choose a cell from "name" column and any row at run time. Now I start to type letters from keyboard. In this case, I bring up the custom user control.
The problem is: The typed letters don't show in custom user control, because it's TextBox not focused yet. Therefore I must click one more time to focus it, before start type letters.
Question is: How can I focus the TextBox of custom user control when it appear from CellBeginEdit event?
Microsoft Visual Studio 2017 (C# language) installed on Windows 10 - Telerik WinForms 2017 R3 SP1 (2017.3.1017.40).
-------------------------------------------
ccTest.Designer.cs:
namespace TestGridFocus{ partial class ccTest { /// <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 Component 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.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill; this.textBox1.Location = new System.Drawing.Point(0, 0); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(92, 20); this.textBox1.TabIndex = 0; // // button1 // this.button1.Dock = System.Windows.Forms.DockStyle.Right; this.button1.Location = new System.Drawing.Point(92, 0); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(28, 20); this.button1.TabIndex = 1; this.button1.Text = "?"; this.button1.UseVisualStyleBackColor = true; // // ccTest // this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Name = "ccTest"; this.Size = new System.Drawing.Size(120, 20); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; }}
ccTest.cs:
using System.Windows.Forms;namespace TestGridFocus{ public partial class ccTest : UserControl { public ccTest() { InitializeComponent(); } public string Value { get { return textBox1.Text; } set { textBox1.Text = value; } } }}Form1.Designer.cs:
namespace TestGridFocus{ partial class Form1 { /// <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() { Telerik.WinControls.UI.GridViewDecimalColumn gridViewDecimalColumn1 = new Telerik.WinControls.UI.GridViewDecimalColumn(); Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn1 = new Telerik.WinControls.UI.GridViewTextBoxColumn(); Telerik.WinControls.UI.GridViewDecimalColumn gridViewDecimalColumn2 = new Telerik.WinControls.UI.GridViewDecimalColumn(); Telerik.WinControls.UI.TableViewDefinition tableViewDefinition1 = new Telerik.WinControls.UI.TableViewDefinition(); this.radGridView1 = new Telerik.WinControls.UI.RadGridView(); ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.radGridView1.MasterTemplate)).BeginInit(); this.SuspendLayout(); // // radGridView1 // this.radGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.radGridView1.Location = new System.Drawing.Point(12, 12); // // // this.radGridView1.MasterTemplate.AllowAddNewRow = false; gridViewDecimalColumn1.FieldName = "id"; gridViewDecimalColumn1.HeaderText = "id"; gridViewDecimalColumn1.Name = "id"; gridViewDecimalColumn1.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter; gridViewTextBoxColumn1.FieldName = "name"; gridViewTextBoxColumn1.HeaderText = "name"; gridViewTextBoxColumn1.Name = "name"; gridViewTextBoxColumn1.Width = 150; gridViewDecimalColumn2.FieldName = "age"; gridViewDecimalColumn2.HeaderText = "age"; gridViewDecimalColumn2.Name = "age"; gridViewDecimalColumn2.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter; this.radGridView1.MasterTemplate.Columns.AddRange(new Telerik.WinControls.UI.GridViewDataColumn[] { gridViewDecimalColumn1, gridViewTextBoxColumn1, gridViewDecimalColumn2}); this.radGridView1.MasterTemplate.ViewDefinition = tableViewDefinition1; this.radGridView1.Name = "radGridView1"; this.radGridView1.Size = new System.Drawing.Size(270, 237); this.radGridView1.TabIndex = 0; this.radGridView1.Text = "radGridView1"; this.radGridView1.CellBeginEdit += new Telerik.WinControls.UI.GridViewCellCancelEventHandler(this.radGridView1_CellBeginEdit); this.radGridView1.CellEndEdit += new Telerik.WinControls.UI.GridViewCellEventHandler(this.radGridView1_CellEndEdit); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(294, 261); this.Controls.Add(this.radGridView1); this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.radGridView1.MasterTemplate)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit(); this.ResumeLayout(false); } #endregion private Telerik.WinControls.UI.RadGridView radGridView1; }}
Form1.cs:
using System;using System.Data;using System.Windows.Forms;using Telerik.WinControls;namespace TestGridFocus{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private DataTable GetData() { DataTable dt = new DataTable(); dt.Columns.Add("id", typeof(int)); dt.Columns.Add("name", typeof(string)); dt.Rows.Add(1, "Jack"); dt.Rows.Add(2, "Joe"); dt.Rows.Add(3, "Bob"); dt.AcceptChanges(); return dt; } private void Form1_Load(object sender, EventArgs e) { radGridView1.DataSource = GetData(); } ccTest test = new ccTest(); private void radGridView1_CellBeginEdit(object sender, Telerik.WinControls.UI.GridViewCellCancelEventArgs e) { if (e.Column == radGridView1.Columns["name"]) { RadHostItem item = new RadHostItem(test); test.Value = e.Row.Cells[e.ColumnIndex].Value.ToString(); radGridView1.CurrentCell.Children.Add(item); item.HostedControl.Focus(); } } private void radGridView1_CellEndEdit(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e) { if (e.Column == radGridView1.Columns["name"]) { e.Row.Cells[e.ColumnIndex].Value = test.Value; radGridView1.CurrentCell.Children.RemoveAt(0); } } }}
