473,466 Members | 1,367 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Binding validation / tool-strip

Maybe I'm having a dumb day...

I'm having some "fun" trying to force a tool-strip to trigger
validation, i.e. so that the last (bound) control being edited updates
the model with changes. Can anybody see what I am doing daft? The
"save" in this case writes the values into the Form's caption - if you
are mid-edit then the last committed value is used - which risks data
loss.

All I want to do is force validation, as though a regular Control was
used (see the Button in this code below). For now (in the actual code)
I've hacked it by switching control in the handler - but this is not
very pleasing; any input?

Marc

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;

// just a data class that supports notification
sealed class SomeData : INotifyPropertyChanged {
private bool UpdateField<T>(ref T field, T value, string
propertyName) {
if (EqualityComparer<T>.Default.Equals(field, value)) return
false; // no change
field = value;
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new
PropertyChangedEventArgs(propertyName));
return true;
}
private string name = "Fred";
private int size = 24;
public string Name {
get { return name; }
set { UpdateField(ref name, value, "Name"); }
}
public int Size {
get { return size; }
set { UpdateField(ref size, value, "Size"); }
}
public event PropertyChangedEventHandler PropertyChanged;
}
// a form with 2 save buttons; one Button, one ToolStripButton
sealed class Demo : Form {
readonly SomeData data = new SomeData();
static void Main() {
Application.EnableVisualStyles();
Application.Run(new Demo());
}

private void AddEditor(string property) {
TextBox tb = new TextBox();
tb.Name = "Editor_" + property;
tb.Dock = DockStyle.Top;
Binding b = tb.DataBindings.Add("Text", data, property);
b.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
b.DataSourceUpdateMode = DataSourceUpdateMode.OnValidation;
Controls.Add(tb);
tb.BringToFront();

}

Demo() {
ToolStrip ts = new ToolStrip();
ts.Items.Add("Save fails (old values used)", null,
Save_Click);
ts.Dock = DockStyle.Top;
ts.CausesValidation = true;
Controls.Add(ts);

Button btn = new Button();
btn.Text = "Save works (new values used)";
btn.CausesValidation = true;
btn.Click += Save_Click;
btn.Dock = DockStyle.Top;
Controls.Add(btn);
btn.BringToFront();

AddEditor("Name");
AddEditor("Size");
AddEditor("Name");
AddEditor("Size");
}

void Save_Click(object sender, EventArgs args) {
Text = data.Name + " : " + data.Size;
}
}
Jun 20 '07 #1
0 1656

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

Similar topics

0
by: Cees Wesseling | last post by:
For a couple of years I am using now my own data binding tool that can create C++ classes from a DTD. At this moment I need to reconsider this tool since I want to use W3C XML Schemas and need to...
67
by: Scott Meyers | last post by:
I have a web site that, due to maintenance by several people, some of whom are fairly clueless about HTML and CSS, etc. (notably me), has gotten to the point where I'm pretty sure it's suffering...
13
by: Paul Slavin | last post by:
I have a textbox bound to a dataview, when I update the text in the textbox no changes take place in the underlying dataset. Why is this?? any answers appreciated, as to due to the underlying...
0
by: maxim mat | last post by:
Hi I need to build client for web service. But when I'm using Visual Studio .NET to add Web Reference, I get error: "Custom tool error: Unable to import WebService/Schema. Unable to import...
2
by: GD | last post by:
Hi, There are serveral validation controls in our intranet application. The controls work fine on all of our local machines that have Windows XP professional operating systems on them. After...
5
by: Chris H | last post by:
Hello All, Hope all is having a cool holiday season... I am looking for a code validation tool for VB.NET. It should be able to point out variables that are not in use as well as ways to make...
0
by: Steven Bolard | last post by:
Hello, I am trying to port my .net 1.1 application to 2.0. I am using vs2005. I am trying to get my webservices to run and although i can compile them and and get wsdl and service descriptions...
2
by: steadyspirit | last post by:
I'm now using STG XML validation tool to validate my XML files through uploading. But, STG can not include local DTD files. Also, testing XML files on Firefox and IE always lead to inconsistent...
0
by: diff | last post by:
I am having trouble preventing a bound source from updating using event-driven validation. There is a very nice article on MSDN explaining this;...
8
by: Bryan | last post by:
I want my business objects to be able to do this: class Person(base): def __init__(self): self.name = None @base.validator def validate_name(self): if not self.name: return
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
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,...
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
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
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
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.