WPF: Validation of multiple textboxes - c#

Here's the situation:
I've got 3 textboxes where one put First Name, Last Name and Age respectively. Label with big red "!" shall be only visible when:
Any of the textboxes has no content OR Age (third textbox) is a negative number OR Age is bigger than int.MaxValue.
I already got two validation rules (one for first/last name and another for age). What do I do now? Do I need some kind of converter that checks all three input fields and returns true/false? How to implement this in XAML?

You can use IDataErrorInfo to validate the fields - see this article which explains how to use it. You'll just need to adapt it to have one field displaying errors instead of individual ones for all three.

Related

Calculating a range of User Inputed values (Unspecified amount) in a textbox created grid

In this assignment I have to create a spreadsheet like excel without importing a library. Now So far I created a grid of 26x26 textboxes (USING CODE), created the indexed rows and alphabet indexed columns. (All of this using a class named Cellfields as a template)
Gave the names (A1,A2,A3 etc) to each cell. and created a formula bar up top.
then I created an if statement that if what the user writes.SubString(0) == to "=" (Meaning the start of a formula),
it will then go to a switch to read the other input the user entered, meaning if user enters =Sum(A1:A3) it will calculate the sum of cells A1,A2,A3.
Now since the calculated cells required are unspecified, I am unable to find a solution to how to calculate a range of values online, as all you can find online are how to import libraries.
I Did try a few things but were immediately removed. I am trying to create a method for it however, have been staring at laptop for the past 3 days.
So you already have a matrix of 26x26 Textboxes, I assume they would be arranged in a Grid, say the name of your Grid is "gridCells".
Now, Users will click on a Cell (actually textbox) and then go to the Formula Bar and type a formula. So On GotFocus of the textboxes, let the textbox remain "Selected", in the sense keep the reference at form level also may be Highlight the Border to show that this is the currently selected cell. This will help you know which Textbox to apply the formula.
Now, when someone types "=Sum(A1:A3)" in the Formula Bar, you would parse the formula, identify it is a "Sum" operation and find out the names of the Textboxes A1, A2 & A3.
From your post looks like you are done this far. Now write a function as below (Not bothering about performance etc. right now).
private String GetValueOfCell(String cellName)
{
foreach (var control in gridCells.Children.OfType<TextBox>())
if (control.Name.Equals(cellName))
return control.Text;
return "";
}
Call this function for all 3 Textboxes, like this:
String value = GetValueOfCell("A1");
And if the value is not empty and can be parsed as int/double, keep doing the Sum and finally store the value in the Text property of our highlighted Textbox.
Hint: You might also want to Save the Formula on the highlighted textbox, because this value goes into the Text property you can use the Tag property to hold the formula.

I need a multi-column control that only displays one column in c#

In my project I need a control that allows me to enter 2 columns.
First column is an Id Number
Second column is some Text.
Example...
row 1 Id = 1 Text = Day Shift
row 2 Id = 2 Text = Night Shift
But I only want to display the Text Values and then have the user select either Day Shift or Night Shift.
The program can then just lookup the corresponding value (1 or 2) for whatever text they chose.
- In Microsoft Access I would just have used a ComboBox and hidden the first column.
I cannot find anything in Visual Studio 2017 to put on my Windows Form that easily does this.
I want to set it all up at design time and the closest I have come so far is by using a LISTVIEW control using a display type of LIST (I don't want column headers either.
It seems to do what I want except that it always wants me to leave a blank space below my lines of text - presumably for a horizontal scrollbar even though I set it to False in the properties and it does not actually display a scroll bar.
If I resize the ListView control to just be big enough for my two rows of text it tries to display the 2nd row next to the 1st row and still leaves a blank space below the rows.
See the below images, assuming I uploaded them properly, I am totally new to asking questions here.
Is there a way I can achieve this - should I be using a different control?
The closest I came to what I need is the top image
[![enter image description here][1]][1]
Thanks and I hope it all makes sense.
In reply to Harry I added the below...
After adding the ListView control to my form I then clicked on the little arrow selector (in the top right of the ListView control).to bring up the collections list popup box.
It is there I set the view to be ‘List’ so I don’t have to have column header, then I clicked on EDIT ITEMS to get another pop-up screen…Click the [Add] button to add items in the rows.
Then I changed the Text to ‘Day Shift’ for the first member (and ‘Night Shift’ for the second member).
To add values in the rows for the 2nd column I then clicked the SUBITEMS (collection) box in properties to get the next popup…
I then clicked the Add button and created a new Text item (value 1)
and repeated this for the Night Shift member but gave it a value of 2.
These values are to be the actual ID values (in the 2nd column).
Note…
I did create column Headers but setting the View to List means I don’t get them displayed (and don’t want them displayed).
but I did notice that changing the View to List also removes the second column (Id) from the ListView display.
As I don’t need the Id values (1 & 2) displayed I am not going to worry about what happened to them disappearing in the display. All I did, though, was just changed the View from Details to List.
Sorry If I’m misunderstanding you but at this point there is no actual code I am creating, no doubt Visual Studio 2017 does that itself but I am not looking at that. Is that what you needed to see?
Hope this makes better sense though, I did try and include images but the system will not let me do that until my reputation points are higher, sorry.
Thanks

Custom Grouping with Telerik Grid for MVC

I wish to create a Telerik grid which can be grouped by different fields in the table, but I'm running into quite a problem - Telerik only allows me to designate a property to group by, and then that's it.
What I want to do is to partly be able to designate the title of the group. Instead of "Current Index: 9" I might want to remove the property name (Current Index) and replace the number by getting the value for it in a dictionary.
Also, say I want to sort by a date - Then I'll be wanting to sort only by year, month and day, and not by the full extent of the datetime down to the very last millisecond (results in separate groups for every object).
Is there any way to achieve something like this at all?
Thanks.
I solved my conumdrum by using ClientTemplates (Ajax binding) and custom properties in my viewmodel.

Dynamic DataGrid columns in WPF DataGrid based on the underlying set of data (and their type)

I've got kind of a conceptual question. I am in the process of wrapping some statistics classes I wrote into WPF.
For that I have two DataGrid(-Views, currently in WinForms). In one DataGrid each row represents a column in the other. There I can set-up different variables (as in mathematical/statistical variables) with fields like "Header", "DataType", "ValidationBehaviour", "DisplayType". There I can also set-up how it should be displayed. Some Columns can automatically be set to ComboBoxColumns, some TextBoxColumns, and so on and so forth.
So, now once I've set-up these Columns I can go to the other grid and enter my data. I may, for instance, have generated (in grid 1) one Column called "Annual Gross Salary" with input of numerical values. Another Column called "Education" with "0=NoEducation", "1=College Level", "3=Universitary" etc. These labels are displayed as text in the combobox and my statistics engine behind then selects the respective value (0-3) for calculations (i.e. ordinal, nominal variables).
Sooo. In WinForms I could basically generate all the columns by hand in code and then add my data in the respective cells/rows. Now in WPF I thought that must be easy to realise. However, yesterday I got started with ICustomPropertyDescriptor which (maybe I was too thick) didn't give me the results I was looking for.
Basically, I just need to be able to dynamically generate columns (and rows) with different Layout, Controls (ComboBox, simple Input, DateTimes) based on the data that I have. But I don't really know how to go about it?
So here in summary:
DataGrid 1
Purpose is to display columns that have been specified in DataGrid 2
In rows, the user can add any kind of data in the rows below the columns that is allowed as to the columns specifications
DataGrid 2
Each row in this grid represents a column in DataGrid 1
Contains fields like Name/Header, DataType, Validation Behaviour, Default Value, Data Formatting, etc.
Also contains a function to be able to set-up how it should be displayed. The user can select from, for instance, ComboBoxColumn (and also add the available options), DateTime, normal TextBox, CheckBox etc.
After finishing adding a row it will automatically appear as a new column in DataGrid 1
I'd appreciate any kind of pointer into the right direction. Thanks very, very much in advance! :)
Look up DataTemplates. They do exactly this. The UI is determined by the related type.
Here is an MSDN article...

How to check if either none or all textboxes in an ASP.NET page have text entered?

I am creating an invoice application and in the section where the user can enter line items, some of the fields will include: Quantity, Price, Extended Price, Description, etc.
If the user enters something into any of the fields, the corresponding fields are required, but if no data is entered into any field, then nothing is required in the corresponding fields. What is an efficient and clean way of doing this without doing a bunch of if statements?
I guess this is like doing validation if only one textbox has text.
You could try something like this in the ServerValidate event of a CustomValidator.
// test for all textboxes having some text in them...
e.IsValid = (from TextBox c in this.Controls
where c is TextBox
select c).All(tb => !string.IsNullOrEmpty(tb.Text));
That might need some tinkering to get right - but you get the idea. Change accordingly to test for all textboxes being blank.
You could also use jQuery for the client side validation in your CustomValidator
function clientValidate(sender, e) {
// get array of textboxes with a common css class
var textBoxes = $("#SomeCssSelector input").val();
// loop here to test for having text or no text.
e.IsValid = ... ;
}
You could create a TextBox array and store references to all related text boxes in the array. When ever you need to you could then iterate over the array in a for loop looking for the first item with a value. As soon as you find one with a value you know all the other items in that array also need values.
Additionally, if you have other groups of related text boxes you could create additional arrays to help keep them grouped.
Several ways if this is ASP.NET.
Use field validators that are available in ASP.NET.
Use JavaScript for Windows Forms.
For Windows Forms, also use control validators, or you could use a simple function where you pass a control array. If any item in the array is filled then it requires all items to be filled by returning which fields are not filled, etc.

Categories