|
Make sure CausesValidation is set to true.
If you have a custom control that was implemented differently you can force validation. My company used the following code in some of our projects:
public class Tasks
{
public static void ForceControlValidation(Control c)
{
Type t = typeof(Control);
MethodInfo m = t.GetMethod("NotifyValidating", BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.NonPublic);
object obj = m.Invoke(c, new object[0]);
if (obj is bool && (bool)obj == false)
{
MethodInfo m2 = t.GetMethod("NotifyValidated", BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.NonPublic);
m2.Invoke(c, new object[0]);
}
}
}
Does anyone have something that works better? Please let me know.
This posting is provided "AS IS" with no warranties, and confers no rights.
Alex Korchemniy
|
|
|
|
|
Alex Korchemniy wrote:
Make sure CausesValidation is set to true.
All controls have CausesValidation set to true.
The Validating/Validated events are generated when the control loses focus, for every control. But when I click on the toolbar, the focused control doesn't lose it. That's why it's value is not saved.
If I modify a control, click on some other control, and then the toolbar, there is no problem.
-- LuisR
___________
Luis Alonso Ramos
Chihuahua, Mexico
www.luisalonsoramos.com
|
|
|
|
|
The ToolBar class in the BCL - and most other third-party toolbars I've seen - never gain the focus.
You need to invoke validation when a button is clicked. Since the ToolBar class in the BCL fires a single event for any button click, it makes it easy to do this: just call the function that implements your functionality before checking which button was clicked (well, in most cases I would imagine).
How do you implement this? There are several ways, such as recursively enumerating your Controls collection for each Control (starting at your Form , UserControl , or whatever control you're extending or using for your container) and check the Focused property defined by the Control class (so every control has it, but they may not implement it - like the Panel class).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Heath Stewart wrote:
The ToolBar class in the BCL - and most other third-party toolbars I've seen - never gain the focus.
So, that's the problem then.
Heath Stewart wrote:
There are several ways, such as recursively enumerating your Controls collection for each Control
That's easy enough. I'll implement it Monday morning.
Thanks for your help!
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Heath Stewart wrote:
You need to invoke validation when a button is clicked.
How do I do invoke events for a control?
If it were a single control, I'd know in advance the event handler name and could call it directly. But I don't know. Also, I implemented the ValidateFocusedControl routine in my base clase, so it doesn't have access to derived clases' members.
I tried this:
public void ValidateFocusedControl()
{
foreach(Control ctl in Controls)
if(ValidateFocusedControlRecursive(ctl))
break;
}
bool ValidateFocusedControlRecursive(Control ctl)
{
if(ctl.Focused)
{
if(ctl.CausesValidation)
{
CancelEventArgs cea = new CancelEventArgs();
cea.Cancel = false;
ctl.Validating(ctl, cea);
if(cea.Cancel != true)
ctl.Validated(ctl, new EventArgs());
}
return true;
}
foreach(Control ctl2 in ctl.Controls)
if(ValidateFocusedControlRecursive(ctl2))
return true;
return false;
}
The recursion code works, I confirmed it with a MessageBox. But how can I call the validation routines? the above code gives me a compile error.
Thanks,
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
That's what the protected OnEventName methods are for. Note that calling these won't necessary perform the same actions as when the .NET Framework invokes them (like for mouse events), but in this case it will fire validation events that will invoke the handler list.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Heath Stewart wrote:
That's what the protected OnEventName methods are for.
But I don't have access to the protected members. I want to call them for a control belonging to a form, from the form. Any ideas?
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Reflection, or if you're referring to specific controls extend it and then you can call it (perhaps expose the protected member via a public member like Validate ).
You could also make the control lose focus and regain it like so:
Control active = ActiveControl;
Control next = active.GetNextControl(active, false);
next.Focus();
active.Focus(); * To avoid getting into a case where invalid input (like no data for a required field) try searching back when possible.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hello Heath,
Finally fixed it. This is what I did:
public void ValidateFocusedControl()
{
foreach(Control ctl in Controls)
if(ValidateFocusedControlRecursive(ctl))
break;
}
bool ValidateFocusedControlRecursive(Control ctl)
{
if(ctl.Focused)
{
if(ctl.CausesValidation)
{
TextBox txtBox = new TextBox();
txtBox.Bounds = new Rectangle(-100, -100, 1, 1);
txtBox.Parent = ctl.Parent;
txtBox.Focus();
ctl.Focus();
txtBox.Parent = null;
txtBox.Dispose();
}
return true;
}
foreach(Control ctl2 in ctl.Controls)
if(ValidateFocusedControlRecursive(ctl2))
return true;
return false;
}
Basically, just create a control, set focus to it, and then set the focus back to my control. It works!
Thanks for all your help!
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
You shouldn't instantiate and add that TextBox with each iteration of your loop! Talk about sucking up system resources. Instantiate it once in the first method, then pass a reference to it in the recursive method.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I only create the TextBox if the current control has focus and causes validation events. It is not for every control, only when I find the focused one. And once I find it, I immediately return and the loop stops. I believe that TextBox is created only once every time I call the main ValidateFocusedControl routine.
And still I only force validation when the Save button is clicked on the toolbar, so that's once or twice in the life of the form.
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Hello,
I have a MDI application, with several types of MDI children. When I get a ButtonClick event from the toolbar in the main form, I want to determine the active MDI child, so I can react appropiately.
I have some code that looks like this:
void ToolBar_OnButtonClick()
{
foreach(Form form in this.MdiChildren)
{
if(form.Focused && form.WindowState != Minimized)
{
if(form is FormType1)
MessageBox.Show("form type 1 active");
else if(form is FormType2)
MessageBox.Show("form type 2 active");
}
}
}
I would expect a messagebox telling me the type of the active form when I click a button on the toolbar, but nothing happens. Any ideas?
-- LuisR
___________
Luis Alonso Ramos
Chihuahua, Mexico
www.luisalonsoramos.com
|
|
|
|
|
See ActiveMdiChild property.
For more information see: MSDN[^]
This posting is provided "AS IS" with no warranties, and confers no rights.
Alex Korchemniy
|
|
|
|
|
Alex Korchemniy wrote:
See ActiveMdiChild property
Exactly what I need. Actually I looked for something named like MdiActiveChild or MdiChildren.ActiveForm before posting this message. I just missed the exact wording.
Thanks!
-- LuisR
___________
Luis Alonso Ramos
Chihuahua, Mexico
www.luisalonsoramos.com
|
|
|
|
|
Hello,
I have a dataTable in my database with columns title, category, problem(description of the problem) and solution. I want to develop a good search mechanism for certain problems so that the user can search for problems – solutions, when he quotes a search term. But I think it is not a good search mechanism when the specific problem with solution is only displayed when the user quotes the correct title of the problem. Is there any other possibility, which is not so difficult to implement?
Regards
mat
|
|
|
|
|
This is more of a database thing. You're going to have more luck asking in the SQL / ADO / ADO.Net Forum [^]
It boils down to how you query the database. SQL is where you need to solve the problem. Moving data into C# and using ADO.NET is not the way to go.
This posting is provided "AS IS" with no warranties, and confers no rights.
Alex Korchemniy
|
|
|
|
|
I think I can make it with Full Text Search. Does anbody know how the SQL Query String should look like, when I want to retrieve all titles of the dataTable when the quoted search term occurs in the title or the problem description or the solution?
As said my columns are
Title
Category
Problem
Solution
Regards
|
|
|
|
|
The syntac for FREETEXT is as follows:
FREETEXT ( { column | * }, 'freetext_string' )
Where
- Column is a full-text index-enabled column, to which the search is directed.
- * specifies that all columns are enabled for full-text search and that the search is directed to these columns
- 'freetext_string' specifies the search criteria. This has to be data of type char, varchar, nchar, nvarchar, or any other character data.
An example:
USE Pubs
GO
SELECT pr_info
FROM pub_info
WHERE FREETEXT (pr_info, 'PuBLiShInG')
This posting is provided "AS IS" with no warranties, and confers no rights.
Alex Korchemniy
|
|
|
|
|
But I use SQL Server 2000 and there I find no menu point FUll Text Search enable. What can I do in that case?
|
|
|
|
|
I have just read that the SQL Server CE does not support Full Text Search. Is there any other possibilty to get such an mechanism??
|
|
|
|
|
|
String s = "Lorem ipsum dolor sit amet, kec=123abc";<br />
int index = s.IndexOf("kec=");
You can then use SubString method to extract what you need.
This posting is provided "AS IS" with no warranties, and confers no rights.
Alex Korchemniy
|
|
|
|
|
Ok, this is fairly simple, i need to find/make a text control
that behaves much like a dos box except, i cant use the console because i want to be able
to use graphics (images, controls, etc) also within this space... how would i do this?
any ideas much appriciated
_O
|
|
|
|
|
RichTextBox is the way to start. Then you need to find a way of getting the console part of it to work.
This posting is provided "AS IS" with no warranties, and confers no rights.
Alex Korchemniy
|
|
|
|
|