A Developer Journey who codes for fun

Daily Dose Of Code

  • Home
  • Dot.Net Basics
    • .Net Basics
      • CTS
      • CLS
      • CLR
      • Strong Vs Weak Ref
      • .Net Framework
      • What is Manifest
    • Memory Management
      • Garbage Collection 1
      • Garbage Collection 2
      • Circular Reference
  • C Sharp
    • Abstract Class in C#
    • Interfaces in C#
    • Value type by Val and By Ref
    • Var keyword
    • Null Coalescing Operator
    • Buit-in code snippets
  • OOPS
    • Abstraction and Encapsulation
    • Polymorphism
    • Inheritence
    • Aggregation
  • Threading
    • Delegates
      • Calling Delegate using Invoke, BeginInvoke
      • Multicast Delegate
      • Exception Handling in Multicast Delegate
      • Action
      • Predicate
      • Func
    • Synchronization
    • Thread Pool
    • Exception Handling
    • TPL
  • Design Pattern
    • Creational Patterns
      • Singleton Pattern
      • Factory Pattern
      • Abstract Factory Pattern
      • Prototype Pattern
      • Builder Pattern
    • Structural Patterns
      • Adapter Pattern
      • Bridge Pattern
      • Composite Pattern
      • Proxy Pattern
      • Facade Pattern
      • Decorator Pattern
      • Flyweight Pattern
    • Behavioral Patterns
      • Command Pattern
      • Interpreter Pattern
      • Iterator Pattern
      • Mediator Pattern
      • Memento Pattern
      • Observer Pattern
      • State Pattern
      • Strategy Pattern
      • Visitor Pattern
      • Chain Of Responsibility Pattern
      • Template Pattern
  • Data Structures
    • Generic List in C#
    • 2d array to 1d array
    • 3d arrayto 1d array
    • Linked List
      • Singly Linked List in C#
    • Queue
      • Dummy Data 1
    • Stack
      • Dummy Data 2
    • Tree
      • Dummy Data 3
    • Graph
      • Dummy Data 4
  • WCF
    • WCF Service using VS 2015
  • Scripts
    • Chrome Extensions
      • Create a Chrome Extension
      • Facebook autologout script
      • Gmail autologout script

How to Send data from a User Control to another user control on a single form using Events and Delegates:--

 Unknown     2:40 AM     No comments   

This is an example how to create Dynamic Check Boxes and how to communicate betwwen two user controls on a sigle form using Events and Delegates.
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)
{

}

}
}
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • How to handle generic errors in WinApp using C# ?Pass your exception or error through catch block to this method this will catch your error and show the messagebox regarding that error.Method which t… Read More
  • How to combine two images into one image in C#?using System.Drawing;public static System.Drawing.Bitmap Combine(string[] files){ Create a list for images and read images List images = new List();… Read More
  • Differences between Connected and disconnected architecture ?Hii friends,Today one of my frend ask about what approach is better connected or disconnected architecture ..So let me explain more about this problem… Read More
  • What is interface and why we implement interfaces ?Interface defined by using interface keyword .In visualstudio you can directly add class as a interface its not a class it behaves as a template of th… Read More
  • What do you meant by Containment in C#?Containment is the replacement of inheritence,no no if inheritance isn’t the right choice,then the answer is containment, also known as aggregation. R… Read More
Newer Post Older Post Home

0 comments:

Post a Comment

About The Author

Unknown
View my complete profile

Total Pageviews

84732

Popular Posts

  • Clr - Common Language Runtime
    .Net framework provides a run time environment - CLR. Common language runtime takes the IL code from the compiler( language specific) and p...
  • Auto logout chrome extension for Gmail
    Hello Friends, In the last article we learned to create a sample chrome extension. Here we are going to create auto logout Gmail script as...
  • Predicate delegate in C#
    Hello Everyone, In the article we will talk about Predicate delegate. Predicate is also a delegate which encapsulate a method that takes...
  • .Net Framework overview
    Hello friends : Here i am writing my first article on .Net framework anyways....So the question is What is .Net Framework ? The .Net fram...
  • Nagarro Placement Papers..
    Ques.1 :- Seat Reservation prog for the theatre. Write a function for seat allocation for the movie tickets. Total no of seats available are...
  • Calling the Delegates using Invoke(), BeginInvoke() and DynamicInvoke() ?
    Hello Guys, So in the last article we talked about What is delegate and how can we create a delegate. In this article we will discuss w...
  • What does it mean by disconnected data access architecture of ADO.Net?
    ADO.Net introduces the concept of disconnected data architecture. In traditional data access components, you make a connection to the databa...
  • C code to Check the string has valid identifier or not in.
    #include #include #include char keyword[][10]={"auto","break","case","char","const","...
  • Garbage Collection - Automatic Memory Management Part II
    Welcome friends in the second article of Garbage Collection. Those who have missed the first one can visit here . So in this article i will...
  • Delegates in C Sharp
    A Delegate is a type variable that holds the reference to a method. Delegates are similar to Pointer to functions in C and C++ When we...

Blog Archive

  • ►  2016 (4)
    • ►  September (2)
      • ►  Sep 03 (2)
    • ►  August (1)
      • ►  Aug 28 (1)
    • ►  April (1)
      • ►  Apr 24 (1)
  • ►  2015 (12)
    • ►  September (10)
      • ►  Sep 30 (1)
      • ►  Sep 29 (1)
      • ►  Sep 28 (1)
      • ►  Sep 27 (2)
      • ►  Sep 26 (3)
      • ►  Sep 20 (1)
      • ►  Sep 19 (1)
    • ►  August (1)
      • ►  Aug 16 (1)
    • ►  March (1)
      • ►  Mar 31 (1)
  • ►  2013 (10)
    • ►  June (1)
      • ►  Jun 16 (1)
    • ►  April (1)
      • ►  Apr 21 (1)
    • ►  February (8)
      • ►  Feb 18 (3)
      • ►  Feb 17 (2)
      • ►  Feb 16 (2)
      • ►  Feb 15 (1)
  • ►  2012 (1)
    • ►  May (1)
      • ►  May 27 (1)
  • ►  2010 (22)
    • ►  October (14)
      • ►  Oct 21 (1)
      • ►  Oct 06 (12)
      • ►  Oct 04 (1)
    • ►  April (2)
      • ►  Apr 22 (1)
      • ►  Apr 16 (1)
    • ►  March (1)
      • ►  Mar 30 (1)
    • ►  January (5)
      • ►  Jan 08 (3)
      • ►  Jan 01 (2)
  • ▼  2009 (110)
    • ►  December (8)
      • ►  Dec 18 (2)
      • ►  Dec 05 (1)
      • ►  Dec 04 (5)
    • ►  November (1)
      • ►  Nov 27 (1)
    • ►  October (14)
      • ►  Oct 09 (4)
      • ►  Oct 07 (1)
      • ►  Oct 06 (3)
      • ►  Oct 05 (3)
      • ►  Oct 01 (3)
    • ►  September (17)
      • ►  Sep 30 (1)
      • ►  Sep 29 (1)
      • ►  Sep 28 (1)
      • ►  Sep 25 (1)
      • ►  Sep 24 (1)
      • ►  Sep 17 (2)
      • ►  Sep 15 (3)
      • ►  Sep 11 (2)
      • ►  Sep 09 (3)
      • ►  Sep 08 (2)
    • ►  August (31)
      • ►  Aug 31 (1)
      • ►  Aug 27 (3)
      • ►  Aug 26 (1)
      • ►  Aug 25 (2)
      • ►  Aug 24 (1)
      • ►  Aug 22 (2)
      • ►  Aug 21 (3)
      • ►  Aug 20 (2)
      • ►  Aug 19 (3)
      • ►  Aug 18 (1)
      • ►  Aug 16 (1)
      • ►  Aug 12 (2)
      • ►  Aug 11 (1)
      • ►  Aug 10 (3)
      • ►  Aug 07 (4)
      • ►  Aug 06 (1)
    • ►  July (24)
      • ►  Jul 25 (4)
      • ►  Jul 24 (20)
    • ▼  April (15)
      • ▼  Apr 10 (3)
        • Window Controls in ASP.Net
        • An Example to create an UserControl to create a ch...
        • How to Send data from a User Control to another us...
      • ►  Apr 07 (9)
      • ►  Apr 06 (3)

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments
copyright @ TechGiant 2015. Powered by Blogger.

Disclaimer

This is my personal blog and i write articles on .Net, WPF, C#, OOPS, Threading and other .Net technologies. This is not related to any of my employer and organizations. This is the result of my personal interest.

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Followers

Copyright © 2025 A Developer Journey who codes for fun | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com