473,493 Members | 4,333 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Custom Control Event

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
Jun 12 '06 #1
3 1800
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


Jun 12 '06 #2
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();
}
}
}
Jun 12 '06 #3
Thanks for your answers Tasos and Marc. They have been very helpfully.

Marc

"Marc Solé" <ms***@emdep.com> wrote in message
news:OG**************@TK2MSFTNGP05.phx.gbl...
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

Jun 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4738
by: Steve Amey | last post by:
Hi all I am creating a basic control to perform some tasks, and I want to declare some events to be raised so they can be handled from the form that the control is on. I can create my own Event...
6
2416
by: Christian H | last post by:
Hi! I've created a custom control (myDrawControl) that deals with drawing. This control is then added to a form( myMainForm) Now, whenever something is dragged and dropped onto myDrawControl ,...
3
549
by: jlea | last post by:
I've created a custom control based on TreeView and it handles several events, such as Mousedown. I added this custom control to the toolbox in another project, dragged the custom control to the...
15
10916
by: Tinus | last post by:
Hello all, I've created a custom control that draws a monthly schedule (using the Draw function in C#). Basically it draws 31 boxes and writes the day number in every box. This works...
1
1961
by: Lamont Adams | last post by:
Hi all, I've created numerous custom controls of varying complexity, but I've been on this problem for a day and a half, and I can't figure this mystery out. I hope one of you kind folks can...
3
1552
by: Eric Sabine | last post by:
I've created a custom control (ascx) which contains a tree view, a drop down list, and a listbox. The custom control is included in a web form and at runtime, won't compile (I get the...
15
6469
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
2
2717
by: Michal Valent | last post by:
I would like to fire some custom server control event before Page_Load event like this (from the trace of an aspx page) : Trace Information Category Message From First(s) From Last(s) aspx.page...
2
19391
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
2897
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7192
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6862
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5452
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.