|
I have a textbox that I validate the value of. On the same form, are two buttons, OK, and Cancel which return DialogResults. In the validating event ii check to see if the textbox is empty, to stop empty values being inserted into the DB.
However, if I open the form and then want to cancel it (eg: close the form) because the validating event has been fired, I cannot click the Cancel button.
Anyone know a work around for this?
Regards,
Gareth.
(FKA gareth111)
|
|
|
|
|
Hi,
Validating is a bit problematic when using default events. As you noticed, you cannot move the focus from the control as long as it's not validated.
However, you can resolve this in many ways. One that comes first in mind is that you create a single validation method where you actually do the validations. This method can be called whenever you want that validation occurs and it can use error providers or such to indicate problem controls. If you return a boolean from this method indicating if validation was succesfull, you can use that boolean for example in OK button so that it doesn't perform it's actions if validation has failed. However, cancel button doesn't use the return value since it's going to cancel the whole operation. Make validation events to call this method, but nevermind the return value and never set cancel to false in validation event.
Hope this helps (and was understandable),
Mika
|
|
|
|
|
Mika,
I'd rather not do it that way, do you know of another work around?, or am I stuck with handling it my self?
Regards,
Gareth.
(FKA gareth111)
|
|
|
|
|
Sorry to say, but nothing comes in mind since if you set Cancel to true in validating-event, you cannot move focus away from the control.
Is there some particular reason why you don't want to use centralized validation?
Mika
|
|
|
|
|
|
Well, you could have Form1 register for Form2's OnClosed event. That way Form1 would be told when Form2 was closed.
My current favourite word is: Nipple!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
If you do not need Form1 to remain responsive then you should make Form2 modal ( start it with Form2.ShowDialog() ). Then when Form2 closes the code in Form1 picks up on the next line so you can re-enable the button then. Something like:
Form1.btn.Enabled = false;
Form2.ShowDialog();
Form1.btn.Enabled = true;
If you need Form1 to remain responsive, other than for the button that opens Form2, then you should make Form2 non-modal ( Form2.Show() ), have Form1 listen for Form2's Close event and re-enable the button when Form1 responds to Form2’s Close event.
modified on Tuesday, September 9, 2008 5:23 PM
|
|
|
|
|
The answers above are all quite helpful and here is the source code to help you:
In Form1 :
private void myButton_Click(object sender, EventArgs e)
{
changeMyButton(false);
Form2 frm2 = new Form2(this);
frm2.Show();
}
public void changeMyButton(bool state)
{
myButton.Enabled = state;
}
In Form2 :
Form1 form1Ref;
public Form2(Form1 fRef)
{
InitializeComponent();
form1Ref = fRef;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
form1Ref.changeMyButton(true);
}
Good luck
zafer
|
|
|
|
|
This is not a good way to do it - passing references of a form to the constructor of another - yuk!
Why not just suscribe to the second form's FormClosing event? Something like...
void myButton_Click(object sender, EventArgs e)
{
myButton.Enabled = false;
Form2 frm2 = new Form2();
frm2.FormClosing += new FormClosingEventHandler(frm2_FormClosing);
frm2.Show();
}
void frm2_FormClosing(object sender, FormClosingEventArgs e)
{
myButton.Enabled = true;
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Sorry I disagree, what do you mean by "not good"?
Are you aware that in form1 code you are writing code related with form2. What if you have form3, form4 etc.
Than your form1 gets larger and larger.
Passing the reference of the main form to other forms is a practical way of form communication.
|
|
|
|
|
Form2 has no purpose to know about form1 at all. It does nothing with it at all so the reference it pointless.
Form1 however neeeds to know when form2 is closing so it should listen for that event.
That's what events and delegates are designed for. If any object needs to tell any other objects about anything at all it should raise an event so any interested (subscribed) objects can react.
zafersavas wrote: What if you have form3, form4 etc.
This applies the other way too - what if you have multiple objects that need to know that form2 is closing? You'd need to pass every object in the constructor. How can you predetermine how many there will be or what types? It makes the constructor(s) virtually unmaintainable. By using events/delegates there is no limit to the number or type of objects that can have methods invoked from the object raising the event.
That's what OOP is all about!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Well, this topic seems going deeper
DaveyM69 wrote: what if you have multiple objects that need to know that form2 is closing? You'd need to pass every object in the constructor. How can you predetermine how many there will be or what types?
absolutely no, no and no, have a look at this:
Form1 : is the main form, Form2 and Form3 are declared here.<br />
Form2(Form1 frmRef);<br />
Form3(Form1 frmRef);
2 knows 1, 1 knows 3 so 2 can know 3 through 1.
Just pass Form1 as a constructor parameter and dont think the rest. As every form is aware of Form1 and Form1 is where other forms are declared, this means "everybody knows everybody". Form1 is something like a common node for all others. So as you see you dont have to pass other forms to the constructor.
Anyway, its a way up to you. No problem if it is efficient and causing any trouble.
Kind regards
zafer
|
|
|
|
|
zafersavas wrote: Well, this topic seems going deeper
Yeah, don't want to get into this too deep - this board probably isn't the right place, and don't want to hijack the thread! You say tomato, I say... etc etc.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Adding the event - thats cool.
Presumably you could also use the same method for any child form simply by adding the formclosing event. If you needed custom processing for a particular form use a switch on the sender name.
Oh joy, new toys to play with.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Mycroft Holmes wrote: Presumably you could also use the same method for any child form
You can do anything you like with it. Eg.You can respond to events from objects that are part of the child form too by either making them public (not recommended) or better still, in the event handler on the child, fire a new custom event that the other form can listen for. the possibilities are endless...
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
|
hi
my problem is OLE structure storage API parameters ,, functions like OpenStgStorage()
i want to learn who can i proramming it
i there any sites.......... books.......
THANx
Amani
|
|
|
|
|
Try this[^] article as a starting place.
|
|
|
|
|
Awful subject - thats why you got voted down (not by me I add).
PInvoke.net[^] is what you're looking for in regard to API programming.
I think the method you're after is actually StgOpenStorage[^]
|
|
|
|
|
I suspect she got voted down because she's asked the same question so many times over the last umpteen months. She doesn't seem to have moved on any.
|
|
|
|
|
That I've noticed, hence ignore the posts
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
Hi All
I have a light/siren device that is plugged into my computer.
I also have a program running on my machine.
If there is any problem or error on the program, this light/siren device should be automatically set on, the light comes on and the siren makes the siren noise, this is done to alert the administrator of a problem.
So I don't know if there is anyone here who has done something like that.
I just need pointers to the right direction on how set this light/siren on programatically in C#.
Your assistance will be greatly appreciated.
Thank You
MP
|
|
|
|
|
I assume there is some form of SDK with this device. If so, you'll need to use that.
|
|
|
|
|
First of all,
You said: I have a light/siren device that is plugged into my computer.
How is it connected to the computer? Is it RS-232, Parallel Port, USB etc...
You should know the interface between your equipment and the PC.
- If it is RS-232, probably there are serial commands which makes the the light ON/OFF. .NET framework includes serial port libraries so you can easily send and receive commands.
- If it is parallel port, you should set/reset necessary bits to make light ON/OFF. There is a popular, free library for parallel port : inpout32.dll
- If it is something else, you should be given some library to control the light. You should just call it from your application.
Good luck
zafer
|
|
|
|
|
khuzwayom wrote: If there is any problem or error on the program, this light/siren device should be automatically set on, the light comes on and the siren makes the siren noise, this is done to alert the administrator of a problem.
This can only end badly, seeing as you are a computer programmer dealing with visual studio
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my Blog
|
|
|
|