I am a bit new to programming. I have some edits on my form, and for each edit completed it will have to do a procedure.
Currently, I use this code that works perfectly, but I am looking for a better way to accomplish this task. If I add another edit not have to repeat all the code.
string rgxExpr = @"(\w+)\s([\w\s]*)"; // Pega primeiro nome
string rgxExpr2 = @"\b(\w{1})"; // Abreviação
string s1, s2, s3, s4,s5;
/* EDIT 1 */
Match match1 = Regex.Match(edtAutor1.Text, rgxExpr);
Match match2 = Regex.Match(match1.Groups[1].ToString(), rgxExpr2);
s1 = match1.Groups[2].ToString().ToUpper() + ", " + match2.Groups[1].ToString().ToUpper();
// Apenas um autor
s4 = match1.Groups[2].ToString().ToUpper() + ", " + match1.Groups[1].ToString().ToUpper();
/* EDIT 2 */
Match match11 = Regex.Match(edtAutor2.Text, rgxExpr);
Match match22 = Regex.Match(match11.Groups[1].ToString(), rgxExpr2);
s2 = match11.Groups[2].ToString().ToUpper() + ", " + match22.Groups[1].ToString().ToUpper();
/* EDIT 3 */
Match match111 = Regex.Match(edtAutor3.Text, rgxExpr);
Match match222 = Regex.Match(match111.Groups[1].ToString(), rgxExpr2);
s3 = match111.Groups[2].ToString().ToUpper() + ", " + match222.Groups[1].ToString().ToUpper();
/* EDIT 4 */
Match match1111 = Regex.Match(edtAutor4.Text, rgxExpr);
Match match2222 = Regex.Match(match1111.Groups[1].ToString(), rgxExpr2);
s5 = match1111.Groups[2].ToString().ToUpper() + ", " + match2222.Groups[1].ToString().ToUpper();
if (radioButton1.Checked == true)
{
if (edtAutor1.Text != "")
{
/* Bibliográfia - 1 AUTOR */
if (string.IsNullOrWhiteSpace(edtAutor2.Text) && string.IsNullOrWhiteSpace(edtAutor3.Text))
{
addbb.dataGridView1.Rows.Add(s4+". ", abntrTitulo.Text+"." , abntrEdicao.Text +" Ed. ", abntrLocal.Text + ": ", abntrEditor.Text + ", ",
abntrAno.Text + ". ", abntrPaginas.Text +"p.");
}
/* Bibliográfia - 2 AUTORES */
if (string.IsNullOrWhiteSpace(edtAutor3.Text))
{
if (edtAutor2.Text != "")
{
addbb.dataGridView1.Rows.Add(s1 + "; " + s2 + ". ", abntrTitulo.Text + ".", abntrEdicao.Text + " Ed. ", abntrLocal.Text + ": ", abntrEditor.Text + ", ",
abntrAno.Text + ". ", abntrPaginas.Text + "p.");
}
}
/* Bibliográfia - 3 AUTORES */
if (string.IsNullOrWhiteSpace(edtAutor4.Text))
{
if (edtAutor1.Text != "" & edtAutor2.Text != "" & edtAutor3.Text != "")
{
addbb.dataGridView1.Rows.Add(s1 + "; " + s2 + "; " + s3 + ". ", abntrTitulo.Text + ".", abntrEdicao.Text + " Ed. ", abntrLocal.Text + ": ", abntrEditor.Text + ", ",
abntrAno.Text + ". ", abntrPaginas.Text + "p.");
}
}
/* Bibliográfia - 4 AUTORES */
if (edtAutor1.Text != "" & edtAutor2.Text != "" & edtAutor3.Text != "" & edtAutor4.Text != "")
{
addbb.dataGridView1.Rows.Add(s1 + "; " + s2 + "; " + s3 + ";" + s5 + ". ", abntrTitulo.Text + ".", abntrEdicao.Text + " Ed. ", abntrLocal.Text + ": ", abntrEditor.Text + ", ",
abntrAno.Text + ". ", abntrPaginas.Text + "p.");
}
}
else
{
MessageBox.Show("Você precisa preencher todos os campos obrigátorios (*). Por favor, verifique se todos os campos estão preenchidos corretamente!");
}
}
The code uses regex and separates the words of EDIT, reverses the order and abbreviates.
edtAutorX
textbox? \$\endgroup\$