messagebox always pop up when clicking combo box - c#

Good day, anyone can help me with this problem... I have a combo box and a textbox. the textbox(txtFruitNo) will check the length of text under Leave event. It is ok. But if I click on the combo box while txtFruitNo is not yet completed. It needs me to complete first the length of txtFruitNo then only I can click the combo box.
I do not want to show the messagebox if I click on the combo box even if the length of the txtFruitNo is not yet completed.
Thanks
private void cmbFruitSelection_SelectedIndexChanged(object sender, EventArgs e)
{
DateTime thetime = DateTime.Now;
String varApple = "App-Red";
String varBanana = "Ban-Yellow";
if (cmbFruitSelection.SelectedItem.ToString() == "Apple")
{
txtFruitNo.Text = varApple.ToString() + thetime.ToString("yyyy");
txtFruitNo.SelectionStart = txtFruitNo.Text.Length;
txtFruitNo.MaxLength = 18;
}
else if (cmbFruitSelection.SelectedItem.ToString() == "Banana")
{
txtFruitNo.Text = varBanana.ToString() + thetime.ToString("yyyy");
txtFruitNo.SelectionStart = txtFruitNo.Text.Length;
txtFruitNo.MaxLength = 17;
}
}
private void txtFruitNo_Leave(object sender, EventArgs e)
{
if (txtFruitNo.TextLength != txtFruitNo.MaxLength)
{
MessageBox.Show("Your fruit number is too short. Please check.");
txtFruitNo.Focus();
}
else
{
// Do something here
}
}

At what point is it important for continuation of the program that the "Fruit Number" is within parameters. If it is not at the time of leaving focus try moving it to a different control for example the "OK" button could run the parameter check and if valid continue if not flag mesage box and return to the textbox

Since your requirement is to only to do the validation and prompt the message box once the user has selected a value from the combo, please do the following;
Introduce a form variable
private bool isComboClicked = false;
Add the below line to cmbFruitSelection_SelectedIndexChanged
isComboClicked = true;
Adding the above line at the beginning of the above event would prompt the length validation message on selection of value from the combo. If you want to prompt message for specific value on the combo move it within the if statements if (comboBox1.SelectedItem.ToString() == "Apple") etc.
Now in txtFruitNo_Leave event enclose the code within the below if condition.
if (isComboClicked)
{
// Your Code
if (txtFruitNo.TextLength != txtFruitNo.MaxLength)
{
MessageBox.Show("Your fruit number is too short. Please check.");
txtFruitNo.Focus();
}
else
{
// Do something here
}
}

As I understand:
You have "validation" on TextBox in Leave eventhandler, which show error message if validation fails.
But if TextBox.Leave event was raised by selecting ComboBox control, then validation must be suppressed.
Create Panel and put there only txtFruitNo and cmbFruitSelection controls.
// Validation function
private bool IsTextBoxValid()
{
return this.txtFruitNo.Length == this.txtFruitNo.maxlength;
}
Then create and hook up Validating eventhandler for Panel where you will validate txtFruitNo
private void Panel_Validating(Object sender, CancelEventArgs e)
{
if(this.IsTextBoxValid() == false)
{
e.Cancel = true;
MessageBox.Show("Your fruit number is too short. Please check.") ;
}
}
Validating will be raised only when focus move outside of the panel.
Using Validating event will prevent changing focus to outside controls automatically if e.Cancel = true
In that case combobox cmbFruitSelection can be focused and user can complete txtFruitNo text by selecting valid value from ComboBox.
I think using of ErrorProvider control will be more friendly for the user, then MessageBox.
Add ErrorProvider control in the Form through designer and add few lines in the code
private void Panel_Validating(Object sender, CancelEventArgs e)
{
if(this.IsTextBoxValid() == false)
{
e.Cancel = true;
this.ErrorProvider1.SetError(txtFruitNo,
"Your fruit number is too short. Please check.");
}
else
{
this.ErrorProvider1.Clear();
}
}
And clear error after valid value was used from ComboBox
private void cmbFruitSelection_SelectedIndexChanged(object sender, EventArgs e)
{
DateTime thetime = DateTime.Now;
String varApple = "App-Red";
String varBanana = "Ban-Yellow";
if (cmbFruitSelection.SelectedItem.ToString() == "Apple")
{
txtFruitNo.Text = varApple.ToString() + thetime.ToString("yyyy");
txtFruitNo.SelectionStart = txtFruitNo.Text.Length;
txtFruitNo.MaxLength = 18;
//Clear error
this.ErrorProvider1.Clear();
}
else if (cmbFruitSelection.SelectedItem.ToString() == "Banana")
{
txtFruitNo.Text = varBanana.ToString() + thetime.ToString("yyyy");
txtFruitNo.SelectionStart = txtFruitNo.Text.Length;
txtFruitNo.MaxLength = 17;
//Clear error
this.ErrorProvider1.Clear();
}
}

Related

Disable button when text is entered in text box correctly and adding dialog box in c#

I have been trying to figure out on disabling the text box when text is entered in the text box. I am able to do this but I have also got another problem which is, lets say you have a text box with some word i.e "Welcome". If I edit that and add more letter on to that i.e "WelcomeSSS" adding SSS then text is enabled. But when I delete "SSS" from that text box, button is still enabled and not DISABLED as the text is the same as it was before editing.
How do I make sure that the text is disabled in this situation?
And also I want to add dialog box when a user click on different button to go to different page without saving the edited content. How do i do this?
Here is my code so far:
private void textbox1_IsChanged(object sender, KeyEventArgs e)
{
//SaveButton.IsEnabled = !string.IsNullOrEmpty(TextBox1.Text);
if (TextBox1.Text.Trim().Length > 0)
{
SaveButton.IsEnabled = true;
}
if (WpfHelpers.Confirmation(resources.QuitWithoutSaving, resources.Changes))
{
}
}
This is using KeyUp event handler in wpf.
If I understood your question correctly...
private void textbox1_IsChanged(object sender, KeyEventArgs e)
{
if (textbox1.Text == "Welcome"){
SaveButton.IsEnabled = false;
}
else{
SaveButton.IsEnabled = true;
}
}
You need a data structure for storing the saved values. E.g. a List of strings. In the following snippet, these values are stored in the SavedTextBoxTexts list.
At first, the SaveButton gets disabled (you can do this in the XAML as well). When SaveButton has been clicked, the textBox1.text value will be stored in the list and the button gets disabled.
When textBox1.text is edited and SaveButton exists (already), the different conditions get checked.
If textBox1.text is already stored in SavedTextBoxTexts or textBox1.text is empty or contains only whitespace characters, SaveButton gets disabled. Otherwise the SaveButton will be enabled.
public partial class MainWindow : Window
{
private List<string> SavedTextBoxTexts = new List<string>();
public MainWindow()
{
InitializeComponent();
// Disable button right from the beginning.
SaveButton.IsEnabled = false;
}
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
// The function may be called, while the window has not been created completely.
// So we have to check, if the button can already be referenced.
if (SaveButton != null)
{
// Check if textBox1 is empty or
// textBox1.text is already in the list of saved strings.
if (String.IsNullOrEmpty(textBox1.Text) ||
textBox1.Text.Trim().Length == 0 ||
SavedTextBoxTexts.IndexOf(textBox1.Text.Trim()) >= 0)
{
// Disable Button
SaveButton.IsEnabled = false;
}
else
{
// If textBox1.text has not been saved already
// or is an empty string or a string of whitespaces,
// enable the SaveButton (again).
SaveButton.IsEnabled = true;
}
}
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
// Store the text in textBox1 into the SavedTextBoxTexts list.
SavedTextBoxTexts.Add(textBox1.Text.Trim());
// Disable the SaveButton.
SaveButton.IsEnabled = false;
}
// This is executed, when the other button has been clicked.
// The text in textBox1 will not be saved.
private void AnotherButton_Click(object sender, RoutedEventArgs e)
{
if (WpfHelpers.Confirmation(resources.QuitWithoutSaving, resources.Changes))
{
// Move to other page ...
}
}
}

TextBox Validation in Visual Studio C#

I am fairly new to Visual Studio and C# in general but basically I need to check that the contents of the textbox are valid before proceeding to add the contents to a list with a button.
I am using the following objects:
A TexBox to enter the value
A Validating event linked to the TextBox to validate the data.
A Button to take action
A Click event associated to the button.
The problem is that I cannot check if the values in the box are valid or not and prevent the click event in the button to happen. In other words if the contents are not valid then do not take action.
This is my code.
public partial class mainForm : Form
{
public mainForm()
{
InitializeComponent();
}
private void addButton_Click(object sender, EventArgs e)
{
// I need to check if the content is valid before adding it to the form
ListViewItem item = new ListViewItem(this.nameTextBox.Text);
this.listView1.Items.Add(item);
}
private void nameTextBox_Validating(object sender, CancelEventArgs e)
{
int maxCharacters = 15;
String err = "";
String contents = this.nameTextBox.Text;
if (contents.Length == 0)
{
err = "I am sorry but the name cannot be empty";
e.Cancel = true;
}
else if (!contents.Replace(" ", "").Equals(contents, StringComparison.OrdinalIgnoreCase))
{
err = "I am sorry but the name cannot contain spaces";
e.Cancel = true;
}
else if (contents.Length > 15)
{
err = "I am sorry, but the name cannot have more than " + maxCharacters + " characters";
e.Cancel = true;
}
this.mainFormErrorProvider.SetError(this.nameTextBox, err);
}
}
You are confused about when the "name" text boxes' validation method is called.
See here
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Selector SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order...
So clicking the button has nothing to do with the validation of the text box.
What you need to do is put the validation logic in a separate method, and then call it from both events.
Also, since you're new to C# here are some pointers.
Namespaces, Classes, methods, and properties are supposed to be Pascal Case.
Instead of using a long winded work around like this
!contents.Replace(" ", "").Equals(nameText, StringComparison.OrdinalIgnoreCase)
You could simply use
contents.Contains(" ")
There are tons of useful methods just like that, so in the future you should do more research on what you need before implementing something yourself, especially if it seems like a commonly used technique.
Also, you want to avoid if/else's as much as possible in favour of returning early.
Here's what your class might look with better practice in mind
const int NAME_MAX_CHARACTERS = 15;
public mainForm()
{
InitializeComponent();
}
private void addButton_Click(object sender, EventArgs e)
{
if(!Validate())
{
return;
}
// I need to check if the content is valid before adding it to the form
ListViewItem item = new ListViewItem(this.nameTextBox.Text);
this.listView1.Items.Add(item);
}
private void nameTextBox_Validating(object sender, CancelEventArgs e)
{
e.Cancel = !Validate();
}
private bool Validate()
{
string nameText = nameTextBox.Text;
if(String.IsNullOrEmpty(nameText))
{
this.mainFormErrorProvider.SetError(this.nameTextBox, "I am sorry but the name cannot be empty");
return false;
}
if(nameText.Contains(" "))
{
this.mainFormErrorProvider.SetError(this.nameTextBox, "I am sorry but the name cannot contain spaces");
return false;
}
if (nameText.Length > 15)
{
this.mainFormErrorProvider.SetError(this.nameTextBox, "I am sorry, but the name cannot have more than " + NAME_MAX_CHARACTERS + " characters");
return false;
}
return true;
}

DataGridView - cell validation - prevent CurrentRow/Cell change

I have WinForms DataGridView with source set to SortableBindingList. In this form, there's column Comment and I need to prevent user from inserting some characters, thus validation.
What I want to do is, whenever user enters invalid value, system will notify him (OnNotification( 'You entered wrong comment');) and force him/her to stay in edit mode.
So far I build solution like this:
void MyDataGridView_CellEndEdit( object sender, DataGridViewCellEventArgs e )
{
if (e.ColumnIndex == ColumnComment.Index) {
object data = Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
if( (data != null) && (!CommentIsValid( data.ToString()))){
CurrentCell = Rows[e.RowIndex].Cells[e.ColumnIndex];
BeginEdit( true );
// My notification method
OnNotification( String.Format( "Comment `{0}` contains invalid characters) );
return;
}
}
}
I have following issues with this:
OnCellValidating is triggered only when whole form is closing or when current row is changed, not after I finish editing of single cell, so I've put check into CellEndEdit.
When I used Enter/Esc to end editing, it works as expected and desired.
When I use mouse and click to another row, cell stays in edit mode, but another row gets selected.
When I try to use Enter (displays notification on invalid comment) and then Esc (to cancel edit) it uses value pushed by Enter (because edit mode has finished).
So my questions are:
How can I fire CellValidating after each cell edit, not when form is closing
How can I prevent CurrentRow and CurrentCell change even after mouse click?
How can I force cell to stay in edit mode?
When I use mouse and click to another row, cell stays in edit mode, but another row gets selected.
Here I would use a global Boolean, bool isInvalidState say and a global DataGridViewCell = invalidCell object. In the default state you can set isInvalidState = false and invalidCell = null. Then using
private bool OnNotification(string cellValue)
{
// Check for error.
if (error)
return false;
}
Then in the above method
void MyDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == ColumnComment.Index) {
object data = Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
if((data != null) && (!CommentIsValid(data.ToString()))){
CurrentCell = Rows[e.RowIndex].Cells[e.ColumnIndex];
BeginEdit(true);
// My notification method
isInvalidState = OnNotification(
String.Format("Comment `{0}` contains invalid characters));
if (isInvalidState)
invalidCell = MyDataGridView[e.RowIndex, e.ColumnIndex];
return;
}
}
}
Now, wire-up an event CellContentClick on your DataGridView and check if isInvalidState == true
private void MyDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (isInvlaidState)
{
isInvalidState = false;
MyDataGridView.CurrentCell = invalidCell;
invalidCell = null;
return;
}
// Do other stuff here.
}
When I try to use Enter (displays notification on invalid comment) and then Esc (to cancel edit) it uses value pushed by Enter (because edit mode has finished).
I am not sure about this problem; it is likely you will have to handle the KeyDown event and capture the escape key - handling it differently.
I hope this helps.
Try something like this. It shall work.
private void datagridview1_dataGridview_CellValidating
(object sender, DataGridViewCellValidatingEventArgs e)
{
if (datagridview1_dataGridview.Rows[e.RowIndex].Cells[2].Value.Equals(""))
{
MessageBox.Show("Product name should not be empty", "Error");
datagridview1_dataGridview.CurrentCell = datagridview1_dataGridview.Rows[e.RowIndex].Cells[2];
datagridview1_dataGridview.CurrentCell.Selected = true;
}
}
Unfortunately, MoonKnight's solution didn't work fully for me as the code in CellContentClick event handler never set the control back to the cell that was being validated for its value, when it had an invalid value. Nevertheless, considering his valuable hint of using global variables isInvalidState and invalidCell helped me constructing the following solution that works exactly as asked in the OP.
Using the combination of CellValidating and CellValidated in the right way solves the problem as follows:
Do your data validation inside the CellValidating event handler. Set the isInvalidState flag and the cellWithInvalidUserInput variable (NOTE: I renamed the invalidCell to cellWithInvalidUserInput):
private void MyDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
var cellUnderConsideration = MyDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (!ValidateCurrentCellValue(cellUnderConsideration))
{
OnNotification( String.Format( "Comment `{0}` contains invalid characters) );
//Or MessageBox.Show("your custom message");
isInvalidState = true;
cellWithInvalidUserInput = cellUnderConsideration;
e.Cancel = true;
}
}
The data validation function:
bool isInvalidState;
DataGridViewCell cellWithInvalidUserInput;
private bool ValidateCurrentCellValue(DataGridViewCell cellToBeValidated)
{
//return 'true' if valid, 'false' otherwise
}
Perform desired actions on UI controls inside the CellValidated event handler:
private void MyDataGridView_CellValidated(object sender, DataGridViewCellEventArgs e)
{
if (isInvalidState)
{
isInvalidState = false;
if (cellWithInvalidUserInput != null && cellWithInvalidUserInput.RowIndex > -1)
{
MyDataGridView.CurrentCell = cellWithInvalidUserInput;
MyDataGridView.CurrentCell.Selected = true;
MyDataGridView.BeginEdit(true);
}
cellWithInvalidUserInput = null;
}
}

How To Check if button is hovered, and output message

I have 40 buttons in a application that I need custom hovers that will show in a status field. I have made a function for adding a certain message and one to remove, so upon a hover, it calls the function, and same with leaving the button.
I want 40 different messages and one way of doing that is to check which button is being hovered over by the mouse.
if(button1.hovered == true){
string message = "scenario1";
}
elseif(button2.hovered == true){
scenario2...etc
}
Is there a way to check if hovered? and check it in a if statement?
ive decided to add more info so it might be easier to get my point.
add message to listview when mouse hoover.
void messAdd(object sender, EventArgs e)
{
string now = DateTime.Now.ToString("M/d/yyyy") + " - " + DateTime.Now.ToString("HH:mm:ss tt");
string message = "message 1";
found = false;
ListViewItem item = new ListViewItem(message);
foreach (ListViewItem z in listView1.Items)
{
if (z.Text == message)
{ found = true; }
}
if (found == false)
{
item.SubItems.Add(now.ToString());
listView1.Items.Add(item);
listView1.EnsureVisible(item.Index);
}
else
{
DeleteIfNecessary(message);
}
}
delete message from listbox when mouse leave:
void messdel(object sender, EventArgs e)
{
string message = "message 1";
found = false;
ListViewItem item = new ListViewItem(message);
foreach (ListViewItem z in listView1.Items)
{
if (z.Text == message)
{ found = true; }
}
if (found == true)
{
DeleteIfNecessary(message);
}
}
I can make 4 of these functions for each buttons, but since i need 40 different messages, stupid yes, but there is no way to send a message argument through the function, so i need to use the if test and check witch button is hovered and then output the message to that specified button. and im using visual studio and windows forms, sorry for not mentioned.
There is a Control.MouseHover event. You can implement:
private void button_MouseHover(object sender, System.EventArgs e)
{
doSomething(sender);
}
and for all of your buttons, set event handler for MouseHover to button_MouseHover in IDE, or do it in code:
this.button1.MouseHover += new System.EventHandler(this.button_MouseHover);
By checking the sender parameter you can know which button is calling the event handler.
Update
According to your update in the question, I think you can just use messAdd as the event handler for MouseEnter event for all of your buttons, and use messdel as the event handler for MouseLeave. Again, you don't need to create a copy of these two methods for all of your buttons, you just need to assign the same event handler method for all the buttons, and check sender to know who is calling the event handler - then creating different messages.
The sender is your Button object. Just cast it to a Button and access what you want (text, tag, name, etc.) to know which Button is trying to add/remove message on the list view.
Update 2
Button button = sender as Button;
if (button == null) {
// not a button, do nothing
return;
}
string message = String.Empty;
if (sender.Equals(button1)) {
message = "message1";
} else if (sender.Equals(button2)) {
message = "message2";
} ...
I'm going to assume this is WinForms, since you didn't specify otherwise.
You can create an int hoveredId that represents which button is hovered (value -1 means nothing is hovered). When creating the buttons, set the Tag property to the button's id number.
Then register each button to both of these functions:
private void buttons_MouseEnter(object sender, System.EventArgs e)
{
Button btn = ((Button)sender);
hoveredId = (int)btn.Tag;
}
private void buttons_MouseLeave(object sender, System.EventArgs e)
{
Button btn = ((Button)sender);
if ((int)btn.Tag == hoveredId)
hoveredId = -1;
}
When checking which button is hovered, you can use a switch statement:
switch (hoveredId)
{
case 1:
string message = "scenario1";
break;
case 2:
scenario2...etc
break;
}

How to prevent/cancel a combobox's value change in c#?

I have a combobox at the top of a form that loads editable data into fields below. If the user has made changes, but not saved, and tries to select a different option from the combobox, I want to warn them and give them a chance to cancel or save.
I am in need of a "BeforeValueChange" event with a cancelable event argument.
Any advice on how to accomplish?
Save the ComboBox's SelectedIndex when to box if first entered, and then restore it's value when you need to cancel the change.
cbx_Example.Enter += cbx_Example_Enter;
cbx_Example.SelectionChangeCommitted += cbx_Example_SelectionChangeCommitted;
...
private int prevExampleIndex = 0;
private void cbx_Example_Enter(object sender, EventArgs e)
{
prevExampleIndex = cbx_Example.SelectedIndex;
}
private void cbx_Example_SelectionChangeCommitted(object sender, EventArgs e)
{
// some custom flag to determine Edit mode
if (mode == FormModes.EDIT)
{
cbx_Example.SelectedIndex = prevExampleIndex;
}
}
Here is the simplest fix:-
bool isSelectionHandled = true;
void CmbBx_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (isSelectionHandled)
{
MessageBoxResult result = MessageBox.Show("Do you wish to continue selection change?", this.Title, MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.No)
{
ComboBox combo = (ComboBox)sender;
isSelectionHandled = false;
if (e.RemovedItems.Count > 0)
combo.SelectedItem = e.RemovedItems[0];
return;
}
}
isSelectionHandled = true;
}
Save the current value on the Enter event.
Implement the BeforeValueChange logic in the ValueChanged event, before the actual ValueChanged logic. If the user cancels, set the stored value and don't continue in the method (return).
If you're going to use this system a lot, I'd suggest inheriting ComboBox and implementing your BeforeValuechange event there.
The Validating event can be used for this scenario
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx
You don't get an appropriate event by default. You could cache the previous value and set it back to that if the user wants to cancel.
How about using the Validating / Validated events?
It works well, if the event happening on LostFocus instead of Change is ok with you.
Otherwise, how about
public void Combobox_ValueChanged(object sender, EventArgs e) {
if (!AskUserIfHeIsSureHeWantsToChangeTheValue())
{
// Set previous value
return;
}
// perform rest of onChange code
}
You could use a message filter to intercept clicks and key presses, which would allow you to prevent the combo box's normal behaviour. But I think you'd be better off disabling the combo box when the user makes a change, and require them to either save or revert their changes.
You can't really prevent it, but you can change it back to the old value if certain requirements aren't met:
private SomeObject = selectedSomeObject=null;
private void cbxTemplates_SelectionChangeCommitted(object sender, EventArgs e)
{
if (!(sender is ComboBox cb)) return;
if (!(cb.SelectedItem is SomeObject tem)) return;
if (MessageBox.Show("You sure?", "??.",
MessageBoxButtons.OKCancel) != DialogResult.OK)
cb.SelectedItem = selectedSomeObject;
else
{
selectedSomeObject = tem;
}
}

Categories

Resources