1

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.

enter image description here

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

1

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.

Jamin
  • 1,362
  • 8
  • 22
  • Ironically enough, that post you linked to is my post I did earlier. Now that I have that information, I can do the formatting there. The problem here really is I am struggling to figure out how to handle the different types of data types and the fields the potential input will be typed in. As stated before, dio I leave the box a textbox and just adjust the restrictions accordingly or is there an easier way to handle each different type of data type? As in different boxes that would handle the restrictions for me. No point in reinventing the wheel if something is there I can use. – Ryan Wakefield Dec 13 '17 at 10:15
  • @RyanWakefield Edit: Although you might not be able to escape doing some conditional cases for the sql part. You may find a lot of the DateTime parsing has already been done online in various forms. For example, there's a DateTimePicker Class https://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker(v=vs.110).aspx . This video good example: https://www.youtube.com/watch?v=Yb7R7Dr2DBk . Just look for alternatives, google search DateTime Text Box Gui Windows Form and you will find them. – Jamin Dec 13 '17 at 18:22
  • Thanks for the links. I think between both you and David Fletcher you both answered my question together. – Ryan Wakefield Dec 13 '17 at 21:29
1

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.

David Fletcher
  • 305
  • 1
  • 7
  • Yes, i already can get the respective datatyoes. And this is the kind of logic I want to do. However, I am not sure how to do the swapping of the two boxes. I am used to JAVA and having to code the size and location and everything in the code which made swapping easy. With C#, I am little lost on that. So that is hill #1. But as I said to Jamin is there any easier way to do this? Are there already boxes that I can use that will do all of the formatting and restrictions for me? Trying to not do a ton of coding when I really don't need to a d can use built in methods to the system already. – Ryan Wakefield Dec 13 '17 at 10:19
  • It is fairly simple in .NET to do the swapping. In the development environment, just place the controls on the form, and since you only want one visible at a time, they can be moved to overlap one another. As far as what controls, there are many choices. For numbers, you could use a numeric up/down control or a masked edit control, or a textbox. Each option has it's pro's and con's. Ironically, the DateTime datatype is probably the easiest choice with the date time picker. If you don't want to do a lot of coding, I would suggest either the up/down control or the masked edit for numbers. – David Fletcher Dec 13 '17 at 16:17
  • So are you saying I create all of the boxes and then just overlay them on each other? And then apply all of the specific checks and balances to them? I guess I could see how that could work. I will need to try and find a possible solution which will limit the amount of box types I will be creating. – Ryan Wakefield Dec 13 '17 at 21:27
  • Yep, should be simple to do, and require minimal coding. – David Fletcher Dec 14 '17 at 01:47