C# asp.net check if textbox contains spaces - c#

I am trying to prevent the user from leaving the textbox empty and it worked with me using this code:
textbox1.Text != ""
but now I want to prevent entering spaces, I tried this code below but it doesn't prevent it:
if(!string.IsNullOrWhiteSpace(textbox1.Text))
is there any other way to prevent entering spaces?

Do you mean that you want to check if the value contains a space character? The string object has a method to check that:
if (!textBox1.Text.Contains(" "))

The following simply removes the spaces:
textbox1.Text = textbox1.Text.Replace(" " ,"");

Related

how to remove a certain set of characters from textbox

Ok so I'm having a problem where my converter is for some reason adding the characters "%22" to the output textbox for some of the users of my program. All I need is something that i can put into a timer that detects if "%22" is detected in the textbox, and if it is detected, it will be deleted (leaving no spaces if it is the middle of a word)
if (metroTextbox2.Text.Contains("%22")
{
metroTextbox2.Text.Remove("%22");
}
(That code above doesn't work btw. It leaves an error under ("%22") on the code "metroTextbox2.Text.Remove("%22");")
Remove gets int parameter and not string.
Please use String.Replace method instead.
I.e.
metroTextbox2.Text = metroTextbox2.Text.Replace("%22", string.Empty);
Instead using REMOVE try use REPLACE Like this :
if (!metroTextbox2.Text.Contains("%22")
{
metroTextbox2.Text.Replace("%22", "");
}

New line is not working in MessageBox in C#/WPF

Short question: I have a string in my resources: "This is my test string {0}\n\nTest"
I'm trying to display this string in my Messagebox:
MessageBox.Show(String.Format(Properties.Resources.About,
Constants.VERSION),
Properties.Resources.About_Title, MessageBoxButton.OK,
MessageBoxImage.Information);
However I don't get new lines. The \n still show up as characters, not as new lines.
I also tried to use a workaround like mystring.Replace("\n", Environment.NewLine) but this also doesn't change anything.
What am I doing wrong?
Edit: Funny thing to mention: Replace("\n", "somethingelse") doesn't change anything.
Edit2: Shift+Enter in my Resource-File instead of \n seems to work... Strange behaviour anyway
Put a place holder where you want to put new line and in the code where you use that resource string, just replace it with new line: string resource: "This is first line.{0}This is second line.{0}This is third line." You will use this resource string like this: MessageBox.Show(string.Format(MyStringResourceClass.MyStringPropertyName, Environment.NewLine));
OR
Unconventional Method
But i just now got it working by coping newline from word directly (or anyother place) & pasting it inside the resource string file.
It was simple..
OR
\r\n characters will be converted to new line when you display it by using message box or assign it to text box or whenever you use it in interface.
In C# (like most C derived languages), escape characters are used to denote special characters such as return and tab, and + is used in place of & for string concatenation.
To make your code work under C# you’ve got two options... the first is to simply replace the NewLine with the return escape character \n ala:
MessageBox.Show("this is first line" + "\n" + "this is second line");
The other method, and more correct is to replace it instead with Environment.NewLine which theoretically could change depending on the system you are using (however unlikely).
MessageBox.Show("this is first line" + Environment.NewLine + "this is second line");
In the resource editor seperate your string content by using shift+enter. Or else, edit your ResX file in xml editor and using enter key create a new line for your resource string.
Refer this link for detail info: Carriage Return/Line in ResX file.
Try this:
String outputMessage = string.Format("Line 1{0}Line 2{0}Line 3", Environment.NewLine);
MessageBox.Show(outputMessage);
A further example with another variable:
String anotherValue = "Line 4";
String outputMessage = string.Format("Line 1{0}Line 2{0}Line 3{0}{1}", Environment.NewLine, anotherValue);
MessageBox.Show(outputMessage);
Try this
removing the MessageBoxButtons.OK and MessageBoxImage. Information.
MessageBox.Show(String.Format(Properties.Resources.About,
Constants.VERSION),
Properties.Resources.About_Title);

HttpRequestValidationException workaround

I have a textbox that when user inputs a string such as "<daily" (to signify less than daily) it throws a HttpRequestValidationException. However if there is a space between the less than symbol and the string, it works fine such as "< daily".
I have had it change the value that is submitted in the code behind by using the replace function. For example:
string s = "This is a <test";
if(s.Contains("<")){
s = s.Replace("<", "< "); //I have also used "<" & "<"
}
However, I still get the exception because in the textbox it is still showing it as "<daily". I am wondering if there is a way that if the focus is off the textbox to dynamically add a space to the string?
I understand that the HttpRequestValidationException is not supposed to allow those characters, but it seems to allow if there are spaces. Any thoughts?
It would be nice to know how you use the string in the HttpRequest. Depending on how and where you use we could come up with some ideas.

Convert pasted text into single line

So I have a textbox where I want an address to be stored, but as a single line. To get the address in there I will always do a copy and paste, and the issue is that where I copy it from, it is multiline, so when I paste it, it only shows the first line. I know I can change it into a multiline textbox and then use backspace to make it one line, but I'm just wondering if there is a way I can just make it single line with code.
textBox1.Text = Clipboard.GetText().Replace(Environment.NewLine, " ");
Try,
textBox1.Text = textBox2.Text.Replace("\r\n", " ");
EDIT:
String singleLine=multiLine.Replace("\r\n"," ");
You can remove '\n' characters from the input and '\r' as well.

C# Reading a file and writing out replacing string

What I have is a C# windows app that reads a bunch of SQL tables and creates a bunch of queries based on the results. What I'm having a small issue with is the final "," on my query
This is what I have
ColumnX,
from
I need to read the entire file, write out exactly what is in the file and just replace the last , before the from with nothing.
I tried .replace(#",\n\nfrom),(#"\n\nfrom) but it's not finding it. Any help is appreciated.
Example:
ColumnX,
from
Result:
ColumnX
from
The line break is most likely the two character combination CR + LF:
.replace(",\r\n\r\nfrom","\r\n\r\nfrom")
If you want the line break for the current system, you can use the Environment.NewLine constant:
.replace(","+Environment.NewLine+Environment.NewLine+"from",Environment.NewLine+Environment.NewLine+"from")
Note that the # in front of a string means that it doesn't use backslash escape sequences, but on the other hand it can contain line breaks, so you could write it in this somewhat confusing way:
str = str.replace(#",
from", #"
from");
There are two solutions that you can try:
Remove the # symbol, as that means it's going to look for the literal characters of \n rather than a newline.
Try .replace("," + Environment.NewLine + Environment.NewLine + from, Environment.NewLine + Environment.NewLine + "from)
Instead of replacing or removing the comma when you read the file, it would probably be preferable to remove it before the file is written. That way you only have to bother with the logic once. As you are building your column list, just remove the last comma after the list is created. Hopefully you are in a position where you have control over that process.
If you can assume you always want to remove the last occurrence of the comma you can use the string function LastIndexOf to find the index for the last comma and use Remove from there.
myString = myString.Remove(myString.LastIndexOf(","), 1);
What about using Regex? Does that handle different forms of linefeed better?
var result = Regex.Replace(input, #",(\n*)from", "$1from");

Categories

Resources