Permanent prefix in a text box

I'm trying to have a persistent prefix input in a textbox. In my case, I want to have the following prefix:

DOMAIN\

So that users can only enter their username after the domain prefix. This is not something I should be doing, or pursuing, but my question is more out of curiosity.

I was trying to come up with some logic to do this on TextChangedEvent

, however that means I need to know which characters have been removed and then pre-add DOMAIN\

to any of their input - I can "Develop logic for this, so I cannot post what i tried other than where i got it.

public void TextBox1_TextChanged(object sender, EventArgs e)
{
  if(!TextBox1.Text.Contains(@"DOMAIN\")
  {
    //Handle putting Domain in here along with the text that would be determined as the username
  }
}

      

I looked online and found nothing, How do I prefix the text in a winforms textbox with unlinked text? tried a similar thing, but the answers don't really help.

Any ideas on how to store the prefix DOMAIN\

in TextBox

?

+3


source to share


4 answers


This is the KISS principle. Attempting to catch keystrokes will simply do nothing if the user is using Ctrl + V or the Cut and Paste context menus. Just restore the text if anything happens that the prefix has undergone:

    private const string textPrefix = @"DOMAIN\";

    private void textBox1_TextChanged(object sender, EventArgs e) {
        if (!textBox1.Text.StartsWith(textPrefix)) {
            textBox1.Text = textPrefix;
            textBox1.SelectionStart = textBox1.Text.Length;
        }
    }

      



And help the user avoid editing the prefix accidentally:

    private void textBox1_Enter(object sender, EventArgs e) {
        textBox1.SelectionStart = textBox1.Text.Length;
    }

      

+8


source


Why not see the previous value and the new value in the arguments of the textChanged event, and if it Domain\

doesn't exist in the new value, keep the old one.



Or, why not just show it Domain\

as a label in front of the TextBox and just add it to the code behind so the final text is something like Domain\<username>

.

+1


source


Yes, I somehow solved it as soon as I asked the question ... I will not rule out the question in case anyone else has the same question in the future, because I could not find a suitable answer. I set Text

to Domain\

and then used event KeyPress

.

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
  e.Handled = (textBox1.GetCharIndexFromPosition(Cursor.Position) < 7);
}

      

I try to work as soon as I ask, instead of letting people do all the work for me :)

0


source


How about a multiple custom TextBox control. There are comments in the code

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        textBox1.Prefix = @"DOMAIN\";
    }
}

class PrefixedTextBox : TextBox
{
    private string _prefix = String.Empty;
    public string Prefix
    {
        get { return _prefix; }
        set
        {
            _prefix = value;
            Text = value;
        }
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        // Don't allow Backspace and Delete if the only text is Prefix
        if (Text == Prefix && (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete))
            e.Handled = true;

        // If home key is pressed set cursor just after the prefix
        if (e.KeyCode == Keys.Home)
        {
            e.Handled = true;
            SelectionStart = Prefix.Length;
        }

        // Don't allow cursor to be moved inside Prefix
        if (SelectionStart <= Prefix.Length && (e.KeyCode == Keys.Left || e.KeyCode == Keys.Up))
            e.Handled = true;

        base.OnKeyDown(e);
    }

    protected override void OnClick(EventArgs e)
    {
        EnsureCursorPosition();
        base.OnClick(e);
    }

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        EnsureCursorPosition();

        // this was checked OnKeyDown. This prevents deleting and writing back behaviour
        if (Text == Prefix && e.KeyChar == '\b')
            e.Handled = true;

        base.OnKeyPress(e);
    }

    protected override void OnKeyUp(KeyEventArgs e)
    {
        // Yet, some how an invalid text is entered fix it by just displaying the Prefix
        if (!Text.StartsWith(Prefix))
            Text = Prefix;

        base.OnKeyUp(e);
    }

    private void EnsureCursorPosition()
    {
        // Never allow cursor position before Prefix
        if (SelectionStart < Prefix.Length)
            SelectionStart = Text.Length;
    }
}

      

0


source







All Articles