473,246 Members | 1,587 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,246 software developers and data experts.

Disabling Controls when BindingSource.Count == 0

Hello:

I am sure this question comes up a lot. I need to disable the controls
on my Windows forms so that when the BindingSource is empty some the
controls bound to it will be disabled.

This will make it clear to the user that they have to create a new
item first before they start working.

This needs to be an easy process because there are many, many forms.
We can't just go through every control and disable it since some
controls need to always be enabled. My hope was to perform the
operation with some sort of binding.

We have considered other options, such as automatically inserting when
the binding source is empty, but that comes with its own set of
problems. We think disabling is probably the most straighforward way
to eliminate the problem.

Any insight would be helpful.

Thanks,
Travis
Sep 30 '08 #1
5 4877
On Sep 30, 10:15*am, "jehugalea...@gmail.com" <jehugalea...@gmail.com>
wrote:
Hello:

I am sure this question comes up a lot. I need to disable the controls
on my Windows forms so that when the BindingSource is empty some the
controls bound to it will be disabled.

This will make it clear to the user that they have to create a new
item first before they start working.

This needs to be an easy process because there are many, many forms.
We can't just go through every control and disable it since some
controls need to always be enabled. My hope was to perform the
operation with some sort of binding.

We have considered other options, such as automatically inserting when
the binding source is empty, but that comes with its own set of
problems. We think disabling is probably the most straighforward way
to eliminate the problem.

Any insight would be helpful.

Thanks,
Travis
This group is focused on the C# language itself, not WinForms, so
maybe another group may provide better advice, I do remember however
seeing a discussion here in the last month or so about something
similar.

My post then was about disabling a bunch of controls by placing them
in a panel and only disabling the panel.

If I'm not mistaken, you want to look into the events that
BindingSource exposes and handle enable/disable there. If that doesn't
work, then you need to handle individual control's binding event - you
could probably write a single handle and point all relevant controls
to it.
Sep 30 '08 #2
On Sep 30, 11:09*am, "G.S." <gstoy...@gmail.comwrote:
On Sep 30, 10:15*am, "jehugalea...@gmail.com" <jehugalea...@gmail.com>
wrote:


Hello:
I am sure this question comes up a lot. I need to disable the controls
on my Windows forms so that when the BindingSource is empty some the
controls bound to it will be disabled.
This will make it clear to the user that they have to create a new
item first before they start working.
This needs to be an easy process because there are many, many forms.
We can't just go through every control and disable it since some
controls need to always be enabled. My hope was to perform the
operation with some sort of binding.
We have considered other options, such as automatically inserting when
the binding source is empty, but that comes with its own set of
problems. We think disabling is probably the most straighforward way
to eliminate the problem.
Any insight would be helpful.
Thanks,
Travis

This group is focused on the C# language itself, not WinForms, so
maybe another group may provide better advice, I do remember however
seeing a discussion here in the last month or so about something
similar.

My post then was about disabling a bunch of controls by placing them
in a panel and only disabling the panel.

If I'm not mistaken, you want to look into the events that
BindingSource exposes and handle enable/disable there. If that doesn't
work, then you need to handle individual control's binding event - you
could probably write a single handle and point all relevant controls
to it.- Hide quoted text -

- Show quoted text -
.... something along those lines:
http://www.codeproject.com/KB/vb/Dat...OnlyProps.aspx
Sep 30 '08 #3
On Tue, 30 Sep 2008 07:15:20 -0700, je**********@gmail.com
<je**********@gmail.comwrote:
I am sure this question comes up a lot. I need to disable the controls
on my Windows forms so that when the BindingSource is empty some the
controls bound to it will be disabled.

This will make it clear to the user that they have to create a new
item first before they start working.

This needs to be an easy process because there are many, many forms.
We can't just go through every control and disable it since some
controls need to always be enabled. My hope was to perform the
operation with some sort of binding. [...]
You don't have enough information in your question for us to understand
exactly what you're doing. For example, what kind of control are you
using? What kind of data source is bound to the control? Is it always an
instance of BindingSource? Or do you bind with other types as well?

Generally speaking, I'm not aware of any particular feature in data
binding that does exactly that. But you can easily implement what you're
asking using the appropriate events on the objects you're dealing with.

In particular, when you create the controls, subscribe a handler to the
DataSourceChanged event. In that event, depending on the type of the data
source, subscribe a handler to the appropriate event (e.g. ListChanged)
that will enable/disable the control as appropriate for the data.

For example, let's assuming we're binding a BindingSource instance to a
DataGridView control. The above might look something like this:

class Form1 : Form
{
public Form1()
{
InitializeComponent();

dataGridView1.DataSourceChanged +=
dataGridView1_DataSourceChanged;
}

private void dataGridView1_DataSourceChanged(object sender,
EventArgs e)
{
DataGridView dgv = (DataGridView)sender;

if (dgv.DataSource != null)
{
BindingSource source = (BindingSource)dgv.DataSource;

source.ListChanged += delegate(object sender,
ListChangedEventArgs e)
{
BindingSource source = (BindingSource)sender;

dgv.Enabled = source.Count 0;
}
}
}
}

The above assumes that if the binding source for a control is changed, the
source itself is discarded, never used again. If that assumption isn't
true, you'll have to include some code to unsubscribe the previous
BindingSource.ListChanged event handler, to ensure that the control
doesn't wind up getting enabled or disabled spuriously (or worse, after
it's been disposed). This is kind of a pain, because you'd have to keep a
reference to the previous DataSource, but it's doable and necessary in
that situation. (It's unfortunate that the "XXXChanged" events in .NET
don't include an easy way to get at the old value, but that's what we're
stuck with).

Pete
Sep 30 '08 #4
On Sep 30, 11:46*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Tue, 30 Sep 2008 07:15:20 -0700, jehugalea...@gmail.com *

<jehugalea...@gmail.comwrote:
I am sure this question comes up a lot. I need to disable the controls
on my Windows forms so that when the BindingSource is empty some the
controls bound to it will be disabled.
This will make it clear to the user that they have to create a new
item first before they start working.
This needs to be an easy process because there are many, many forms.
We can't just go through every control and disable it since some
controls need to always be enabled. My hope was to perform the
operation with some sort of binding. *[...]

You don't have enough information in your question for us to understand *
exactly what you're doing. *For example, what kind of control are you *
using? *What kind of data source is bound to the control? *Is it always an *
instance of BindingSource? *Or do you bind with other types as well?

Generally speaking, I'm not aware of any particular feature in data *
binding that does exactly that. *But you can easily implement what you're *
asking using the appropriate events on the objects you're dealing with.

In particular, when you create the controls, subscribe a handler to the *
DataSourceChanged event. *In that event, depending on the type of the data *
source, subscribe a handler to the appropriate event (e.g. ListChanged) *
that will enable/disable the control as appropriate for the data.

For example, let's assuming we're binding a BindingSource instance to a *
DataGridView control. *The above might look something like this:

* * *class Form1 : Form
* * *{
* * * * *public Form1()
* * * * *{
* * * * * * *InitializeComponent();

* * * * * * *dataGridView1.DataSourceChanged += *
dataGridView1_DataSourceChanged;
* * * * *}

* * * * *private void dataGridView1_DataSourceChanged(object sender, *
EventArgs e)
* * * * *{
* * * * * * *DataGridView dgv = (DataGridView)sender;

* * * * * * *if (dgv.DataSource != null)
* * * * * * *{
* * * * * * * * *BindingSource source = (BindingSource)dgv.DataSource;

* * * * * * * * *source.ListChanged += delegate(object sender, *
ListChangedEventArgs e)
* * * * * * * * *{
* * * * * * * * * * *BindingSource source = (BindingSource)sender;

* * * * * * * * * * *dgv.Enabled = source.Count >0;
* * * * * * * * *}
* * * * * * *}
* * * * *}
* * *}

The above assumes that if the binding source for a control is changed, the *
source itself is discarded, never used again. *If that assumption isn't*
true, you'll have to include some code to unsubscribe the previous *
BindingSource.ListChanged event handler, to ensure that the control *
doesn't wind up getting enabled or disabled spuriously (or worse, after *
it's been disposed). *This is kind of a pain, because you'd have to keep a *
reference to the previous DataSource, but it's doable and necessary in *
that situation. *(It's unfortunate that the "XXXChanged" events in .NET*
don't include an easy way to get at the old value, but that's what we're *
stuck with).

Pete
Thanks, Pete. That was informative. I kind of took your idea and made
an extender control. However, the dang thing doesn't work, and I can't
figure out why. If this topic is still alive, could you please look
and see. I'd appreciate it.

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using PowerBillingUI.Controls;

namespace PowerBillingUI.Helpers
{
[ProvideProperty("DisableWhenUnbound", typeof(Control))]
public class DisableHelper : Component, IExtenderProvider
{
private class Handlers
{
public ListChangedEventHandler ListChangedHandler;
public EventHandler EnableChangedHandler;
}

private IndirectBindingSource bindingSource; // just think of
this as a BindingSource
private IDictionary<Control, Handlerslisteners;
private bool enabled;

public DisableHelper()
{
listeners = new Dictionary<Control, Handlers>();
}

public bool Enabled
{
get
{
return enabled;
}
set
{
if (bindingSource != null && enabled)
{
foreach (KeyValuePair<Control, Handlerspair in
listeners)
{
bindingSource.ListChanged -=
pair.Value.ListChangedHandler;
pair.Key.EnabledChanged -=
pair.Value.EnableChangedHandler;
}
}
enabled = value;
if (enabled && bindingSource != null)
{
foreach (KeyValuePair<Control, Handlerspair in
listeners)
{
bindingSource.ListChanged +=
pair.Value.ListChangedHandler;
pair.Key.EnabledChanged +=
pair.Value.EnableChangedHandler;
}
}
}
}

public IndirectBindingSource BindingSource
{
get
{
return bindingSource;
}
set
{
bindingSource = value;
}
}

public bool CanExtend(object extendee)
{
return extendee is Control;
}

[DefaultValue(false)]
public bool GetDisableWhenUnbound(Control control)
{
return listeners.ContainsKey(control);
}

public void SetDisableWhenUnbound(Control control, bool
disableWhenUnbound)
{
Handlers handlers;
if (!listeners.TryGetValue(control, out handlers))
{
handlers = null;
}
if (handlers != null)
{
if (bindingSource != null)
{
bindingSource.ListChanged -=
handlers.ListChangedHandler;
}
control.EnabledChanged -=
handlers.EnableChangedHandler;
handlers = null;
}
if (disableWhenUnbound)
{
handlers = new Handlers();
handlers.EnableChangedHandler = (EventHandler)
delegate(object sender, EventArgs e)
{
control.Enabled = bindingSource.Count 0;
};
handlers.ListChangedHandler =
(ListChangedEventHandler)
delegate(object sender, ListChangedEventArgs e)
{
control.Enabled = !control.Enabled; // fires
EnableChangedHandler
};
}
if (handlers == null)
{
listeners.Remove(control);
}
else
{
listeners[control] = handlers;
}
}
}
}
Oct 2 '08 #5
Nevermind. It does work so long as you set Enabled at the right time.
Oct 2 '08 #6

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

Similar topics

2
by: Don | last post by:
is there any way to disable the controls on a form without changing their appearance? i would like to make it impossible for a user to click buttons and edit text boxes while the controls retain...
3
by: Mike | last post by:
That is exactly what I am looking for. Just one thing, after I turn the cursor into a wait cursor, is there a way to disable all the controls (e.g buttons, textboxes) so that I can't click on...
4
by: Dan =o\) | last post by:
Hi guys, in the scenario where a user fills in a form, and clicks on a button to Save, there's a period of waiting (the slower the connection between client and server, the longer the delay)...
0
by: spacehopper_man | last post by:
hi - I am writing a "tab" control. - but it's losing the viewstate of the content pane when I switch between tabs. can anyone shed any light on why I'm losing ViewState based on my simple...
3
by: Fred Flintstone | last post by:
I'm a bit confused about something. I have an aspx page populated with dynamic controls; textboxes, labels, radios and so on. On the page is a Yes/No radio combination and 3 textboxes. The...
8
by: helpful sql | last post by:
Hi, I have 2 radio buttons on my Windows form control. The radio button's CheckedChanged event disables or enables other controls on the form based on the value of the Checked property. When the...
0
by: rakhee | last post by:
Hello, I am using 2 div tags on my aspx form. I am making each one enabled and disabled using javascript based on some conditions. This is working fine. But when i disable the div tag, even though...
0
by: Rick | last post by:
VS 2005 I have some custom business objects (BindingList(of T)) connected through BindingSources to a BindingNavigator. When the user moves focus the BindingSource of the navigator can be...
3
by: =?Utf-8?B?cml2YWxAbmV3c2dyb3Vwcy5ub3NwYW0=?= | last post by:
I've a situation where I'm creating controls programatically. I'm programatically creating table cells and rows. I've got an array of data, and I want to format the data on screen in rows of 4. ...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.