|
-
April 29th, 2009, 05:51 AM
#4
Re: Problems with the event handler
 Originally Posted by nightscorpion
Code:
public bool CreateButton(TabPage tp, Object parentobj)
{
Button button = new Button();
button.Location = new System.Drawing.Point(250,440);
tp.Controls.Add(button);
parentglobalobj = parentobj;
button.Click += new EventHandler(handler1);
return true;
}
public void handler1(object sender, EventArgs e)
{
/ / tp.text takes the last tabpage name n not the tabpage from where the respective button was created
test.addform(parentglobalobj, tp.text);
}
Hi !
At first please use codetags as it is written in our forum rules
Its needed because code looses formating when nor done between Codetags. The advanced editor has a button for it to create them.
Second problem is:
I cannot see in your code the scope of your variables
So what is the scope of your parentglobalobj whats the scope of test and how this is defined also whats tp defined in handler1
if it is the TabPage wherefrom did you get this tp ?
I'm asking because this may be your problem. If tp is global sope in this Form and you have created one TabPage and used tp to keep its adress then this is overwritten when you are creating the next tab page so you always will get the last created page.
I cannot find out what you are trying to accomplish
Why not simple using the tab buttons itself ? The TabControl has a property where you will be able to access every single Tabpage.
Simple use a TabControl and add all your pages to it. In the TabPages[i] you will find them again.
Additional: Using Objects ( if not absolutly necessary) is a bad habit and should not be used whenever possible. Use generic Lists when necessary or for a parent you will normally know what Type of class the parent is. Interfaces could also be used, Objects - no ! Its quite the same like driving without security belt.
Next Question: You are creating the button in a method which returns a boolean. The code within that method has no chance not to return true. Yes per sure it can maybe create an exception before it is ready and then the result would not be true. But there is no try /catch for an exception in this method, so if that happend ( I personally cannot imagine why this should happen ) the code will crash anyway. So this method could simple be void instead of bool
Last edited by JonnyPoet; April 29th, 2009 at 05:58 AM.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|