In this application I have to send some data from control2 to control1 on a single button click.So for this i have used event and Delegates.
Have A look.
//Control2:----
namespace ParserF
{
public partial class Control2 : UserControl
{
public ArrayList CheckBoxName = new ArrayList();
public char[] seperator = new char[5];
static int count = 0, indexing=0;
public delegate void PassSelectedValues(ArrayList CheckBoxName, int countDefault);
public event PassSelectedValues DataSelected;
public int Flag = 0;
public Control2()
{
InitializeComponent();
}
# region Properties
///
/// These are the properties value contains the text string seperated by the seperator
///
private string _Value;
public string Value
{
get
{
return _Value;
}
set
{
_Value = value;
}
}
///
/// Seperator is also a string by which we will parse the text string.
///
private string _Seperator;
public string Seperator
{
get
{
return _Seperator;
}
set
{
_Seperator = value;
}
}
///
/// This is default value means which checkbox will be checked by default.
///
private string _Default;
public string Default
{
get
{
return _Default;
}
set
{
_Default = value;
}
}
# endregion
# region private
///
/// This function is just when we will click on Ok button then all the data will be sent to control1
/// by delegate public delegate void PassSelectedValues(ArrayList CheckBoxName, int ind);
/// and event public event PassSelectedValues DataSelected; In this function i m taking a string Builder
/// Temporary String in that we will store the string after parsing through seperator and then store it in a arraylist.
///
///
///
///
private void OkButton_Click(object sender, EventArgs e)
{
StringBuilder TemporaryString = new StringBuilder();
InitializaValues();
//This if codition will check that value (Text String) contains default or not .If yes the continue otherwise will make a messagebox.
if (Value.Contains(Default))
{
for (int index = 0; index < Seperator.Length; index++)
{
seperator[index] = Seperator[index];
}
//This for loop is just to extract strings upto the length of the value
for (; (indexing < Value.Length); indexing++)
{
if (Value[indexing] != seperator[0])
{
TemporaryString.Append(Value[indexing]);
}
else
{
if (Flag == 0)
{
count++;
}
String str = TemporaryString.ToString();
//This if condition is just to comapare default one and store the index.
if (!System.Convert.ToBoolean((str.CompareTo(Default))))
{
Flag = count;
count = 0;
}
//This will add the string to the array list
CheckBoxName.Add(str);
//This will replace the string Builder Temporary String to Empty One.
TemporaryString.Replace(str, String.Empty);
}
}
if ((indexing == Value.Length)&&(Flag==0))
{
Flag = (count+1);
}
String str1 = TemporaryString.ToString();
CheckBoxName.Add(str1);
//DataSelected is the event which will fire by this we are sending the arraylist and the index for defaultCheck
DataSelected(CheckBoxName, Flag-1);
}
else
{
MessageBox.Show("No focus set due to No string matches from TextString");
}
}
///
/// This is just to Inotialize values to the text boxes.
///
private void InitializaValues()
{
Value = ValueTextBox.Text;
Seperator = SeperatorTextBox.Text;
Default = DefaultTextBox.Text;
}
# endregion
}
}
//Control1:---
namespace ParserF
{
public partial class Control1 : UserControl
{
public Control1()
{
InitializeComponent();
}
public int flag1;
ArrayList Array1 = new ArrayList();
static int index2 = 0;
public ArrayList SelectedCheckBox
{
set
{
Array1 = value;
}
}
public int indexing
{
set
{
flag1 = value;
}
}
public void AddCheckBox()
{
for (int index = 0; index < Array1.Count; index++)
{
CheckBox checkBox = new CheckBox();
checkBox.AutoSize = true;
checkBox.Location = new Point((28), (27 + index2));
checkBox.Size = new System.Drawing.Size(80, 17);
checkBox.Text = Array1[index].ToString();
if (index == flag1)
{
checkBox.Checked = true;
}
if (checkBox.Checked)
{
textBox1.Text = checkBox.Text;
}
checkBox.UseVisualStyleBackColor = true;
index2 += 20;
Controls.Add(checkBox);
checkBox.CheckedChanged += new EventHandler(checkBox_CheckedChanged);
}
}
///
/// This is an event which will fire when a user will check the check Box
///
///
///
private void checkBox_CheckedChanged(object sender, System.EventArgs e)
{
if (((CheckBox)sender).Checked == true)
{
if (textBox1.Text == string.Empty)
{
textBox1.Text = ((CheckBox)sender).Text;
}
else
{
textBox1.AppendText(",");
textBox1.AppendText(((CheckBox)sender).Text);
}
}
else if (textBox1.Text == ((CheckBox)sender).Text)
{
textBox1.Text = string.Empty;
}
else
{
int FirstIndex=textBox1.Text.IndexOf(((CheckBox)sender).Text);
int length=((CheckBox)sender).Text.Length;
string str1=textBox1.Text.Remove(FirstIndex, length);
textBox1.Text = str1;
}
}
private void Control1_Load(object sender, EventArgs e)
{
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
//Form1.cs
namespace ParserF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
control21.DataSelected += new Control2.PassSelectedValues(passValuesHandlerMethod);
}
protected void passValuesHandlerMethod(ArrayList CheckBoxName, int flag2)
{
control11.SelectedCheckBox = CheckBoxName;
control11.flag1 = flag2;
control11.AddCheckBox();
}
private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
{
}
private void control21_Load(object sender, EventArgs e)
{
}
private void control11_Load_1(object sender, EventArgs e)
{
}
}
}
0 comments:
Post a Comment