PC Review


Reply
Thread Tools Rate Thread

Custom Control Event

 
 
Marc Solé
Guest
Posts: n/a
 
      12th Jun 2006
Hello to everybody.

I have a little problem with a Custom Control that I have created. The thing
is that in my control, when I click a button, it should create another
object in the main form.

My control is located in a TabPage, and I want that when the user clicks a
button, a new tabPage is created, but by now, I don't know how to do that.

Any ideas, suggestions, links are wellcomed.

Many thanks,

Marc


 
Reply With Quote
 
 
 
 
Tasos Vogiatzoglou
Guest
Posts: n/a
 
      12th Jun 2006
To add a tab page to an existing tab control you need the following :

TabControl tb1;

TabPage tabPage1 = new TabPage();
tabPage1.Text="Tab page1";
tb1.Controls.Add(tabPage1);


Hope this helps..

Regards,
Tasos



Marc Solé wrote:
> Hello to everybody.
>
> I have a little problem with a Custom Control that I have created. The thing
> is that in my control, when I click a button, it should create another
> object in the main form.
>
> My control is located in a TabPage, and I want that when the user clicks a
> button, a new tabPage is created, but by now, I don't know how to do that.
>
> Any ideas, suggestions, links are wellcomed.
>
> Many thanks,
>
> Marc


 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      12th Jun 2006
Well, to do it /properly/ you need to think about what each object should
legitimately be able to see; your control instance would either need to know
about the tab-control instance somehow, or the code containing your control
needs to catch an event and add the page outside of your control (since the
TabControl is not normally available inside your control - their could be 17
TabControl instances nested on the form for instance; which one do we add
to?).

From your post, the former seems more like what you want? In which case a
property may suffice:

using System;
using System.Windows.Forms;

public class MyControl : UserControl {
public MyControl() {
Button button = new Button();
button.Text = "Click Me";
Size = button.Size;
button.Dock = DockStyle.Fill;
button.Click += new EventHandler(button_Click);
Controls.Add(button);
}
private TabControl _tabControl;
public TabControl ParentTabControl {
get { return _tabControl; }
set { _tabControl = value; }
}
void button_Click(object sender, EventArgs e) {
TabControl tab = ParentTabControl;
if (tab == null) return;
tab.TabPages.Add("New page");
}
}

public class Program {
private static void Main() {

using (Form form = new Form())
using (TabControl tc = new TabControl())
using (MyControl mc = new MyControl())
{
tc.Dock = DockStyle.Fill;
form.Controls.Add(tc);
tc.TabPages.Add("Page1");
mc.ParentTabControl = tc; // key line
tc.TabPages[0].Controls.Add(mc);
form.ShowDialog();
}
}
}


 
Reply With Quote
 
Marc Solé
Guest
Posts: n/a
 
      12th Jun 2006
Thanks for your answers Tasos and Marc. They have been very helpfully.

Marc

"Marc Solé" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello to everybody.
>
> I have a little problem with a Custom Control that I have created. The

thing
> is that in my control, when I click a button, it should create another
> object in the main form.
>
> My control is located in a TabPage, and I want that when the user clicks a
> button, a new tabPage is created, but by now, I don't know how to do that.
>
> Any ideas, suggestions, links are wellcomed.
>
> Many thanks,
>
> Marc
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom event from a custom web control is fired twice SammyBar Microsoft ASP .NET 1 21st Jul 2009 08:16 PM
Help with custom control and event. David Microsoft Dot NET Framework Forms 1 25th Oct 2007 03:40 PM
Web custom control losing event handling on included control =?Utf-8?B?Q2Fyb2x5biBWbw==?= Microsoft C# .NET 0 27th Jul 2005 12:18 AM
Capturing event from other custom control within another custom control Jonah Olsson Microsoft ASP .NET 1 5th Apr 2005 02:39 PM
Custom Control Event Ryan Joseph So Microsoft C# .NET 1 29th Mar 2004 12:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:55 AM.