c968-wgu-project/C968Project/Helpers.cs

75 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C968Project
{
static class Helpers
{
public static void SetTextBoxValidationState(TextBox textBox, bool isValid)
{
if (isValid)
{
textBox.BackColor = Color.White;
}
else
{
textBox.BackColor = Color.Red;
}
}
public static bool ConfirmDelete()
{
var result = MessageBox.Show("Are you sure you want to delete this item?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
return true;
}
return false;
}
public static void SearchDataGridView(DataGridView data, string searchValue)
{
// Search the datagridview items for text and then select the first occurence
data.CurrentCell = null;
string search = searchValue;
if (searchValue == string.Empty)
{
MessageBox.Show("Cant search an empty value", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (data.Rows.Count == 0)
{
MessageBox.Show("Nothing to search for", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
foreach (DataGridViewRow row in data.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.FormattedValue != null && cell.FormattedValue.ToString().ToLower().Contains(search.ToLower(), StringComparison.OrdinalIgnoreCase))
{
data.CurrentCell = cell;
return;
}
}
}
MessageBox.Show("No matching search results", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public static void ShowErrorMessage(string message, string title = "", MessageBoxButtons buttons = MessageBoxButtons.OK)
{
MessageBox.Show(message, title, buttons, MessageBoxIcon.Error);
}
}
}