title: Fill exact pattern in String
body: <p>I have a textbox. The textbox contains the pattern of a guid, looking like:</p>
  
  <p>{0000000-0000-0000-0000-000000000000}</p>
  
  <p>I want the user, who types in the textbox, to only replace the zeros with their value.</p>
  
  <p>F.e.
  I type "ABC123456789" the GUID should look like {ABC1234-5678-9000-0000-000000000000}</p>
  
  <p>I tried something like:</p>
  
  <pre><code>string tmp = "";
  tmp = textBox1.Text.Substring(replaceIndex, 1);
  
  if (textBox1.Text.Length &gt; replaceIndex)
  {
      Console.WriteLine(tmp);
      if (tmp == "-")
          replaceIndex++;
  
      textBox1.Select(replaceIndex, 1);
      replaceIndex++;
  }
  </code></pre>
  
  <p>Which works partly. I have problems with the user deleting chars. F.e. if my GUID contains:
  {ABC1234-5 <strong>|</strong> 678-9000-0000-000000000000}</p>
  
  <p>My cursor is positioned at the "<strong>|</strong>". If I press backspace (or mark a substring f.e. C1234 and press delete) I want the 5 removed and replaced with 0 again.</p>
  
  <p>I think I've got some troubles with the index or my logic is completely wrong. Please tell me if so.</p>
  
  <p>I've tried to solve it like:</p>
  
  <pre><code>if (e.KeyChar == (char)Keys.Back)
  {
      replaceIndex--;
      int tmpIndex = replaceIndex;
      string xReplace = defaultGUID.Substring(tmpIndex, 1);
      textBox1.Text = textBox1.Text.Insert(tmpIndex, xReplace);
  }
  </code></pre>
  
  <p>Maybe there's a complete different solution to my problem I haven't thought about. Thank you in advance.</p>
  
  <p>//Edit: Working with WindowsForms.</p>