String.ToUpper does not capitalize string when passing to DevExpress TextEdit control - c#

Can someone explain me why first code block doesn't work while second one does. In first example string is not capizalized when control lose focus.
Don't work (not capitalized):
private void nameTextEdit_Leave(object sender, EventArgs e)
{
if(Properties.Settings.Default.capitalizeCustomer != false)
{
string userEnteredString = nameTextEdit.EditValue.ToString();
string capitalizedString = userEnteredString.ToUpper();
nameTextEdit.EditValue = capitalizedString;
}
}
Work's OK (when control lost focus text is capitalized):
private void nameTextEdit_Leave(object sender, EventArgs e)
{
if(Properties.Settings.Default.capitalizeCustomer != false)
{
string userEnteredString = nameTextEdit.EditValue.ToString();
nameTextEdit.EditValue = userEnteredString.ToUpper();
}
}

This is strange, both examples should have the same effect.
My best guess is you accidentally did
nameTextEdit.EditValue = userEnteredString;
Instead of:
nameTextEdit.EditValue = capitalizedString;

Why don't you simply set CharacterCasing property to Upper and prevent lower case from the start?

Related

SuppressKeyPress Property of KeyEventArgs not accessible

After spending 90 minutes searching for a solution to this simple problem I have to post a question in shame.
I'm working on a WPF project where the user inputs text. I want to check the inputs while the user is typing, display a tool tip and ideally block characters that are not allowed. Basically it's this thread:
How do I validate characters a user types into a WinForms textbox? or this
Is there a best practice way to validate user input?
private void NameTextbox_KeyDown(object sender, KeyEventArgs e)
{
e.???
}
I created this code behind by double clicking in the KeyDown-Property Field in the designer (just mentioning this if I messed up there).
Screenshot of the Property Window
I can not access the e.SupressKeyPress Property. Why?
As of the Properties offered by VS I think that e is of the wrong Type or in the wrong context here.
Intellisense Screenshot
Edit1
private void NameTextbox_KeyDown(object sender, KeyEventArgs e)
{
var strKey = new KeyConverter().ConvertToString(e.Key);
if (!strKey.All(Char.IsLetter))
{
MessageBox.Show("Wrong input");
e.Handled = true;
}
}
Thanks to #rokkerboci I was able to build something that kind of works.
Yet I think it is overly complex. So improvements are still welcome :)
New Error When Creating a Message Box the application hangs without an exception thrown.
You are using WPF, which does not include the WindowsForms specific SupressKeyPress property.
You can do this in WPF by using the KeyDown event, and setting the KeyEventArgs.Handled property to true (it tells the handler, that it doesn't have to do anything with this event.)
private void NameTextbox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
MessageBox.Show("delete pressed");
e.Handled = true;
}
}
EDIT:
I have found a perfect answer to your question:
C#:
char[] invalid = new char[] { 'a', 'b' };
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
foreach (var item in invalid)
{
if (e.Text.Contains(item))
{
e.Handled = true;
return;
}
}
}
private void TextBox_Pasting(object sender, DataObjectPastingEventArgs e)
{
var text = e.DataObject.GetData(typeof(string)).ToString();
foreach (var item in invalid)
{
if (text.Contains(item))
{
e.CancelCommand();
return;
}
}
}
XAML:
<TextBox PreviewTextInput="TextBox_PreviewTextInput" DataObject.Pasting="TextBox_Pasting" />

bunifu material textbox mode password is not working

I want to create a login page based C# desktop application. I use the bunifu toolbox to create a login page design. But when I want to create a password field using the bunifumaterialtextbox, the textbox does not show any changes / it only displays alphabet. It looks like the ispassword contained in the textbox properties is not working. So what should I do so that this texbox can display the correct password (not displaying alphabeth) when the program is run ?. I apologize for any errors in this question.
is just a easy solution i have found
private void passbox_OnValueChanged(object sender, EventArgs e)
{
passbox.isPassword = true;
}
I'm new using the Bunifu framework tool and I had the same problem then you. The solution I found was to invoke the _TextBox method which I suppose that gives you all the normal TextBox controls.
My code was something like this:
txtPassword._TextBox.PasswordChar = '*';
I loaded this code inside the Form_Load code block. It worked for me, hope that be useful for you too. Good luck!
in side the properties tab of the bunifu text box there is a property ispassword set it to true.
to fully make the bunifu textbox to passwordbox you have to do the following:
1.in side the properties tab of the bunifu text box there is a property ispassword set it to False.
2.create an event of Enter and use the following code:
3.create an event of Leave and use the following code:
private void txtpassword_Enter(object sender, EventArgs e)
{
if (txtpassword.Text == "Password")
{
txtpassword.Text = "";
txtpassword.isPassword = true;
}
}
private void txtpassword_Leave(object sender, EventArgs e)
{
if (txtpassword.Text == "")
{
txtpassword.Text = "Password";
txtpassword.isPassword = false;
}
}
like this
You can use the UseSystemPasswordChar property in your text box.
Example:
TextBox1.UseSystemPasswordChar = true;
this problem's solution is using enter,leave and textchange event such as below code if the name of your text box is txtPassword:
private void txtPassword_TextChange(object sender, System.EventArgs e)
{
if (txtPassword.Text.Trim() != "")
{
txtPassword.PasswordChar = '*';
}
else
{
txtPassword.PasswordChar = '\0';
}
}
private void txtPassword_Leave(object sender, System.EventArgs e)
{
if (txtPassword.Text.Trim() == "")
{
txtPassword.PasswordChar = '\0';
txtPassword.TextPlaceholder = "insert your placeholder..";
}
}
private void txtPassword_Enter(object sender, System.EventArgs e)
{
if (txtUserName.Text.Trim() != "")
{
txtPassword.PasswordChar = '*';
txtPassword.PlaceholderText = "";
}
}

check dropdown menu if matches

Long story short: There are specific tags given (like Pop, Rock, Metal) and the User should write into a textbox and every time he adds a char the given tags are checked if one (or more) matches. At the moment I'm using a combobox with the following code:
private void EnterComboBox_TextChanged(object sender, EventArgs e)
{
List<string> AllTags = new List<string>();
AllTags.Add("Pop");
if (AlleTags[0].ToLower().StartsWith(EnterComboBox.Text.ToLower()))
{
EnterComboBox.Items.Clear();
EnterComboBox.Items.Add("Pop");
EnterComboBox.DroppedDown = true;
}
}
this is working fine but the problem is, that after the first char entered the dropbox drops down and the entered text is marked and will be overwritten when a new char is entered. Any ideas how I could fix this? Every idea is welcome it doesn't have to be a combo box :)!
Edit:
After some more (detailed) research I realized I could explain it like this: Basically I want the combobox the behave like the search-bar from google. The users enters letters and in the dropdown menu are autocomplete suggestions
At the moment I solved it like this:
I placed a textbox in front of a combobox so that only the arrow of the combobx is visible and if you click on it you automatically write in the textbox.
public Form1()
{
InitializeComponent();
EingabeTextBox.AutoSize = false;
EingabeTextBox.Size = new Size(243, 21); //the size of the combobox is 260;21
}
private void EingabeTextBox_TextChanged(object sender, EventArgs e)
{
EingabeComboBox.Items.Clear();
List<string> AlleTags = new List<string>();
AlleTags.Add("Example");
if (AlleTags[0].ToLower().StartsWith(EingabeTextBox.Text.ToLower()))
{
EingabeComboBox.Items.Add(AlleTags[0]);
EingabeComboBox.DroppedDown = true;
}
}
For me it would work like this. I hope I can help someone else with this too, but I am still open for any better ideas :)!
Changing the ComboBox entries while typing into it obviously creates undesired interferences. Instead combine a TextBox and a ListBox.
private bool changing;
private void TextBox_TextChanged(object sender, EventArgs e)
{
if (!changing) {
changing = true;
try {
// manipulate entries in the ListBox
} finally {
changing = false;
}
}
}
private void ListBox_IndexChanged(object sender, EventArgs e)
{
if (!changing) {
changing = true;
try {
// Put selected entry into TextBox
} finally {
changing = false;
}
}
}
The changing guard makes sure that the ListBox does not influence the TextBox while you are entering text into the TextBox and vice versa.
The try-finally ensures that the guard will be reset in any circumstances, even if an exception should occur.

contents of combo box shown in list box

I was wondering, if I click a selection on a ComboBox for instance letter A, and I want its content to show in label or ListBox, how can I do that? I tried experimenting with some codes below. This codes below are not working for me. any other way or suggestion?
private void selectContents_SelectedIndexChanged(object sender, System.EventArgs e)
{
string var;
var = selectContents.Text;
if (var == "A")
{
Label1.Text = "hi";
listBox1.Text = "hi";
}
}
ok problem solve i just need to change the var :D
you can't have string var;
var is a keyword in c# MSDN C# keywords
I have no clue how that code was compiling, I suppose it wasn't
edit
string a = "var"; //this is ok
string var = "a"; //this is not
I believe this is what you're looking for.
private void selectContents_SelectedIndexChanged(object sender, System.EventArgs e)
{
listBox1.Items.Add(selectContents.SelectedItem);
Label1.Text = selectContents.SelectedItem;
}

How can I transfer text from one textbox to another textbox without it looping and not sending the text?

I have searched for what I am asking of you but I may have been wording it wrong, so I hope what I have typed is easy to understand.
I am creating a Css formatter with two textboxes using WinForms
I would like it to take a css long format code in the first textBoxLongFormat then appear in the second, textboxShortFormat, as css short code.
I have a bit of code for both text boxes and a separate class of other code for the behind the scenes to change the format.
The class works but I’m having a problem with textBoxLongFormat and I’m guessing it will happen the other way round, the code is looping on its self and not closing, so it’s not sending the format to textBoxShortFormat so nothing happens.
There is something I am doing wrong, I know, but I cannot see it. What is it that I am doing wrong? It will be great to have your help.
Here is the code for the textboxes if it helps.
What is it that i need to add or to make it work?
private void textBoxLongFormat_TextChanged(object sender, EventArgs e)
{
CssFormatConverter cssLongFormatConverter = new CssFormatConverter();
string longFormatCss = textBoxLongFormat.Text;
string shortFormatCss = cssLongFormatConverter.ToShortFormat(longFormatCss);
textBoxShortFormat.Text = shortFormatCss;
}
private void textBoxShortFormat_TextChanged(object sender, EventArgs e)
{
CssFormatConverter cssShortFormatConverter = new CssFormatConverter();
string shortFormatCss = textBoxShortFormat.Text;
string longFormatCss = cssShortFormatConverter.ToLongFormat(shortFormatCss);
textBoxLongFormat.Text = longFormatCss;
}
Thank you in advance
Add a boolean check that indicates the other textbox is updating.
bool isUpdating = false;
private void textBoxLongFormat_TextChanged(object sender, EventArgs e)
{
if (!isUpdating)
{
isUpdating = true;
CssFormatConverter cssLongFormatConverter = new CssFormatConverter();
string longFormatCss = textBoxLongFormat.Text;
string shortFormatCss = cssLongFormatConverter.ToShortFormat(longFormatCss);
textBoxShortFormat.Text = shortFormatCss;
isUpdating = false;
}
}
private void textBoxShortFormat_TextChanged(object sender, EventArgs e)
{
if (!isUpdating)
{
isUpdating = true;
CssFormatConverter cssShortFormatConverter = new CssFormatConverter();
string shortFormatCss = textBoxShortFormat.Text;
string longFormatCss = cssShortFormatConverter.ToLongFormat(shortFormatCss);
textBoxLongFormat.Text = longFormatCss;
isUpdating = false;
}
}
Before updating a TextBox, unsubscribe it from the TextChanged event. Then update. Then re-subscribe.
In the first one, that would be:
textBoxShortFormat.TextChanged -= textBoxShortFormat_TextChanged;
textBoxShortFormat.Text = shortFormatCss;
textBoxShortFormat.TextChanged += textBoxShortFormat_TextChanged;

Categories