473,951 Members | 60,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nullable ComboBox

1 New Member
I want to create generic bound NullableComboBo x class which allows to enter null values. I havent found a way to store null value to DataSource.

To reproduce:

1. Run the code
2. Clear text
3. Press button.

Observed:

Value is not null

Expected:

Value must be null

How to fix ?

Andrus.


using System.Windows. Forms;
using System.Collecti ons.Generic;
using System.Componen tModel;

class testForm : Form {

Storage s = new Storage();

testForm() {
Customer c = new Customer();
c.Id = "J";
c.Name = "John";

List<Customer> l = new List<Customer>( );
l.Add(c);

NullableComBobo x cm = new NullableComBobo x();
cm.DisplayMembe r = "Name";
cm.ValueMember = "Id";
cm.DataSource = l;

s.CustId = "J";

cm.DataBindings .Add("SelectedV alue", s, "CustId", true,
System.Windows. Forms.DataSourc eUpdateMode.OnV alidation, null);

Controls.Add(cm );
Button b = new Button();
b.Top = 80;
b.Click += new System.EventHan dler(b_Click);
Controls.Add(b) ;

}

void b_Click(object sender, System.EventArg s e) {
if (s.CustId == null)
MessageBox.Show ("Value is null");
else
MessageBox.Show ("Value is NOT null " + s.CustId.ToStri ng());

}

class Customer {
public string Id { get; set; }
public string Name { get; set; }

}

class Storage {
public string CustId { get; set; }
}

static void Main() {
Application.Run (new testForm());
}
}

/// <summary>
/// Combobox which allows to enter null value
/// </summary>
public class NullableComBobo x : ComboBox {

protected override void OnValidating(Ca ncelEventArgs e) {

if (Text.Trim().Le ngth == 0) {
//SelectedItem = null;
SelectedIndex = -1;
base.OnValidati ng(e);
return;
}

int pos = FindString(Text .Trim());
if (pos >= 0) {
SelectedIndex = pos;
base.OnValidati ng(e);
return;
}

e.Cancel = true;
MessageBox.Show ("Invalid entry");
base.OnValidati ng(e);
}
}
Dec 19 '07 #1
1 2196
Plater
7,872 Recognized Expert Expert
Observed:

Value is not null

Expected:

Value must be null

How to fix ?

Andrus.
Expand|Select|Wrap|Line Numbers
  1. if (Value =="")
  2.    Value=null;
  3.  
Dec 19 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
5984
by: ESPNSTI | last post by:
Hi, Please don't shoot me if the answer is simple, I'm new to .Net and C# :) .. I'm attempting to convert a nullable type to it's "non-nullable" type in a generic way (without knowing what specific type the nullable type is.) The reason I'm trying this is because when I attempt to pass a nullable type value to a SqlCommand parameter and then attempt to execute it I the following error: "No mapping exists from object type...
30
4637
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a corresponding field in the lookup table. In my data table we store the ID in what I will call the 'key' field. == Description of the desired operation:
8
4865
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked one of these syntax formats to work; // if ( l_flg_01 && l_flg_02 && l_flg_03 ) // Line A
0
1421
by: Larry Lard | last post by:
There seems to be something a bit lacking in the way the dataset designer thing deals (or rather doesn't) with nullable fields in VS2005. Maybe it's cos I'm using VB2005 Express (which is variously crippled), I don't know. But it seems to me obvious that there should be some way to specify that DB fields which can be null should be mapped to appropriate Nullable(Of T) generic types. For fields there's a dropdown in the dataset designer...
1
6033
by: Brian | last post by:
Is there a way to bind a nullable Enum property to a combo box? When choosing a value, and then leaving the combo box, the value reverts back to the original value. It seems the databinding doesn' accept the new value. To populate the combo box: cboPageMode.DataSource = Enum.GetValues(typeof(PageMode));
1
5689
by: Joe Bloggs | last post by:
Hi, Can someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide) http://msdn2.microsoft.com/en-us/library/ms366789.aspx however, using their code snippet, I couldn't get it to work:
5
51745
by: GG | last post by:
I am trying to add a nullable datetime column to a datatable fails. I am getting exception DataSet does not support System.Nullable<>. None of these works dtSearchFromData.Columns.Add( new DataColumn( "StartDate", typeof( DateTime? ) ) ); dtSearchFromData.Columns.Add( new DataColumn( "EndDate", typeof( System.Nullable<DateTime>) ) ); Any ideas?
8
10716
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
2
180
by: Tony Johansson | last post by:
Hello! These two declarations(1 and 2) are the same I assume. 1. System.Nullable<intnullable; 2. System.Nullable<intnullable = new System.Nullable<int(); So because these 1 and 2 are the same is no point to use the longer declaration as 2 it good enough to use decaration 1.
0
10172
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9992
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11195
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11371
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8269
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6235
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6353
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4554
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.