// Returns false if the original name was invalid. private bool ValidateName() { if (!Regex.IsMatch(tbName.Text, "^[A-Za-z_][A-Za-z0-9_]*$")) { string strOriginal = tbName.Text; string strNew = tbName.Text; // Fixup the name so that if the user tries to validate again, it will work. // First, auto-generate a name if the field is blank if (strNew == "") { strNew = m_ss.GenerateUniqueSpriteName(); } ; // Replace invalid characters with an underscore strNew = Regex.Replace(strNew, "[^A-Za-z0-9_]", "_"); // Make sure string begins with a letter if (!Regex.IsMatch(strNew, "^[A-Za-z_]")) { strNew = "S" + strNew; } // "Invalid sprite name. // Valid names must begin with a letter and contain only 'A'-'Z', 'a'-'z', '0'-'9' and '_' characters. // Fixing up sprite name - changing from '{0}' to '{1}'." m_doc.ErrorId("ErrorInvalidSpriteName", strOriginal, strNew); // Update the sprite name. tbName.Text = strNew; // The name is now valid, but we need to cancel the current operation so that the // user has a chance to read the error message displayed above before continuing. return(false); } return(true); }