How would I have an input of a variable length in the gridmaskedcolumn.
For this instance the grid is a password which must have at least 4 characters but can be longer than 4 characters and those characters must be numbers only. Also the characters must not be displayed.
<telerik:GridMaskedColumn DataField="Pin" UniqueName="Pin" HeaderText="Pin" Visible="false" Mask="<0..9>" DisplayMask="******" >
</telerik:GridMaskedColumn>
I tried reading for solutions online but it seems there are no solutions regarding a variable lengthen input.
From what I have been reading, GridMaskedColumn is not designed for variable-length input and therefore passwords are not ideal for GridMaskedColumns.
An alternative would be to use a normal GridBoundColumn and set the column mode to password.
if (e.Item is GridDataItem && e.Item.IsInEditMode)
{
GridDataItem edititem = (GridDataItem)e.Item;
TextBox txtpwd = (TextBox)edititem["Pin"].Controls[0];
txtpwd.TextMode = TextBoxMode.Password;
txtpwd.Visible = true;
}
Related
I'm new to C#, and finding some difficulties when trying to implement the 'ignore regex function when textbox is empty'.
As shown below, within Leave event I have made it so that the data submitted in the name textbox is of alphabetic characters, however, upon testing, the application still warns me that the textbox requires alphabetic characters even if the textbox is EMPTY / NULL.
What I would like is to maintain the same regex function, but I want the application to NOT warn me about the requirements if textboxes are left empty.
Many thanks in advance.
private void txtName_Leave(object sender, EventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(this.txtName.Text, "^[a-zA-Z ]"))
{
MessageBox.Show("This textbox accepts only alphabetical characters.", "Warning");
this.txtName.ResetText();
}
else if (txtName.Text.Trim() == string.Empty)
{
return;
}
}
What you need to use is ^[a-zA-Z]*$
^ Beginning of string
[a-zA-Z]* with the addition of the * it represents 0+ Alpha
characters
$ end of string
So I have a C# program that collects a list of customers. The list displays Customer ID, Customer Name, and Customer Phone Number. The list is shown in a multi-line text box. My issue is, a customer is allowed to either use first and last name, or first middle and last name, and when the list is displayed, if one of the customers only put a first and last name, the phone number is literally right next to the name instead of tabbed like the others. I will demonstrate what I mean below.
Notice how Bob Anthony's phone number is off compared to Mary and John? What would I use to make sure that every line has the same space in the tabs?
While some type of a data grid or list view would probably be more appropriate, if you want to keep it in string form you can use some of the composite formatting features in String.Format - notably the alignment flag:
string.Format("{0,-8} {1,-20} {2}", stuff)
The negative/positive indicates left/right alignment. Note that strings aren't truncated for you, you'll have to do that if you don't already know the max width.
You should get length of the longest item and add space after other items so that all of them be equal. By the way, I suggest you to use DataGridView for your application. Something like this:
dataGridView1.Rows.Add(3);
dataGridView1.Rows[0].Cells[0].Value = customer.ID;
dataGridView1.Rows[0].Cells[1].Value = customer.Name;
dataGridView1.Rows[0].Cells[2].Value = customer.Phone;
As everyone has said a DataGridView is recomendable and easy to use, instead of truncating the information or padding it the user can resize the columns. If you still want to do it that way then you have to get each customer and format it accordingly:
var text = new StringBuilder();
foreach (var customer in Customers)
{
var format = String.Format("{0} {1} {2}\n",
FormatField(customer.Id,6),
FormatField(customer.Name,20),
FormatField(customer.Phone,10) // Might need extra formating
);
text.Append(format);
}
And to format it:
private string FormatField(object field, int size)
{
var value = field.ToString();
if (value.Length == size)
return value;
return value.Length > size
? value.Substring(0,size)
: value.PadRight(size, ' ');
}
Depending on where you display it you will see some inconsistencies, like its not the same "view width" the characters "iii" and "MMM" even that they have the same length.
I am trying to create a text box which accepts only alphabets and numbers, and discards all the special characters. I need this for entering a valid file name, without extension.
I am using the following code:
private void txtFName_KeyDown(object sender, KeyEventArgs e)
{
if (!(((e.Key >= Key.A) && (e.Key <= Key.Z)) || ((e.Key >= Key.D0) && (e.Key <= Key.D9)) || (e.Key == Key.Shift) || (e.Key == Key.Back)))
{
e.Handled = true;
MessageBox.Show("No Special Characters are Allowed!");
}
}
Unfortunately the "KeyPress" event is not there, so I figured out that this is the best way to achieve what I am doing.
The problem I am facing is:
As you can see in the code above, I have taken care of the "Shift" key press, but when I press the "Shift" key on the SIP, the "No Special Characters are Allowed" Message box pops up 3 times before I can key in an upper case alphabet!!! So this essentially prevents me from entering any Upper case characters.
Worse still, it is accepting all the characters !##$%^&*(). Probably because it is detecting these as numbers from 0-9. It looks that the key codes are being returned the same for 2 and #, 3 and # and so on. This is very strange behavior! And I can not even use the underscore key with the above technique.
Such behavior is obviously not acceptable in a professional App.
How can I create a text box which accepts only alphabets, numbers and underscore, and discards all other characters?
Also, is there a problem with the "Shift" key not getting detected?
If I understand your query, you want to validate your text box.
I would use regular expressions to do this:
using System.Text.RegularExpressions;
// Add chars which you don't want the user to be able to enter
private Regex regularExpression = new Regex(#"!##$%^&*().", RegexOptions.IgnoreCase);
// Your text changed event handler
private void txtInput_TextChanged(object sender, TextChangedEventArgs e)
{
// Replace the forbidden char with ""
txtInput.Text = regularExpression.Replace(txtInput.Text, "");
txtInput.SelectionStart = txtInput.Text.Length;
}
Hopefully this will work for you.
In textbox autocomplete properties when i entered a character like 'm' it dropped all strings start with 'm' or 'M' but when i write a character 'أ' (this is an Arabic character) it dropped only string start with 'أ' , I want when i typed 'أ' or 'ا' or 'إ' or 'آ' it drop all strings start with any character from those not the typed character only , in C# windows application I'm not using ASP.net
Any suggestions????
I am quite surprised to see that neither the text box control nor the auto complete string collection has a way to specify what defines string equality for the auto-complete mechanism. The only way I can think of to get the behavior that you want would be to create your own auto-complete mechanism.
I have done this several times in the past, before the functionality was supplied by the framework, and it is not difficult.
Just use an editable ComboBox instead of a TextBox, and handle the TextChanged event to create the autocompletions.
Here is some untried pseudocode of the procedure:
bool textChangedProgramatically = false;
List<string> myStrings; // The list of items that can appear in the auto-complete.
private static myComboBox_TextChanged(object sender, EventArgs args)
{
if (textChangedProgramatically)
return;
string searchText = myComboBox.Text;
// Use appropriate culturally-sensative string StartsWith comparisons
List<string> matchingItems = GetMatchingStrings(searchText, myStrings);
string firstMatch;
if (matchingItems.Length > 0)
firstMatch = matchingItems[0];
else
firstMatch = string.Empty;
myComboBox.Items.Clear;
myComboBox.Items.AddRange(matchingItems);
string fulltext = searchText;
if (firstMatch.Length > fullText.Length)
{
fullText = fullText + firstMatch.Substring(fullText.Length);
textChangeProgramatically = true;
myComboBox.Text = fullText;
myComboBox.SelectionStart = searchText.Length;
myComboBox.SelectionLength = fullText.Length - searchText.Length;
textChangeProgramatically = false;
}
}
The trick is to get the right matching behavior in GetMatchingStrings. You may want to use string compatibility normalization to get the Arabic characters into non-presentation forms before doing the comparison, but I expect the right SubString overload might handle all of these cases for you.
I'm faced with a bit of an issue. The scenario is that I have a multi-line text box and I want to put all that text into one single string, without any new lines in it. This is what I have at the moment:
string[] values = tbxValueList.Text.Split('\n');
foreach (string value in values)
{
if (value != "" && value != " " && value != null && value != "|")
{
valueList += value;
}
}
The problem is that no matter what I try and what I do, there is always a new line (at least I think?) in my string, so instead of getting:
"valuevaluevalue"
I get:
"value
value
value".
I've even tried to replace with string.Replace and regex.Replace, but alas to no avail. Please advise.
Yours sincerely,
Kevin van Zanten
The new line needs to be "\r\n". Better still - use Environment.NewLine.
The code is inefficient though, you are creating numerous unnecessary strings and an unnecessary array. Simply use:
tbxValueList.Text.Replace(Environment.NewLine, String.Empty);
On another note, if you ever see yourself using the += operator on a string more than a couple of times then you should probably be using a StringBuilder. This is because strings are Immutable.
Note that new lines can be up to two characters depending on the platform.
You should replace both CR/carriage-return (ASCII 13) and LF/linefeed (ASCII 10).
I wouldn't rely on localized data as David suggests (unless that was your intention); what if you're getting the text string from a different environment, such as from a DB which came from a Windows client?
I'd use:
tbxValueList.Text.Replace((Char)13,"").Replace((Char)10,"");
That replaces all occurrences of both characters independent of order.
Try this
tbxValueList.Text.Replace(System.Environment.NewLine, "");
try this one as well
string[] values = tbxValueList.Text.Replace("\r\n", " ").Split(' ');