Hi everyone,
In my application I have a main form that is split in two areas. One on the left that is the TabControl and one on the right that is a TreeViewControl.
I set up a new user control in my project solution such a way that I can instanciate it at runtime to create TabPage instances for my TabControl at runtime. Does that make sense? It works well for now, but I came to a point where I need to trigger an event when I click on a submit button located on one of these TabPages and it would do something in my TreeViewControl...
I tried to implement that but apparently my TabPage code context does not know the TreeViewControl (I kinda understand why since at design time, that code does not care what it'll be instanciated for, right?)...
So am I on the right tracks?... maybe not, object oriented is far for me... please advise :)
Loading
MSLPosted Apr 26, 2008, 5:22 AM
this refers to the current object of the form NameIt.
correct me if i am wrong.
EmiliePosted Apr 26, 2008, 3:04 AM
Now, can you explain more the code in the containing form? I have problem figuring out what you mean by this.nameIt1
Thanks,
Emilie
Scott LyslePosted Apr 23, 2008, 10:44 PM
Well, there are actually a few different ways you can do this; you could make your treeview public and pass it to your user control and let the user control act directly on it. Or, you could raise an event in response to something done on the user control and then respond to it in the container to update the treeview.
For example, it you have a user control that has a textbox and a button on it, and each time the button is clicked, you want to get the text from the textbox and add a new node to your form's treeview, you could do something like this:
In the user control create an event and delegate and add another class to handle the event arguments (in this case there is only a node name but it could be more):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace UserControlEvents
{
public partial class NameIt : UserControl
{
// add a delegate
public delegate void NameItHandler(object sender, NameItArgs e);
// add an event of the delegate type
public event NameItHandler NameUpdated;
// ctor
public NameIt()
{
InitializeComponent();
}
// handle the button click event to raise
// the NameUpdated event and pass it the new
// node name captured from its textbox.
private void button1_Click(object sender, EventArgs e)
{
NameUpdated(this, new NameItArgs(textBox1.Text));
}
}
///
/// A class used to contain values set from this
/// control - there is only one argument here but
/// it can contain more.
///
public class NameItArgs : System.EventArgs
{
private string mNewNodeName;
// class constructor
public NameItArgs(string sNodeName)
{
mNewNodeName = sNodeName;
}
public string NodeName
{
get
{
return mNewNodeName;
}
}
}
}
The, on the containing form, if you have a split panel with a treeview on one side and this user control on the other, this code will add a new treenode to the treeview whenever the event is fired from the user control:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace UserControlEvents
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void nameIt1_Load(object sender, EventArgs e)
{
this.nameIt1.NameUpdated +=new NameIt.NameItHandler(nameIt1_NameUpdated);
}
// handles the event from the user control
private void nameIt1_NameUpdated(object sender, NameItArgs e)
{
TreeNode tn = new TreeNode(e.NodeName);
this.treeView1.Nodes.Add(tn);
}
}
}