Q:

Check occurence of space in same place

I want restrict user to pass only digits, letters, -, and single space. Problem is I don't want to have a situation where the user can put more than one space in same place like below:
Something  45 Last    Ter

As you see we got two places where more than one space has been placed after "Something" and after "Last". How can I change my code to be secure that situation can't happen?
'check whether Symbol contains only letters, digits or -  symbol otherwise break                                                
For Each c As Char In txtNazwa.Text
    If Not Char.IsLetterOrDigit(c) AndAlso c <> "-"c AndAlso c <> " " Then
        MessageBox.Show("Only letters, digits, - and single space are available", "Ostrzeżenie", MessageBoxButtons.OK, MessageBoxIcon.Warning)
         Exit Try
    End If
Next

A:

I assume the user is typing into a text box. Look at the textbox change event.
private void currencyTextBox_TextChanged(object sender, EventArgs e)
{
   try
   {
      currencyTextBox.Text = Regex.Replace(currencyTextBox.Text, " {2 }", " ")
   }
   catch
   {
      // Do something if we get an error
   }
}

A:

A direct answer to the question:
Check if the string contains 2 consecutive spaces like this:
If txtNazwa.Text.Contains("  ") Then
    MessageBox.Show("The value must not contain two consecutive spaces")
End If

If instead, you want to fix this for the user, you can do this:
Dim str As String = txtNazwa.Text

While str.Contains("  ")
    str = str.Replace("  ", " ")
End While

txtNazwa.Text = str

