473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Copy of a control

Hello,

is there any simple posibility to have a control on 2 diferent tabs of a
tabcontrol without synchronizing? I want to have e.g. a checkbox with the
same label and functionality on two different possitions (e.g. on two
different tabs of a tabcontrol). If I check the first the second sould be
checked too. Adding the same control to each of the tabs dosn't work,
because only the last add to a control will have the checkcontrol the other
additions are deleated. The only two solutions I found jet were
synchronizing 2 checkboxes (event checkbox_1 checked will check the second
and reverse) wich isn't a nice solution. The other one would be to catch the
tab changed event and add the checkbox control to the selected tab each time
the tabcontrol changes.
Are there any better posibilities?

regards Andi
Jul 5 '07 #1
6 2403
Perhaps data-bind the two check-boxes to a single object property that
supports notifications, and let the bindings do some work? Let me know
if you need more detail.

Marc
Jul 5 '07 #2
What the heck ;-p

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
static class Program {
static void Main() {
Application.EnableVisualStyles();
using (DemoForm form = new DemoForm()) {
Application.Run(form);
}
}
}
sealed class DemoForm : Form {
private void AddCheckbox(string caption) {
CheckBox cb = new CheckBox();
cb.Dock = DockStyle.Top;
cb.Text = caption;
cb.DataBindings.Add("Checked", sync, "Enabled",true,
DataSourceUpdateMode.OnPropertyChanged);
Controls.Add(cb);
}
private readonly DummySync sync = new DummySync();

protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
AddCheckbox("Bing");
AddCheckbox("Bang");
AddCheckbox("Bong");
}
private sealed class DummySync : INotifyPropertyChanged {
private bool enabled;
public bool Enabled {
get { return enabled; }
set { UpdateField(ref enabled, value, "Enabled"); }
}
private void UpdateField<T>(ref T field, T value, string
propertyName) {
if (EqualityComparer<T>.Default.Equals(field, value))
return;
field = value;
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new
PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
Jul 5 '07 #3
Marc,
That is the best kind of answer - code that works. (This bit sounds
like sarcasm, but it isnt...) And without comments - yes we still have
to do some work to 'find out what it all means', but we can get a
working program and then understand it/modify it later.

Maximum respect.

On 5 Jul, 09:26, "Marc Gravell" <marc.grav...@gmail.comwrote:
What the heck ;-p

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
static class Program {
static void Main() {
Application.EnableVisualStyles();
using (DemoForm form = new DemoForm()) {
Application.Run(form);
}
}}

sealed class DemoForm : Form {
private void AddCheckbox(string caption) {
CheckBox cb = new CheckBox();
cb.Dock = DockStyle.Top;
cb.Text = caption;
cb.DataBindings.Add("Checked", sync, "Enabled",true,
DataSourceUpdateMode.OnPropertyChanged);
Controls.Add(cb);
}
private readonly DummySync sync = new DummySync();

protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
AddCheckbox("Bing");
AddCheckbox("Bang");
AddCheckbox("Bong");
}
private sealed class DummySync : INotifyPropertyChanged {
private bool enabled;
public bool Enabled {
get { return enabled; }
set { UpdateField(ref enabled, value, "Enabled"); }
}
private void UpdateField<T>(ref T field, T value, string
propertyName) {
if (EqualityComparer<T>.Default.Equals(field, value))
return;
field = value;
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new
PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}

}- Hide quoted text -

- Show quoted text -

Jul 5 '07 #4
Allow me to annotate:

The DummySync class exposes a property, "Enabled", that supports full
data binding - primarily by correctly implementing the
INotifyPropertyChanged interface : whenever the property changes, it
notifies any interested parties (via the event), telling them what
changed.

We create a selection of check-boxes, and tell them to bind their
"Checked" property to the "Enabled" property on a common instance of
DummySync (this "common instance" is important, as in this case you
want all the checkboxes to agree on whether they are checked). This
binding (provided by the framework) does 4 things:
* initally, it will query the "Enabled" value of the common instance
and set "Checked" accordingly
* it subsribes to change notification on the common instance
* when the notification is triggered it re-reads the value
* when the Control's own property is changed directly, it updates the
common instance (indirectly causing the other controls to update
themselves)

This pattern is very useful for abstracting "what features must the UI
offer" from "what must the UI look like"; you can have, for instance,
a SystemOptions class with various checkboxes, textboxes, etc - and by
simply using data-binding you don't need to do a lot of manual "read
the 17 settings from the UI"; additionally, you can easily swap out
the UI (perhaps to use WPF) without having to make major code changes.

Directly linking to the CheckedChanged event-handlers of each control
and updating the siblings would, arguably, work equally - but for my
money the above is quite elegant and (at the minimum) worthy of
consideration.

Marc
Jul 5 '07 #5
Thank you, very nice pice of code. Is there any posibility to use this with
buttons too (click event and enable / disable)?

regards Andi
Jul 5 '07 #6
This code snippet is pretty much textbook "observer" pattern (mvp too?
I'm not a comp-sci buff); you should be able to use it for any
bindable/browsable property pair, which means most of them. I'm 99%
sure you can sink it to the "Enabled" property on a button - however,
there is no suitable property that gets set automatically on click, so
you'd have to code something. Some types (ToolStripMenuItem for
example) have suitable-looking properties (i.e. Checked when combined
with CheckOnClick = true), but aren't bindable... it doesn't fit every
situation, but is quite tidy most of the time.

Marc
Jul 5 '07 #7

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

Similar topics

3
by: Lore Leuneog | last post by:
Hello Which command do I've to use to set the focus on a control placed on a report in Design-View. Ctrl.SetFocus and Cmd.GoToControl don't work for a report. The aim is to copy the control...
3
by: Phil Stanton | last post by:
I am trying to produce a program that will output any Access report to an editable Word document. So far it is working well but slowly with text boxes, labels, and lines. I now need to try to...
5
by: Woody Splawn | last post by:
I seem to remember that you can copy a control, like a textbox AND any code associated with it to another form. Is this true. If so, how? When I select a textbox, for example in form and, copy...
4
by: Nathan Sokalski | last post by:
I have a Control that I want to copy as a copy of the Control, not a copy of the reference to the original. My reason for doing this is because some of the methods I would calling would prevent...
6
by: Ben R. | last post by:
Hi, I've got a vb.net winforms app. Out of the box, I can use Ctrl X, C and V as expected in controls like textboxes. I've got a menustrip, and if I click the link "Add standard items" which...
17
by: Steve | last post by:
I'm trying to code cut, copy, and paste in vb 2005 so that when the user clicks on a toolbar button, the cut/copy/paste will work with whatever textbox the cursor is current located in (I have...
10
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and...
0
by: =?Utf-8?B?TmVpbGc=?= | last post by:
I have a SplitContainer in which I have TextBox filled on one side and a RichTextBox on the other. I just noticed when I do a text select in either textbox, the controls don't respond to...
1
by: fl | last post by:
Hi, I browse through C++ primer. I have a question about the calling of copy constructor in the example of that book. Below, "item" is an associate container. In main func, there is a call of...
14
by: Newbie23 | last post by:
Hi All, I'm trying to develop a control button on my form 'frm_initialProperty' that will copy a contact address (5 fields) from my tbl_customer table to my tbl_properties table. The form...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
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,...
0
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...

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.