Click here to Skip to main content
15,886,798 members
Home / Discussions / C#
   

C#

 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 6:18
Damian Bz27-Dec-17 6:18 
GeneralRe: C # + Regex help (for a newbie) Pin
OriginalGriff27-Dec-17 6:26
mveOriginalGriff27-Dec-17 6:26 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 6:29
Damian Bz27-Dec-17 6:29 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 7:37
Damian Bz27-Dec-17 7:37 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 9:15
Damian Bz27-Dec-17 9:15 
GeneralRe: C # + Regex help (for a newbie) Pin
Richard MacCutchan27-Dec-17 21:56
mveRichard MacCutchan27-Dec-17 21:56 
GeneralRe: C # + Regex help (for a newbie) Pin
OriginalGriff28-Dec-17 1:51
mveOriginalGriff28-Dec-17 1:51 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz28-Dec-17 6:59
Damian Bz28-Dec-17 6:59 
Well .. OK, I may have over reacted !, but I WAS very miffed that my code was seemingly trashed.
I spent a good few hours last night piecing it back together and rectifying all of the issues.
Visual Studio is really not helpful when it says that your code is all fluffed, and to REALLY help you i'm going to block you from seeing your form layout (thanks for that!)

Anyway, I got it back to where I was yesterday, then removed all the RegEx stuff and lots of if - else's to do the validation.
I wasn't really up to figuring out the tryparse millarky last night, but I'm personally pretty please with what I have come up with.
It may not be the right way of doing it, and i'm sure you guys\ladies could do it in half the code, but nethermind !
Here's a snippet of just one of the textbox event functions:
C#
private void totalsize_TextChanged(object sender, EventArgs e)
{
    int ts;
    if (string.IsNullOrEmpty(totalsize.Text))
        // do nothing
        ;
    else
    {
        if ((totalsize.Text.All(chr => char.IsLetter(chr))) | (totalsize.Text.Contains(".")))
        {
            MessageBox.Show("Please enter only whole numbers between 1 and 500");
            totalsize.Clear();
            totalsize.Focus();
        }
        else
        {
            ts = Convert.ToInt32(totalsize.Text);

            if (!(ts >= 1 && ts <= 500))
            {
                MessageBox.Show("Please enter only whole numbers between 1 and 500");
                totalsize.Clear();
                totalsize.Focus();
            }
        }

    }
}

private void totalsize_Leave(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(totalsize.Text) && !string.IsNullOrEmpty(shortfillsize.Text))
        nicshotsize.Text = (Convert.ToInt32(totalsize.Text) - Convert.ToInt32(shortfillsize.Text)).ToString();
    if (!string.IsNullOrEmpty(totalsize.Text))
        totalsizenum = Convert.ToDouble(totalsize.Text);

}


The first <if> statement //do nothing allows the user to move out of the textbox without causing an error. (there are other buttons that display help text etc. that the user may want to explore before typing in any numbers)

The first <else> <if> block checks to make sure that no alphabet or period character is entered (the first two textboxes need to be whole numbers only)
As soon as an illegal character is entered, the message box pops up with a warning.

The next <else> block checks that the number entered is in the right range.
That's the end of the TextChanged event.

Once the user moves out this Textbox the _Leave event kicks in.
This then checks if this Textbox + the next Textbox has a value entered, and if so does a very basic deduct one from the other and display the result in a read only Textbox.

The second Textbox has almost the same code.
The idea of this is that this way it allows the user to enter in the values in any order they feel like.

This is probably the simplest set of event functions, there are others that auto fill other Textboxes, for instance when typing in a percentage value in one box, another box is showing the reverse percentage, again these boxes can be used in any order, whatever goes in one fills the other.

The Calculate button then does a final check on all of the Textboxes to make sure they are all filled.
I'm about to start writing extra code to highlight any boxes that are missing data.

On the final calculations, I convert most of the Textbox values to double's, create temp variables that match them, do the calcs on these, then on the actual Textboxes I'm using this:
C#
Textbox.Text = Math.Round(Textboxtmp, 2).ToString();

And all is well in the world ! well apart from the dog who wishes I was paying him more attention ! Smile | :)

And I've also installed DPack tools that has a handy feature of backing up the project automatically and manually !!

So I'm back on track, and ready to keep on learning.
Sorry for throwing my toy out of the pram earlier Smile | :)
Cheers
Damian
AnswerRe: C # + Regex help (for a newbie) Pin
Gerry Schmitz28-Dec-17 8:17
mveGerry Schmitz28-Dec-17 8:17 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz28-Dec-17 23:21
Damian Bz28-Dec-17 23:21 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz29-Dec-17 1:48
Damian Bz29-Dec-17 1:48 
GeneralRe: C # + Regex help (for a newbie) Pin
Gerry Schmitz29-Dec-17 6:03
mveGerry Schmitz29-Dec-17 6:03 
QuestionSet datagrid cell value individually and dynamically Pin
Hervend26-Dec-17 22:17
Hervend26-Dec-17 22:17 
AnswerRe: Set datagrid cell value individually and dynamically Pin
Richard MacCutchan26-Dec-17 22:37
mveRichard MacCutchan26-Dec-17 22:37 
GeneralRe: Set datagrid cell value individually and dynamically Pin
Hervend26-Dec-17 22:52
Hervend26-Dec-17 22:52 
GeneralRe: Set datagrid cell value individually and dynamically Pin
Pete O'Hanlon26-Dec-17 22:56
mvePete O'Hanlon26-Dec-17 22:56 
GeneralRe: Set datagrid cell value individually and dynamically Pin
Hervend26-Dec-17 23:37
Hervend26-Dec-17 23:37 
GeneralRe: Set datagrid cell value individually and dynamically Pin
Richard MacCutchan26-Dec-17 23:29
mveRichard MacCutchan26-Dec-17 23:29 
GeneralRe: Set datagrid cell value individually and dynamically Pin
Hervend26-Dec-17 23:35
Hervend26-Dec-17 23:35 
QuestionParentheses vs curly brackets Pin
User9874326-Dec-17 12:17
professionalUser9874326-Dec-17 12:17 
AnswerRe: Parentheses vs curly brackets Pin
Gerry Schmitz26-Dec-17 12:36
mveGerry Schmitz26-Dec-17 12:36 
GeneralRe: Parentheses vs curly brackets Pin
User9874326-Dec-17 12:54
professionalUser9874326-Dec-17 12:54 
GeneralRe: Parentheses vs curly brackets Pin
Gerry Schmitz26-Dec-17 12:57
mveGerry Schmitz26-Dec-17 12:57 
GeneralRe: Parentheses vs curly brackets Pin
User9874326-Dec-17 15:00
professionalUser9874326-Dec-17 15:00 
GeneralRe: Parentheses vs curly brackets Pin
BillWoodruff26-Dec-17 15:36
professionalBillWoodruff26-Dec-17 15:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.