How i can hide error provider after displaying error? - c#

I have a question, maybe someone help me. Do you know made in this program how to do after the error has been removed icon "error provider" disappear if no more error, but it is appear? (excuse me for grammatical error)
bool IsValidated = true; //will be checked on button click
void dateTimePicker1_Validating(object sender, CancelEventArgs e)
{ DateTimePicker datetimepicker = sender as DateTimePicker;
if (datetimepicker.Value == null)
{ errorProvider1.SetError(datetimepicker, "Required");
IsValidated = false;
} }
void comboBox_Validating(object sender, CancelEventArgs e)
{
ComboBox combo = sender as ComboBox;
if(combo.SelectedIndex == -1)
{
errorProvider1.SetError(combo, "Required");
IsValidated = false;
}}
void textBox_Validating(object sender, CancelEventArgs e)
{
TextBox txtbox = sender as TextBox;
if (txtbox.Text == "" || txtbox.Text.Length > 2)
{
errorProvider1.SetError(txtbox, "Required");
IsValidated = false;
}}

https://msdn.microsoft.com/en-us/library/system.windows.forms.errorprovider.seterror%28v=vs.110%29.aspx
Call SetError with null to clear the error.
if (datetimepicker.Value == null)
{
errorProvider1.SetError(datetimepicker, "Required");
IsValidated = false;
}
else
{
errorProvider1.SetError(datetimepicker, null);
}

Related

Problem with checkbox Ischecked in a click event

I have a problem with checkbox. I'm trying to explain.
My Datagrid can have multiple row, so multiple checkbox.
When one or many checkbox is true, so a button IsEnable=true. But if no checkbox is true, so the button is disable.
Maybe an issue with a count of checkbox IsChecked ? Or maybe the click processus is not appropriate?
Here is my code :
private void Chk_UID_IsSelected_Click(object sender, RoutedEventArgs e)
{
var Chkbox = sender as CheckBox;
if (Chkbox.IsChecked == true)
{
UID_Disconnect.IsEnabled = true;
}
else
{
UID_Disconnect.IsEnabled = false;
}
}
But my code check only one checkbox in fact. If i click on 2 checkbox and uncheck one, my button have an incorrect state.
My design :
Another try with no result:
private void Chk_UID_IsSelected_Click(object sender, RoutedEventArgs e)
{
foreach (CheckBox c in dgConnected_Users.ItemsSource)
{
var Chkbox = sender as CheckBox;
UID_Disconnect.IsEnabled = Chkbox.IsChecked == true;
}
}
Here another try with no result:
private void Chk_UID_IsSelected_Click(object sender, RoutedEventArgs e)
{
int checkedBoxes = dgConnected_Users.Items
.OfType<CheckBox>().Count(c => (bool)c.IsChecked == true);
if (checkedBoxes > 0)
{
// You shall pass!
UID_Disconnect.IsEnabled = true;
}
else
{
// None shall pass
UID_Disconnect.IsEnabled = false;
}
}
OK I have progress in my code, here the new one :
private void Chk_UID_IsSelected_Click(object sender, RoutedEventArgs e)
{
foreach (DataRowView dr in dgConnected_Users.ItemsSource)
{
var Chkbox = sender as CheckBox;
if (Chkbox.IsChecked == true)
{
isAnyChecked++;
}
else
{
isAnyChecked--;
}
}
if (isAnyChecked > 0)
{
UID_Disconnect.IsEnabled = true;
}
else
{
UID_Disconnect.IsEnabled = false;
}
}
That do the staff, but i know that if I check just one checkbox, 'isAnyChecked' = 2, or it must be equal 1.

Calendar in the header of a DataGridView in any column

Is it possible to add calendar picker in the header of the DataGridView in any of the column ?
Something like this:
I managed such an effect similar to Excel Filter in this way.
Obviously you have to build your calendar control.
Using WPF DataGrid is all easier.
If you want this affect you can try to implement this event in DataGridView:
private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DataGridView dg = (DataGridView)sender;
BindingSource bs = (BindingSource)dg.DataSource;
//TODO;
}
else if (e.Button == MouseButtons.Right)
{
FrmMyCalendar f = new FrmMyCalendar();
f.Location = new Point(Cursor.Position.X, Cursor.Position.Y);
//TODO
}
}
You can think of introducing grids WPF slowly through ElementHost
Yes... It's possible
DateTimePicker dtPicker;
private void Form_Load(object sender, EventArgs e)
{
dtPicker = new DateTimePicker();
dtPicker.Format = DateTimePickerFormat.Short;
dtPicker.Visible = false;
dtPicker.Width = 100;
dgT.Controls.Add(dtPicker);
dtPicker.ValueChanged += this.dtPicker_ValueChanged;
dgT.CellBeginEdit += this.dgT_CellBeginEdit;
dgT.CellEndEdit += this.dgT_CellEndEdit;
}
private void dgT_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
try
{
if ((dgT.Focused) && (dgT.CurrentCell.ColumnIndex == 2)) // Here you can define on which column DateTimePicker will be activated
{
dtPicker.Location = dgT.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Location;
dtPicker.Visible = true;
if (dgT.CurrentCell.Value != DBNull.Value)
{
dtPicker.Value = (DateTime)dgT.CurrentCell.Value;
}
else
{
dtPicker.Value = DateTime.Today;
}
}
else
{
dtPicker.Visible = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void dgT_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
if ((dgT.Focused) && (dgT.CurrentCell.ColumnIndex == 2))
{
dgT.CurrentCell.Value = dtPicker.Value.Date;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void dtPicker_ValueChanged(object sender, EventArgs e)
{
dgT.CurrentCell.Value = dtPicker.Text;
}
private void dgT_Scroll(object sender, ScrollEventArgs e)
{
dtPicker.Visible = false;
}
Just put DataGridView on the form a. In my case I named it dgT.

Check if user type a single character or number in textbox c#

I already can make the login for user, so if user type their username or password wrongly in textbox, the message label that says "Invalid username or password" appear. But, i want to when user type a single character or number in textbox when the message label is appear, the message label will not visible to user (visible = false) as user already type a single character or number in textbox. But the message label didn't disappear when user type a single character or number.
This is the code:
private void CheckTextBox(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
{
label5.Visible = true;
}
else
{
label5.Visible = false;
}
}
And here is the image:
Below image is when user type wrongly (the username or password), the message label appear:
Below image is when user type a single character or number, but the message label still at there
My question is: How do i set the message label to not show when user type a single character or number in the textbox?
Any help?
Your answer will be great appreciated!
Thank you!
Problem : You have not wiredup the CheckTextBox() method for both TextBox1 and TextBox2 TextChanged Event.
Solution : in your Form_Load WireUp the CheckTextBox() method for the Textbox1 and TextBox2 TextChanged Event as below:
private void Form1_Load(object sender, EventArgs e)
{
textBox1.TextChanged += new System.EventHandler(this.CheckTextBox);
textBox2.TextChanged += new System.EventHandler(this.CheckTextBox);
}
Suggestion : i think string.IsNullOrWhiteSpace() is more appropriate as it would also check for Whitespace in addition to null and Empty strings.
Try This:
private void CheckTextBox(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(textBox1.Text) || string.IsNullOrWhiteSpace(textBox2.Text))
{
label5.Visible = true;
}
else
{
label5.Visible = false;
}
}
This line is checking if either textbox has information in it.
if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
Change the || to && and then the label will only be shown when BOTH textboxes do not have any data.
If I understand you correctly, try this:
private void CheckTextBox(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
{
label5.Visible = true;
}
else
{
label5.Visible = false;
}
}
If you change || to && then label5 will be visible only if both textboxes are empty.
if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
Try this..use && instead of ||
private void CheckTextBox(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
{
label5.Visible = true;
}
else
{
label5.Visible = false;
}
}
Check this code, Should be call this OnTextChanged="CheckTextBox" on your textbox
protected void CheckTextBox(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
{
label5.Visible = true;
}
else
{
label5.Visible = false;
}
}
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="CheckTextBox"></asp:TextBox>

How to add TextBox in DataGridView at specific column?

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox txtbox = e.Control as TextBox;
if (this.dataGridView1.CurrentCell.ColumnIndex == 0)
{
if (txtbox != null)
{
//
}
}
}
Also I have coded with AutoCompleteStringCollection.
Code is working,
Before edit Column 1, It won't allow autoCompletion for any of other column.
Once edited Column 1, all column will work same as Column one.
Please help me how to fix issue or else any other best way to do that, please share here.
This should work.
private bool firstColEdited = false;
/************************************************************/
var source = new AutoCompleteStringCollection();
String[] stringArray = Array.ConvertAll<DataRow, String>(products.Select(), delegate(DataRow row) { return (String)row["code"]; });
source.AddRange(stringArray);
/************************************************************/
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox txtbox = e.Control as TextBox;
if (this.dataGridView1.CurrentCell.ColumnIndex == 0 || firstColEdited)
{
firstColEdited = true;
txtbox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtbox.AutoCompleteCustomSource = source;
txtbox.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}

cannot see the label content when a item is selected from dynamically added dropdownlist

I have a Dropdownlist (DDL1) when I select any item from this dropdownlist(DDL1), results in creation of another dropdownlist(DDL2), This contains some of the items.When I select other Item from DDL1 , Items will change in DDL2, this happens for the each different item selected in DDL1.
when I select a item from DDL2, label content must be shown, intially I'm making Label invisibe and in the code I changed the visibility to true and added content to it. But the label content is not shown when I select a item from DDL2.
Here is my Code
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "Abe Books")
{
DropDownSeller.Visible = true;
lnkUsdBooks.Visible = true;
lnkUsdBooks.Text = "usedbooks#abe.com";
lnkUsdBooks.NavigateUrl = "mailto:usedbook#abe.com";
DropDownSeller.Visible = true;
DropDownSeller.Items.Remove("Chacha Choudary");
DropDownSeller.Items.Remove("SpiderMan");
DropDownSeller.Items.Remove("Amar chitra Katha");
DropDownSeller.Items.Remove("Chandamama");
DropDownSeller.Items.Remove("Mahabharata");
DropDownSeller.Items.Add("Amar chitra Katha");
DropDownSeller.Items.Add("Chandamama");
DropDownSeller.Items.Add("Mahabharata");
DropDownSeller.DataBind();
if (DropDownSeller.SelectedValue == "Amar chitra Katha")
{
lblPrice.Visible = true;
lblPrice.Text = "$69.99";
}
else if (DropDownSeller.SelectedValue == "Chandamama")
{
lblPrice.Visible = true;
lblPrice.Text = "$59.99";
}
else if (DropDownSeller.SelectedValue == "Mahabharata")
{
lblPrice.Visible = true;
lblPrice.Text = "$49.99";
}
else
{
lblPrice.Visible = false;
}
}
Any ideas on this are appreciated
Thanks,
Remove if (!Page.IsPostBack) from the DropDownList1_SelectedIndexChanged because when the page postbacks this condition will be false. Because your page is posting back to the server that's why it is not visible and not showing.
In short your DropDownList1_SelectedIndexChanged should be like..
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "Abe Books")
{
DropDownSeller.Visible = true;
lnkUsdBooks.Visible = true;
lnkUsdBooks.Text = "usedbooks#abe.com";
lnkUsdBooks.NavigateUrl = "mailto:usedbook#abe.com";
DropDownSeller.Visible = true;
DropDownSeller.Items.Clear(); // it will clear all the items, instead you are removing one by one
DropDownSeller.Items.Add("Amar chitra Katha");
DropDownSeller.Items.Add("Chandamama");
DropDownSeller.Items.Add("Mahabharata");
DropDownSeller.DataBind();
}
protected void DropDownSeller_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownSeller.SelectedValue == "Amar chitra Katha")
{
lblPrice.Visible = true;
lblPrice.Text = "$69.99";
}
else if (DropDownSeller.SelectedValue == "Chandamama")
{
lblPrice.Visible = true;
lblPrice.Text = "$59.99";
}
else if (DropDownSeller.SelectedValue == "Mahabharata")
{
lblPrice.Visible = true;
lblPrice.Text = "$49.99";
}
else
{
lblPrice.Visible = false;
}
}

Categories

Resources