1

I have a button which created at runtime

Button myBtn = new Button();
Initialize(myBtn);
myBtn.Click += myBtn_Click();
myBtn.Text="Add or Edit Text";//default text string

when user click myBtn, it will highlight existing text and it is editable and user can type any new text.

How can i make button control editable during click event?

After user clicking and typing any string, the result will be myBtn.Text = "Any Text"

Example: myBtn.Text="Add or Edit Text"; //before clicking

myBtn.Text="any text or string that user type it"; //after clicking

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Vincent
  • 145
  • 2
  • 11
  • What you actually want to do? seems to be confusing – Just code Dec 27 '18 at 05:20
  • 1
    We call this a `TextBox` – TheGeneral Dec 27 '18 at 05:25
  • @Justcode if a user click a button, existing button.text will highlight and user can now type anything he/she want and will update to new string – Vincent Dec 27 '18 at 05:28
  • @TheGeneral yes it is like a textbox but in a button control. – Vincent Dec 27 '18 at 05:30
  • This seems like a flagrant misuse of standard windows controls, just saying, who is going to know to click the button to change the text? however, you might want overlay a textbox on the button, or use a popup dialog to enter the text – TheGeneral Dec 27 '18 at 05:31
  • No need to overlay, in fact all controls have `Controls` collection and you can add another control to their control collection. – Reza Aghaei Dec 27 '18 at 07:28

2 Answers2

1

You can add a TextBox to the Button control and create EditableButton control having the following methods and properties:

  • EditMode: Gets or sets how editing will start. It can be OnPressF2, OnClick, Programmatically. Depending to the value of this property, editing will start by pressing F2 or on Click or by your code calling BeginEdit.

  • BeginEdit(): Starts editing by showing the TextBox.

  • EndEdit(): Ends editing and hides the text box and commit text of TextBox as text of the Button.
  • CancelEdit(): Cancels the editing and hides the text box.
  • IsEditing: Determines if the control is in edit mode and TextBox is visible.

  • Press Enter to end edit and committing the text.

  • Press Escape to cancel edit and discard changes.

EditableButton

Here is an example of a button control having above properties and methods. You can enhance it by adding some events like BeginingEditing or EndingEdit or exposing some properties or events of the TextBox.

using System;
using System.Windows.Forms;

public class EditablButton : Button
{
    private TextBox txt;
    public enum EditModes { OnPressF2, OnClick, Programmatically }
    public EditModes EditMode { get; set; } = EditModes.OnPressF2;
    public bool IsEditing => txt.Visible;
    public EditablButton()
    {
        txt = new TextBox();
        txt.TextAlign = HorizontalAlignment.Center;
        txt.Visible = false;
        txt.Multiline = true;
        txt.Dock = DockStyle.Fill;
        this.Controls.Add(txt);
        txt.KeyDown += Txt_PreviewKeyDown;
    }
    private void Txt_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Enter)
        {
            EndEdit();
            e.Handled = true;
            e.SuppressKeyPress = true;
        }
        else if (e.KeyData == Keys.Escape)
        {
            CancelEdit();
            e.Handled = true;
            e.SuppressKeyPress = true;
        }
    }
    public void BeginEdit()
    {
        txt.Text = this.Text;
        txt.SelectAll();
        txt.Visible = true;
        txt.Focus();
    }
    public void EndEdit()
    {
        this.Text = txt.Text;
        txt.Visible = false;
        this.Focus();
    }
    public void CancelEdit()
    {
        txt.Visible = false;
        this.Focus();
    }
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (!IsEditing && EditMode == EditModes.OnPressF2 && keyData == Keys.F2)
        {
            BeginEdit();
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
    protected override void OnClick(EventArgs e)
    {
        if (EditMode == EditModes.OnClick)
            BeginEdit();
        else
            base.OnClick(e);
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • this is almost what i'm looking for but instead of pressing F2, its would be better left mouse click – Vincent Dec 27 '18 at 07:55
  • I've created a property for that. `EditMode`, you can set it to `OnPressF2`, `OnClick` or `Programmatically`. After you dropped and instance of the control on form, just set its `EditMode` to `OnClick` to start editing by click. You can also change the default value in the code. – Reza Aghaei Dec 27 '18 at 08:06
  • I add something in my code. I want to capture the click event of button. `string newString = ""; myBtn.Click += (sender, args) => { newString = myBtn.Text; };` It seems it doesnt work , i think its because the click event is already implemented in button. – Vincent Dec 27 '18 at 09:47
  • `if (EditMode == EditModes.OnClick) BeginEdit();` It disables `Click` event becase it's not calling `base.OnClick(e);`. If for any reason you want `Click` event raise (while I disagree on this idea), you can uncomment the `else` statement and let `base.OnClick(e);` calls anyway, regardless of the edit mode. – Reza Aghaei Dec 27 '18 at 09:51
  • If you are interested to get notified about changed text, it's enough to subscribe `TextChange` event of the button. – Reza Aghaei Dec 27 '18 at 09:55
  • As a side-note (maybe off-topic) if you are creating an application for editing some control properties at run-time, I suggest using `PropertyGrid`. You may want to take a look at [this post](https://stackoverflow.com/a/40209045/3110834) and [this one](https://stackoverflow.com/a/51618536/3110834). – Reza Aghaei Dec 27 '18 at 11:18
  • It seems the post answers the question. Let me know if you have any question about the answer in scope of the question. – Reza Aghaei Jan 07 '19 at 16:49
  • `public EditModes EditMode { get; set; } = EditModes.OnClick;` worked in my case – Vincent Jan 08 '19 at 09:57
0

You can put a text box where User can write some text & then on button click you can set the text:

myBtn.Text=txt_value.Text;

Or you can try the following to Highlight the Text:

Make the TextBox by Default Visible=False;

On myBtn_Click

txt_value.Visible=True;
myBtn.FontBold=True;
myBtn.Text=txt_value.Text; //to change the text each time the button is clicked
Alina Anjum
  • 1,178
  • 6
  • 30
  • 53