Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I want a TextBox with an ISBN input where I will type the numbers and accordingly hyphens (-) will appear at intervals. To be precise, I want it in the following format:

123-4-56-789123-4

I don't want to use MaskedTextBox because it always displays the hyphens(-) even before the input. I want it to be more like when product keys are entered, the hyphens(-) appear automatically.

Please help.

Regards
Aman Chaurasia

What I have tried:

I tried using MaskedTextBox but it is not as good.
Posted
Updated 20-May-18 21:57pm

Add an event handler to capture the TextChanged event. Get the existing text from the TextBox and format it according to your rules, then set it back into the display. Don't forget to disable the event while you do your changes.
 
Share this answer
 
To add to what Richard suggests, the formatting itself isn't complicated:
private string ToISBNFormat(string s)
    {
    StringBuilder result = new StringBuilder(13 + 4);
    s = Regex.Replace(s, "-", "");
    int len = s.Length;
    if (len < 3) return s;
    result.AppendFormat("{0}-", s.Substring(0, 3));
    len -= 3;
    if (len >= 1) result.AppendFormat("{0}-", s[3]);
    len -= 1;
    if (len > 0) result.AppendFormat("{0}-", s.Substring(4, Math.Min(2, len)));
    len -= 2;
    if (len > 0) result.AppendFormat("{0}-", s.Substring(6, Math.Min(6, len)));
    len -= 6;
    if (len > 0) result.Append(s.Substring(12, Math.Min(1, len)));
    return result.ToString();
    }
You may also need to work on the cursor position: you can get and set it via the TextBox.SelectionStart property
 
Share this answer
 
Comments
Primo Chalice 22-May-18 1:49am    
So, shall I use the SelectionStart property in TextChanged event itself? Because I tried to call the fucntion in the TextChanged event and I even used SelectionStart but it did not work.
OriginalGriff 22-May-18 1:55am    
How did you use it? I can't see your screen!
Primo Chalice 22-May-18 2:45am    
private string ToISBNFormat(string s)
{
StringBuilder result = new StringBuilder(13 + 4);
s = Regex.Replace(s, "-", "");
int len = s.Length;
if (len < 3) return s;
result.AppendFormat("{0}-", s.Substring(0, 3));
len -= 3;
if (len >= 1) result.AppendFormat("{0}-", s[3]);
len -= 1;
if (len > 0) result.AppendFormat("{0}-", s.Substring(4, Math.Min(2, len)));
len -= 2;
if (len > 0) result.AppendFormat("{0}-", s.Substring(6, Math.Min(6, len)));
len -= 6;
if (len > 0) result.Append(s.Substring(12, Math.Min(1, len)));
return result.ToString();
}

private void txt_BookISBN_TextChanged(object sender, EventArgs e)
{
ToISBNFormat(txt_BookISBN.Text);
txt_BookISBN.Focus();
}
OriginalGriff 22-May-18 3:29am    
When you start asking questions about complex subjects line user input templating we do tend to assume that you know the basics.
What does your code do with the result that the function returns?
Primo Chalice 22-May-18 4:01am    
By the sound of your statement, I believe that I have made a silly mistake or a blunder. I apologize if I somehow irritated you. But I seriously had no intentions of doing so.

Basically, the code does nothing. The input is as it was before. Plain text. No formatting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900