|
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace RKiss.Tools.ServiceBusTester.Dialogs
{
public partial class ParametersDialog : Form
{
public Dictionary<string, object> Parameters { get; set;}
public ParametersDialog(Dictionary<string, object> parameters)
{
InitializeComponent();
this.Parameters = parameters;
if (this.Parameters == null)
this.Parameters = new Dictionary<string, object>();
this.ColumnType.Items.AddRange("Boolean", "Int64", "String", "DateTime", "TimeSpan", "Guid");
foreach(var param in this.Parameters)
{
string typename = "String";
if(param.Value != null)
{
if(this.ColumnType.Items.Contains(param.Value.GetType().Name))
typename = param.Value.GetType().Name;
else if(this.ColumnType.Items.Contains(param.Value.GetType().FullName))
typename = param.Value.GetType().FullName;
}
this.dataGridViewParameters.Rows.Add(param.Key, typename, param.Value);
var row = this.dataGridViewParameters.Rows[this.dataGridViewParameters.Rows.Count - 1];
row.Cells[this.ColumnValue.Name].ValueType = param.Value.GetType();
}
if (this.Parameters.Count() > 0)
{
this.Height = this.Size.Height + Math.Min(this.Parameters.Count(), 5) * this.dataGridViewParameters.Rows[0].Height;
this.MinimumSize = new Size(this.Size.Width, this.Size.Height);
}
this.dataGridViewParameters.DefaultCellStyle.SelectionBackColor = Color.SkyBlue;
this.dataGridViewParameters.ShowRowErrors = true;
this.ColumnType.DisplayStyleForCurrentCellOnly = true;
this.buttonOK.Enabled = false;
}
private void buttonOK_Click(object sender, EventArgs e)
{
this.Parameters.Clear();
foreach (DataGridViewRow row in dataGridViewParameters.Rows)
{
if (row.IsNewRow == false && row.Cells[this.ColumnName.Name].Value != null)
{
this.Parameters.Add((row.Cells[this.ColumnName.Name].Value as string).Trim(), row.Cells[this.ColumnValue.Name].Value);
}
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
}
private bool ClearErrorText(int index)
{
bool bErrorExists = false;
foreach (DataGridViewRow row in this.dataGridViewParameters.Rows)
{
if(row.Cells[index].Value != null && (row.Cells[index].Value as string).IndexOf(' ') < 0 && string.IsNullOrEmpty(row.Cells[index].ErrorText) == false)
{
string name = (row.Cells[index].Value as string).Trim().ToUpper();
var errors = this.dataGridViewParameters.Rows.Cast<DataGridViewRow>().Where(r => r.Cells[index].Value != null && (r.Cells[index].Value as string).Trim().ToUpper() == name);
if (errors.Count() == 1)
row.Cells[index].ErrorText = string.Empty;
else
bErrorExists = true;
}
}
return bErrorExists;
}
private void dataGridViewParameters_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
this.ClearErrorText(this.ColumnName.Index);
string name = this.dataGridViewParameters.Rows[e.RowIndex].Cells[this.ColumnName.Index].Value as string;
string typename = this.dataGridViewParameters.Rows[e.RowIndex].Cells[this.ColumnType.Index].Value as string;
if (e.RowIndex >= 0 && e.ColumnIndex == this.ColumnName.Index)
{
if (string.IsNullOrEmpty(name) == false && string.IsNullOrWhiteSpace(name) == false)
{
this.dataGridViewParameters.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = name.Trim();
name = this.dataGridViewParameters.Rows[e.RowIndex].Cells[e.ColumnIndex].Value as string;
if (name.IndexOf(' ') > 0)
{
this.dataGridViewParameters.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Wrong name";
this.buttonOK.Enabled = false;
}
else
{
var row = this.dataGridViewParameters.Rows.Cast<DataGridViewRow>().Where(r => r.Cells[this.ColumnName.Name].Value != null && (r.Cells[this.ColumnName.Name].Value as string).Trim().ToUpper() == name.ToUpper());
this.dataGridViewParameters.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = row.Count() == 1 ? string.Empty : "The name must be unique";
this.buttonOK.Enabled = row.Count() == 1;
}
}
else
{
this.dataGridViewParameters.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Empty name";
this.buttonOK.Enabled = false;
}
}
else if (string.IsNullOrEmpty(name) || string.IsNullOrWhiteSpace(name))
{
this.dataGridViewParameters.Rows[e.RowIndex].Cells[this.ColumnName.Index].ErrorText = "Empty name";
this.buttonOK.Enabled = false;
}
if (string.IsNullOrEmpty(typename))
this.dataGridViewParameters.Rows[e.RowIndex].Cells[this.ColumnType.Index].Value = "String";
}
private void dataGridViewParameters_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
this.buttonOK.Enabled = false;
}
private void dataGridViewParameters_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is ComboBox)
{
ComboBox combo = e.Control as ComboBox;
combo.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
combo.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
}
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
string newType = ((DataGridViewComboBoxEditingControl)sender).SelectedItem as string;
//change ValueType
var row = this.dataGridViewParameters.Rows[this.dataGridViewParameters.SelectedCells[0].RowIndex];
var cellValue = row.Cells[this.ColumnValue.Name];
cellValue.ValueType = Type.GetType(newType.IndexOf('.') > 0 ? newType : "System." + newType);
cellValue.Value = this.DefaultValue(cellValue.ValueType);
this.dataGridViewParameters.Refresh();
}
public object DefaultValue(Type type)
{
if (type == typeof(string))
return string.Empty;
if(type == typeof(DateTime))
return DateTime.Now;
if(type == typeof(System.Xml.Linq.XElement))
return "<root/>";
return Activator.CreateInstance(type);
}
private void dataGridViewParameters_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
this.buttonOK.Enabled = !ClearErrorText(this.ColumnName.Index);
}
private void dataGridViewParameters_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
this.buttonOK.Enabled = !ClearErrorText(this.ColumnName.Index);
}
private void dataGridViewParameters_RowLeave(object sender, DataGridViewCellEventArgs e)
{
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.