
- VB NET USING ERRORPROVIDER ON MULTIPLE CONTROLS HOW TO
- VB NET USING ERRORPROVIDER ON MULTIPLE CONTROLS CODE
- VB NET USING ERRORPROVIDER ON MULTIPLE CONTROLS TRIAL
- VB NET USING ERRORPROVIDER ON MULTIPLE CONTROLS WINDOWS
MessageBox.Show(q.Count() > 0 ? "There are errors!" : "No errors!")
VB NET USING ERRORPROVIDER ON MULTIPLE CONTROLS CODE
Now our consuming code is a bit simpler because our list is defined as only the items with messages in the ErrorProvider: // Now we don't need the Lambda You’ll notice that this is the same logic just implemented as part of the LINQ statement. Where(c => !"".Equals(errorProvider1.GetError(c))) To do so, we will use yet another Extension Method called Where: var q = from c in () In our case, however, we know that this criteria is the only one we will need, so we have the option to embed our Lambda in the LINQ statement itself. This approach is especially handy if you may need to query the list in multiple ways. If the count is greater than 0, then we have errors, else we do not. MessageBox.Show(q.Count(c => !"".Equals(errorProvider1.GetError(c))) > 0 ? "There are errors!" : "No errors!") Īgain we are using a supplied Extension Method called Count() and passing it a Func defined by our Lambda Expression. I’m going to use our friend the Lambda Expression to find out if there are any Controls with Errors in the errorProvider: // Query as needed One way is to query the entire Sequence when we need it.
VB NET USING ERRORPROVIDER ON MULTIPLE CONTROLS HOW TO
In the meantime, now that we have a queryable object, we have several options for how to complete our task. In fact, this would be a good candidate for a ControlCollection Extension Method in its own right. Now that we have a Sequence instead of a Collection to work with, this could come in quite handy. Now we have a LINQ query object representing all the Controls on our Form. Here is the code that will transform our ControlCollection into a usable Sequence: var q = from c in ()

Microsoft was gracious enough to provide an IEnumerable extension method ( my favorite new feature) that will cast an IEnumerable to an IEnumerable.
VB NET USING ERRORPROVIDER ON MULTIPLE CONTROLS TRIAL
This led me to uncharted territory: how do I get an IEnumerable from an IEnumerable? Well, there was definitely some trial and error, and a lot of IntelliSense browsing, but I finally figured out an elegant solution. It soon dawned on me that ControlCollection does not qualify as a Sequence because it does not implement IEnumerable, but rather IEnumerable. If you read my recent LINQ to Objects post you may recall that LINQ does not work on Collections, but rather Sequences. Naturally, I reexamined the code and couldn’t find anything odd about it, but what did seem odd was that I knew this.Controls was a Collection, and LINQ is supposed to work on Collections, right? Wrong! Consider explicitly specifying the type of the range variable ‘c’. When I tried to compile this, I got an error I did not expect:Ĭould not find an implementation of the query pattern for source type ‘.ControlCollection’. The first step is to try and build a LINQ query of all the Controls on the Form: var q = from c in this.Controls Since it wasn’t as simple as I originally thought it would be, let’s walk through it in stages. Seems it should be pretty straightforward, and ultimately it is, but in this particular case it took a little trial and error to hack it out. In looking at this, it appears we have a good case for using LINQ: we have a Collection and we want to query that Collection for information about itself. If (!"".Equals(errorProvider1.GetError(c))) Here is a typical example of the loop construct: bool hasErrors = false My answer does not specifically address the issue the poster brought to the table, but the post got me to thinking: we should be able to use LINQ to solve this problem. If they do, then I use that information to warn the user or set some Form state so that it is apparent that an error exists. I like ErrorProvider and use it quite a bit, and I have done what the poster was trying to do many times: loop through the Controls on a Form and find out whether any of them have an active error message.
VB NET USING ERRORPROVIDER ON MULTIPLE CONTROLS WINDOWS
The code was using one of my favorite techniques for Windows Forms, the ErrorProvider.

While perusing the C# Corner forums today, I found this post about not being able to break out of a loop. Well, I have joined several, and have even posted a few times. You may recall that one of my New Year’s Resolutions was to find and participate in a.
