|
Ah! So you have the Machine That Goes Ping[^]!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Ni!
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
my first thought also.
|
|
|
|
|
Neeeow...wum...ping!
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Impossible to answer. You haven't specified what the "device" is, so it's impossible for ANYONE to tell you how to "connect it with C#". You've left out every possible detail you could to describe the problem.
Asking questions is a skill[^] that you seriously need to work on.
|
|
|
|
|
Member 14901982 wrote: How to connect the device with the C # language As specified in the documentation; if you want to write a device-driver, then the answer is no.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Have you tried switching it off and on again?
|
|
|
|
|
I created a UserControl with 5 buttons: New, Edit, Delete, Save, Cancel. All buttons have functions associated to them, programable by the user. Normally the 3 first buttons are enabled and the 2 other are disabled. When the user clicks on New or Edit, the 3 first buttons become disabled and the 2 others, enabled. When the user clicks on Save, or Cancel, it returns to the original state. It's working fine, but there is an issue.
In an application, when the user clicks on the Save button, the program must first verify if the fields are all filled. If yes, OK. But if not, the function should do nothing. So, what I need is a way of, in the main program, test the fields and pass this information to the control. But I don't know how. I tried to create a funtion in the control type virtual and in the main program I tried to create the same function with override, but it didn't work. Can anyone help me?
Thanks.
Ismael
|
|
|
|
|
Create a public event inside the control that gets raised when the user clicks the Save button. And make the event handler take the fields that must be verified.
Then the event handler inside the main program can return true or false, which will tell the event whether it should proceed with saving the information.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Hi, Richard.
In my control I have these events:
public event EventHandler ClickNovo;
public event EventHandler ClickEditar;
public event EventHandler ClickApagar;
public event EventHandler ClickSalvar;
public event EventHandler ClickCancelar;
Do you sugest that I create another event? Could you give me an example?
Thanks.
|
|
|
|
|
Some of this depends on your architecture, and whether it's Windows Forms or WPF / UWP; each use different patterns for validation.
If you only check validity on "save", you're doing "record-level" validation (easy) versus field-level validation (harder, due to cross-referencing and handling focus).
The "easy" way is to use a "data object" as the "view model" / transport between the "view" (form / window) and the "model" (data access layer / repository). This data object is used to pass data to the user control (to initialize the view / form / window) and to retrieve the "record" from the UC to be saved (add or update).
You may bind to the view model directly or copy the view data to it when saving. You then validate the data object and display the first error or save. If you have different "validation routines", you can pass a delegate (at initialize / load time) to the user control that accepts the data object and returns a status / error (if it's not feasible to have actually validation code in the user control.)
In the save "click" event, disable save (to avoid re-entrancy), copy the view / UC data to the view model (if not binding directly), validate, then save if OK or return an error. Reset buttons accordingly.
This pattern works for "Add" and "Update".
Deletes usually require some thinking about what to "display" after a delete and any further operations (like "add back in" or revert).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Hi, Gerry.
My idea was to create a validation function in the main program and when the user clicks on the Save buttons, before anything it calls that validation function. Do you know how to do this?
Thanks.
|
|
|
|
|
"Save" is form-level function; primitive controls and UC's are bit players to the form. In the Save / button event (on the form), retrieve the "data object" (view model) which consolidates all the UI / form's input, validate it, then save the contents or display an error.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
When reading this discussion my actual question is :
why a UserControl ? For me a UC is an object which is used more than one time (in a different context).
Basicly (so my opinion) we have a Data-Input object (Form) - so when I see it in OOP-context the question (for me) must be : which is my main-object and which are the sub-objects with which functionality ?
This is also (or primary) a reply to the OP ...
|
|
|
|
|
Ralf.
I'm developing a system with many forms and many of them use these buttons. And these forms have different fields in different amouunts.
|
|
|
|
|
Without seeing his "form", I can make few assumptions about the merits of his architecture.
I have a "user control" that presents "books", complete with tables of contents, incremental searching, etc. This UC itself contains more UC's: virtual keyboard controls (single and split), content sizing sliders, navigators, etc.
I'm trying to describe, in general terms, my "model" for "model <-> view"; UC's are fat controls.
In "data object" terms, I sometimes put a public interface on the UC, and pass the whole thing around. "Live" if you will. All depends on how you build it ... and I "love" code-behind. My apps are also in production.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
modified 30-Jul-20 13:35pm.
|
|
|
|
|
Adding to what Richard said:
Events - C# Programming Guide | Microsoft Docs
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I suggest you handle field validation inside the UserControl, and only enable the 'Save Button when all fields have appropriate values ... assuming the validation code can be located inside.
There are several strategies you can use including using the built-in validation facilities in WinForms [^], [^]. A valuable discussion here: [^].
A simple strategy is to define Event Handlers for all the input fields for 'Enter and 'Leave, and check for values. Use a boolean flag to keep track of whether all fields are valid.
For an example of a complex full-featured validator see CP article: [^]
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
modified 29-Jul-20 10:31am.
|
|
|
|
|
I would do it like this :
Outside your UserControl you have to check if all fields are filled (and perhaps if all information is valid) - because the Information is outside the UserControl.
Your UserControl gets a Property which give this (boolean) Information to it.
This Check could be done each time one of those fields is changed ... or with an Event which comes from the UserControl. But I prefer the 1st ...
|
|
|
|
|
Hi, Ralf. What to do so when the user clicks on the Save buttons it first verify the fields? I mean: the verification must ocur just when the user clicks on the Save buttons. You know how to do this?
Thanks.
|
|
|
|
|
Hi ...
in my opinion it would be not so good to generally allow the using of the Save-Button if the state of the data-field is not checked. In my applications I prefered allways the way of "check input when changes" and give a valid-information (and allow further action).
If you want to do it the other way round you should give an event to "outside" of your UserControl to do the check and the Check itself has to start (when all data is valid) perhaps a public method inside your UserControl which does the action (or raise another Event). What should happen if the Check isn't valid ? Who should give the Message to the User - the UserControl ? Or a Messagebox from the Ckeck-method ?
You see : there are serverall ways to do this ...
|
|
|
|
|
Hi, Ralf.
I used your idea and it's working fine. The only (minor) problem is that the system can't show the user a message to tell him what to correct. Anyway, as I said, is a minor problem. If you have any sugestion about this, please let me know. And thank you for your help.
|
|
|
|
|
Perhaps I have ...
Give me some more details about your project ...
Basicly the Textbox (or whereever you do the input) "knows" what kind of data is required. Here you can decide directly what is wrong ...
But perhaps you write some more ...
|
|
|
|
|
Hi, Ralf.
My system has a main form which is used as an MDI Parent. From it the user can access many other screens to control the system. Some of these screens make the registration of clients, suppliers, banks and others. In these registration screens, there are fields connected to columns in tables. When fields are mandatory, the system must verify what was written there. That’s my problem. At this moment this verification is working well, but it does not warn the user about a wrong entry or an empty field which should be filled. My User Control now has the following code:
private void SalvarButton_Click(object sender, EventArgs e)
{
if (PermSalvar)
{
ClickSalvar?.Invoke(this, e);
EmEdicao = false;
}
}
I created a variable (PermSalvar) which indicates whether the data can be saved. In the main program there is a routine named ValidaCampos which is called in the leave moment of the fields of interest, as you can see below.
private void ValidaCampos()
{
if (tb_Nome.Text == "")
botoes1.PermSalvar = false;
else
botoes1.PermSalvar = true;
if ((tb_Id.Text == "") || (!int.TryParse(tb_Id.Text, out int cp)))
botoes1.PermSalvar = false;
else
botoes1.PermSalvar = true;
}
private void tb_Nome_Leave(object sender, EventArgs e)
{
ValidaCampos();
}
private void tb_Id_Leave(object sender, EventArgs e)
{
ValidaCampos();
}
So, if you could give me any advice, it would be very welcome.
Thank you.
|
|
|
|
|
I question the validity of logic that flags an error (and continues) and then turns it off under another condition further on.
"Initialize" once.
if (tb_Nome.Text == "")
botoes1.PermSalvar = false;
else
botoes1.PermSalvar = true;
if ((tb_Id.Text == "") || (!int.TryParse(tb_Id.Text, out int cp)))
botoes1.PermSalvar = false;
else
botoes1.PermSalvar = true;
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|