c# exception handling in textboxes - c#

I am trying to figure out a solution in C# to perform exception handling for multiple textboxes using windows forms.
The user can only enter one or two positive integers in these textboxes and if the user tries to enter more numbers or letters, a tooltip should appear with a warning message?
Thanks for the assistance!

For a case like this, I like to do what I call "passive Error Reporting". Rather then throwing exceptions, you take every value (usually strings). But display a message if it does not fit some criteria.
The simple approach is INotifyDataError. It allows you to show one error for each property (it is advantagenous to use a property or a string key in the backend storage).
I know there is a more complex brother that allows Multiple errors per Property/Key. But it is too long since I read it's name, so I can not remember it.

Related

startup text randomized it, it wouldnt let me post becuase of the name

aight so I'm making a WPF application and it has a loading window and on that loading window i want the text to be random every time the app is started any idea on how to do this?
Depending on what you mean by "Random" there could be multiple ways to do this:
Have a Hardcoded list of Loading texts in Application (or a resource like xml or database). Read a random line from this store.
Do a WebApi call to fetch the loading text (You need to create/find relevant WebApi).
Want Truly Random/runtine generated text? Ok - generate a random number in range: −2,147,483,648 to 2,147,483,647. Convert this to UTF-32. Keep appending more chars and/or spaces until a minimum string length is achieved. Use this as the Loading text.
If you are looking for generating a random Integer - Look at this

Ways to handle different data type restrictions in a "textbox" that come from SQL Server - C# Winforms

I am currently developing a software to integrate and be a nice front end UI to a database that doesn't have a front end UI. Part of this UI has a spot where there is a listbox on the left and on the right will be multiple sections of a drop down box on the left and a "textbox" on the right with an "and" and "or" radio button below it. It looks similar to the following linked picture.
So with that being said, one of the things I want to do is based on the column that is chosen on the left, the "textbox" will restrict/force reformat the data entered accordingly. So if it is a column of datetime formatting, then the textbox will only allow that kind of input. If the restriction is int[x], then the only input will be no more than the limitation of the integer, etc. etc. but not allow any letters to be inputted.
So this is where the real struggle comes in. I am struggling to figure out how to only allow specific input to the box based on the column chosen. Do I leave it as a text box and then use conditional statements that restrict the data input based on the formatting of the column? (Which this is how I am planning on currently programming it. However, this requires a lot of conditions and making sure to try and handle any and all possibilities out there.) Or is there a better way to handle the different types of SQL Server data types that will potentially be present? As in I create a box with the appropriate input restrictions and somehow dynamically swap them in/out based on the column selected at the time.
The biggest one that makes me question is the datetime formatting. I would love to use a date picker box (make sure I make it as idiot proof as I can). However, I am not sure if there would be an easy way to switch between a date picker box and any other boxes that could be necessary to have and have it all be seamless.
NOTE: All of the data will be stored so that if you go back to a column you already input data for, it will pull it back up.
Grateful for all of the feedback and input.
The way I read your question, you have solved the issue of determining the datatype of the SQL Server column. If that is correct, then all you would need to do is swap out controls based on the datatype of the column. Something like the following:
switch (columnType)
{
case int:
TextBoxInt.Visible = true;
TextBoxString.Visisble = false;
DateTimePicker.Visible = false;
break;
case string:
TextBoxString.Visible = true;
TextBoxInt.Visible = false;
DateTimePicker.Visible = false;
break;
case DateTime:
DateTimePicker.Visible = true;
TextBoxInt.Visible = false;
TextBoxString.Visible = false;
break;
}
Then you can handle limiting the input for each of the controls appropriately based on the datatype that it is for.
Maybe what you're looking for is to possibly handle the datatypes by letting sql do the work for you.
Sometimes it seems there's no way out of validating the user input with conditionals, but there are ways of making it simpler.
For many of problems handling the conditions you can probably use different parsing methods that throw exceptions. When the exceptions is thrown you can have it control the bool condition for submit. DateTime.Parse() is one of them.
For the date-time parsing problem you may be experiencing maybe this post to convert the textbox to a string and use DateTime.Parse(). DateBox Forced Formatting Logic.
Additonally, rather than a text box it may be useful to use a DateTimePicker object.

Create CMD-like arguments for SendKeys

I'm currently working on a Program, that presses buttons for me. I'm working on WPF but I already finished my design in XAML, now I need some C# code.
I have a TextBox that should handle all the SendKeys input. I want to extend it's functionality by providing some CMD-like arguments. The problem is, I don't know how. ;A; For example:
W{hold:500}{wait:100}{ENTER}
This is a example line that I'd enter in the textbox. I need 2 new functions, hold and wait.
The hold function presses and holds the previous key for the specified time (500 ms) and then releases the button.
The wait function waits the specified time (100ms).
I know I could somehow manage to create this function but it would end up being not user editable. That's why I need these arguments.
You're trying to 'parse' the text in the text box. The simplest way is to read each character in the text, one by one, and look for '{'. Once found, everything after that up until the '}' is the command. You can then do the same for that extracted command, splitting it at the ':' to get the parameters to the command. Everything not within a '{}' is then a literal key you send.
There are far more sophisticated ways of writing parsers, but for what it sounds like you are doing, the above would be a good first step to get you familiar with processing text.

Custom .Net library or existing library to handle sequence formatting?

I am thinking a library already exists for this, but I need allow my users to create a numbering format for their documents.
For example, let's say we have an RFI from and the user has a specific format the numbering sequence needs to be in. A typical RFI number looks like this for their system: R0000100. The next RFI in line would be R0000101.
Before I set out to creating a formatting engine for numbers such as these, does something already exist that can accommodate this?
Update:
I failed to save the edit to this question. Anyway, I also want to give the users the ability to create their own formats. So, I may have a form where they can input the format: R####### And also allow them to specify the starting integer: in the case 100. Also, I may want to allow them to specify how they want to increment. maybe only by 100s. So the next number may be R0000200. I know this may sound ridiculous, but you never know. That is why I asked if something like this already exists.
If you keep value and format separated, you won't need a library or such a thing.
The numbers would be simple, say, integers i, i.e. 100, 101, 102, that you manage/store however you see fit. The formatting part would simply be a matter of R + i.ToString("0000000"), or if you want to have the format as a string literal string.Format("R{0:0000000}", i).
I know, this might only be an example, but as your question stands, the formatting options, that .NET provides out of the box seem to suffice.
The incrementing of identity field values is most often handled in an RDBMS-style database. This comes with a few benefits, such as built-in concurrency handling. If you want to generate the values yourself, a simple class to get the last-issued value and increment by one would be very easy to create. Make it thread-safe so you don't get any duplicates or gaps and you'll be good to go.

error provider is not blinking uniformly

I have an error provider providing error for 4 controls..
when I set all the four errors, only two of them blink together at a time and all four settle down after certain time..
even if I set two errors, both blink alternatively..
but I want all of them blink together...How can I do this? (I don't prefer using more than one errorProvider)
You've hinted to an (IMO) acceptable solution, with your last statement: use 2 error providers, one dedicated to blinking exactly one control at a time (the latest one with an invalid input, or the one you'd expect to be corrected ASAP, or whatever criteria you deem most important), and a second one that "silently" displays the icon on all controls with invalid input.

Categories