none
TextBox+Button Custom Control RRS feed

  • Question

  •  TextBoxButtonControl txtbtnControl;
            DataGridViewTextBoxColumn Code, Description,data;
            public Form1()
            {
                InitializeComponent();
                this.Code = new DataGridViewTextBoxColumn();
                this.Code.Name = "FilePath";
                this.Code.Width = 300;
                this.Code.Resizable = DataGridViewTriState.False;
                this.dataGridView1.Columns.Add(Code);

                this.Description = new DataGridViewTextBoxColumn();
                this.Description.Name = "Description";
                this.Description.Width = 150;
                this.dataGridView1.Columns.Add(Description);

                //adding textboxbutton control at code column...
                this.txtbtnControl = new TextBoxButtonControl();
                this.txtbtnControl.Visible = false;
                this.dataGridView1.Controls.Add(this.txtbtnControl);

                this.txtbtnControl.btnCode.Click += new EventHandler(btnCode_Click);
                this.txtbtnControl.Leave += new EventHandler(txtbtnControl_Leave);
                this.txtbtnControl.txtCode.TextChanged += new EventHandler(txtCode_TextChanged);

                this.dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
                this.dataGridView1.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_ColumnHeaderMouseClick);
                this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                this.dataGridView1.Columns.Add("", "");
                this.dataGridView1.Columns[0].DefaultCellStyle.Padding =
            new Padding(20, 0, 0, 0);
                this.dataGridView1.CellPainting += new
            DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
                this.dataGridView1.CellClick += new
            DataGridViewCellEventHandler(dataGridView1_CellClick);

                this.dataGridView1.RowCount = 5;
            }

            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                DataGridView grid = sender as DataGridView;
                int pad =
                grid.Columns[e.ColumnIndex].DefaultCellStyle.Padding.Left;
                if (pad > 5)
                {
                    Rectangle rect = grid.GetCellDisplayRectangle(e.ColumnIndex,
            e.RowIndex, false);
                    rect.X += rect.Width - pad;
                    rect.Y += 2;
                    rect.Width = pad;
                    rect.Height = rect.Height - 4;
                    if (rect.Contains(grid.PointToClient(Control.MousePosition)))
                    {
                        //MessageBox.Show("Click");
                        OpenFileDialog dialog = new OpenFileDialog();
                        if (dialog.ShowDialog() == DialogResult.OK)
                        {
                            try
                            {
                                dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value = dialog.FileName.ToString();
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }
            }       

                private void txtCode_TextChanged(object sender, EventArgs e)
                {
                    if (this.txtbtnControl.txtCode.Modified)
                    {
                        this.dataGridView1.CurrentRow.Cells[Code.Name].Value = this.txtbtnControl.txtCode.Text;
                    }
                }

                private void txtbtnControl_Leave(object sender, EventArgs e)
                {
                    try
                    {
                        this.txtbtnControl.Visible = true;
                        this.txtbtnControl.txtCode.Text = "";
                    }
                    catch (Exception)
                    {
                    }
                }

                private void btnCode_Click(object sender, EventArgs e)
                {
                    //Form2 form2 = new Form2();
                    //form2.ShowDialog();
                    OpenFileDialog dialog = new OpenFileDialog();
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        try
                        {
                            dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value = dialog.FileName.ToString();
                            txtbtnControl.txtCode.Text = dialog.FileName.ToString();
                        }
                        catch (Exception)
                        {
                        }
                    }
                }

                private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
                {

                }

                private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
                {
                    if (e.CellStyle.Padding.Left > 5)
                    {
                        DataGridView grid = sender as DataGridView;
                        e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
                        Rectangle rect = e.CellBounds;
                        rect.X += rect.Width - (e.CellStyle.Padding.Left - 1);
                        rect.Y += 2;
                        rect.Width = e.CellStyle.Padding.Left - 1;
                        rect.Height = rect.Height - 4;
                        ButtonState state = (Control.MouseButtons ==
                    MouseButtons.Right
                                            &&
                    rect.Contains(grid.PointToClient(Control.MousePosition)))
                                        ? ButtonState.Pushed : ButtonState.Normal;
                        ControlPaint.DrawButton(e.Graphics, rect, state);

                        StringFormat formater = new StringFormat();//added
                        formater.Alignment = StringAlignment.Center;//added
                        e.Graphics.DrawString("...", e.CellStyle.Font, new SolidBrush(e.CellStyle.ForeColor), rect, formater);//added
                        e.Handled = true;
                    }
                }
            }

            class TextBoxButtonControl : UserControl
            {
                public TextBox txtCode;
                public Button btnCode;

                public TextBoxButtonControl()
                {
                    this.txtCode = new TextBox();
                    this.Controls.Add(this.txtCode);
                    this.btnCode = new Button();
                    this.Controls.Add(this.btnCode);
                    this.renderControl();
                }
                public void renderControl()
                {
                    this.txtCode.Location = new Point(0, 0);
                    //this.txtCode.Width = 2 * this.Width / 3 - 17;
                    this.txtCode.Width = this.Width + 115;
                    this.txtCode.Height = this.Height;
                    this.btnCode.Location = new Point(this.Width + 115, 0);
                    this.btnCode.Width = 32;
                    this.btnCode.Height = 21;
                }
            }

    While entering value in text box the button get invisible ...i want to it be visible...

    please rply.


    Manoj

    Friday, June 29, 2012 6:17 AM

Answers

All replies