using System;
using System.Windows.Forms;
using System.Drawing;
namespace FormFun
{
class MyWindow : Form
{
public MyWindow() : base()
{
//Form
this.Text = "My First Windows Application";
this.Size = new Size(500, 500);
this.Load += new EventHandler(MyWindow_Load);
//Label
Label lblGreeting = new Label();
lblGreeting.Text = "Hello WinForm";
lblGreeting.Location = new Point(20, 20);
//Button
Button btnExit = new Button();
btnExit.Text = "Exit";
btnExit.Location = new Point(400, 430);
btnExit.Size = new Size(80, 30);
btnExit.Click += new EventHandler(btnExit_Click);
//treeVire
TreeView treeView1 = new TreeView();
treeView1.Location = new Point(50, 50);
treeView1.Size = new Size(300, 300);
//Add Controls to form
this.Controls.AddRange(new Control[] { lblGreeting, btnExit, treeView1 });
}
public void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
//ALL REFERENCES TO treeView1 BELOW FAIL
private void MyWindow_Load(object sender, EventArgs e)
{
treeView1.Nodes.Clear();
treeView1.Nodes.Add("Programming Languages");
TreeNode node = new TreeNode("Object Oriented Programming Languages");
node.Nodes.Add("C++");
TreeNode subnode = node.Nodes.Add("Framework and runtime based languages");
subnode.Nodes.Add("Java");
subnode.Nodes.Add("C#");
treeView1.Nodes[0].Nodes.Add(node);
}
}
class Test
{
static void Main()
{
Application.Run(new MyWindow());
}
}
}
Need help with a stupid event handler mistakeNewb to C# needs some advice
Page 1 of 1
6 Replies - 729 Views - Last Post: 25 October 2010 - 08:47 AM
#1
Need help with a stupid event handler mistake
Posted 25 October 2010 - 07:55 AM
Whenever I try to reference treeView1 outside of the form's constructor method, it is telling me that it does not exist in the current context. I am assuming this is because I need to pass the reference off or hook onto it differently in the event handler, but I'm not sure how to do it. Can anyone point me in the right direction. I realize building forms from scratch the way I am is inefficient, but I am just trying to learn the ins and outs of it all. Thanks!
Replies To: Need help with a stupid event handler mistake
#2
Re: Need help with a stupid event handler mistake
Posted 25 October 2010 - 08:08 AM
That's because treeView1 was created in the scope of the constructor. It's not accessible by that name outside of the constructor.
Fortunately, every EventHandler provides a handy parameter called sender. It's an object, but it can be cast.
In this case, sender will be a TreeView. So you could do something like this:
And then test for null. If treeView isn't null, that means the cast was successful. You could also do an if(sender is TreeView) beforehand.
By the way, this is handy when you have several controls using the same event handler and you need to distinguish which one triggered the event.
Edit:
This is not accurate. I misread the code and thought that the event was of the TreeView, not the Form. Guess we know what happens when I assume.
Or you could just make it global to the form by declaring it outside the constructor.
In this case, sender will be a TreeView. So you could do something like this:
TreeView treeView = sender as TreeView;
And then test for null. If treeView isn't null, that means the cast was successful. You could also do an if(sender is TreeView) beforehand.
By the way, this is handy when you have several controls using the same event handler and you need to distinguish which one triggered the event.
Edit:
This is not accurate. I misread the code and thought that the event was of the TreeView, not the Form. Guess we know what happens when I assume.
Or you could just make it global to the form by declaring it outside the constructor.
This post has been edited by insertAlias: 25 October 2010 - 08:42 AM
#3
Re: Need help with a stupid event handler mistake
Posted 25 October 2010 - 08:09 AM
Hello there,
Well, for one thing, you define the Treee View inside the MyWindow method/contructor. Therefore, it is only accessible from that method. The object reference will go out of scope as soon as the MyWindow method finishes executing. To get around this, you could put the treeView1 reference variable as an instance variable at the top of the class, outside of any method. You can then instantiate the Tree View object from inside the contructor. For example;
Well, for one thing, you define the Treee View inside the MyWindow method/contructor. Therefore, it is only accessible from that method. The object reference will go out of scope as soon as the MyWindow method finishes executing. To get around this, you could put the treeView1 reference variable as an instance variable at the top of the class, outside of any method. You can then instantiate the Tree View object from inside the contructor. For example;
using System;
using System.Windows.Forms;
using System.Drawing;
namespace FormFun
{
class MyWindow : Form
{
private TreeView treeView1; //this is now accessible to all methods in this class
public MyWindow() : base()
{
this.treeView1 = new TreeView(); //initialise tree view object
treeView1.Size = new Size(300, 300);
}
This post has been edited by CodingSup3rnatur@l-360: 25 October 2010 - 08:36 AM
#4
Re: Need help with a stupid event handler mistake
Posted 25 October 2010 - 08:25 AM
Thanks for the help guys, moving the treeview declaration outside the constructor definitely helped. I am curious to learn more about insertAlias' proposed solution though as I think that method is cleaner than having the declaration at the form level.
When I use:
to try to hook onto the treeview object inside the event handler, i get a null reference exception, can you possibly show me an example of how this would work? That event should fire when the form loads, is the sender the form rather than the treeview object? is that why this is throwing an exception?
Thanks much for your advice, I am 100% self taught on all OO concepts and have only been doing C# for a few days, so please do excuse me if I am overlooking something obvious.
When I use:
TreeView treeView = sender as TreeView;
to try to hook onto the treeview object inside the event handler, i get a null reference exception, can you possibly show me an example of how this would work? That event should fire when the form loads, is the sender the form rather than the treeview object? is that why this is throwing an exception?
Thanks much for your advice, I am 100% self taught on all OO concepts and have only been doing C# for a few days, so please do excuse me if I am overlooking something obvious.
#5
Re: Need help with a stupid event handler mistake
Posted 25 October 2010 - 08:39 AM
What event do you have your code in? Casting the sender object will only work for TreeView events. You won't be able to do that for an event like the MyWindow_Load event.
Yes, that's why you are getting the exception. The sender is the control that caused the event. The Form control is what causes the Load event to happen, therefore, the sender is going to be an instance of the Form.
onethirtyone, on 25 October 2010 - 11:25 AM, said:
That event should fire when the form loads, is the sender the form rather than the treeview object? is that why this is throwing an exception?
Yes, that's why you are getting the exception. The sender is the control that caused the event. The Form control is what causes the Load event to happen, therefore, the sender is going to be an instance of the Form.
#6
Re: Need help with a stupid event handler mistake
Posted 25 October 2010 - 08:40 AM
I apologize, I misread your code. The event is the "Load" event of the main form, not any event tied to the TreeView. Your only viable solution is to declare it with a class scope (outside the methods/constructor).
sender in this context would be the current instance of Mywindow.
Just so you know, declaring all UI controls with a class-level scope is actually how the Form Designer does it. If you ever get bored, open up the Mywindow.Designer.cs file that is generated when you use the WYSIWYG editor to make a form. You'll see that they declare them basically the same way you're forced to in this instance.
sender in this context would be the current instance of Mywindow.
Just so you know, declaring all UI controls with a class-level scope is actually how the Form Designer does it. If you ever get bored, open up the Mywindow.Designer.cs file that is generated when you use the WYSIWYG editor to make a form. You'll see that they declare them basically the same way you're forced to in this instance.
#7
Re: Need help with a stupid event handler mistake
Posted 25 October 2010 - 08:47 AM
Makes sense - thanks for the help all.
Edit: Final, working, result for anyone as stupid as me who happens to stumble upon this thread.
Edit: Final, working, result for anyone as stupid as me who happens to stumble upon this thread.
using System;
using System.Windows.Forms;
using System.Drawing;
namespace FormFun
{
class MyWindow : Form
{
Button btnExit = new Button();
TreeView treeView1 = new TreeView();
Label lblGreeting = new Label();
//Constructor
public MyWindow() : base()
{
//Form
this.Text = "My First Windows Application";
this.Size = new Size(500, 500);
this.Load += new EventHandler(MyWindow_Load);
//Label
lblGreeting.Text = "Hello WinForm";
lblGreeting.Location = new Point(20, 20);
//Button
btnExit.Text = "Exit";
btnExit.Location = new Point(400, 430);
btnExit.Size = new Size(80, 30);
btnExit.Click += new EventHandler(btnExit_Click);
//treeView
treeView1.Location = new Point(50, 50);
treeView1.Size = new Size(300, 300);
//Add Controls to form
this.Controls.AddRange(new Control[] { lblGreeting, btnExit, treeView1 });
}
public void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void MyWindow_Load(object sender, EventArgs e)
{
treeView1.Nodes.Clear();
treeView1.Nodes.Add("Programming Languages");
TreeNode node = new TreeNode("Object Oriented Programming Languages");
node.Nodes.Add("C++");
TreeNode subnode = node.Nodes.Add("Framework and runtime based languages");
subnode.Nodes.Add("Java");
subnode.Nodes.Add("C#");
treeView1.Nodes[0].Nodes.Add(node);
}
}
class Test
{
static void Main()
{
Application.Run(new MyWindow());
}
}
}
This post has been edited by onethirtyone: 25 October 2010 - 08:50 AM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|