how to test if a selected index has just been change ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pascal

    #1

    how to test if a selected index has just been change ?

    Hello
    This is, below, a piece of code that I have a problem with:

    In the program (french description here
    http://www.scalpa.info/carre_install...aide_carre.htm) a number is
    generated in the interval given by the value of nudNumMin and nudNumMax.
    In the user interface can be created several "projects" appearing in a
    listbox, which each have their
    characteristics . By clicking on the project in the listbox, controls must
    display values for this project( selected at the moment it was build).
    If in a project carréA nudNumMax is 20 and nudNumMin is 15 and
    in the project carréB nudNumMax is 10 and nudNumMin is 5: When I pass from
    one to the other that triggers the warning message and corrects my data
    while it should not because min is much lower than max within the same
    Project ...

    So where and How can I test the values returned by numericupdown controls
    only within the same project but not when I change project clicking in the
    list box?
    THANKS FOR YOUR HELP
    Pascal
    --
    the code :
    private void SetMazeGroupCon trols()
    {
    _boInitilizing = true;

    // misc
    chbNewRow.Check ed = _currParam.NewR ow;
    nudMazeCount.Va lue = (decimal)_currP aram.Count;

    // generation
    nudMazeWidth.Va lue =
    (decimal)_currP aram.Generation Data.CarreWidth ;
    nudNumMin.Value = (decimal)_currP aram.Generation Data.NumberMin;
    nudNumMax.Value = (decimal)_currP aram.Generation Data.NumberMax;
    nudCellSize.Val ue = (decimal)_currP aram.ExerciseDa ta.CellSize;
    .......

    private void lbxMaze_Selecte dIndexChanged( object sender, EventArgs e )
    {
    int iSelected = lbxMaze.Selecte dIndex;

    if( iSelected >= 0 )
    {
    MazeItemData item = lbxMaze.Items[ iSelected ] as
    MazeItemData;
    _currParam = item;
    SetMazeGroupCon trols();
    if( Properties.Sett ings.Default.Ma rkCurrentMaze )
    ValueChanged( false );
    }

    HandleMazeListB uttons();
    }

    ........
    private void nudNumMin_Value Changed(object sender, EventArgs e)
    {

    if (nudNumMin.Valu e >= nudNumMax.Value )
    {
    MessageBox.Show ("La valeur minimale doit être inférieure à
    la valeur maximale.", "Valeur invalide ", MessageBoxButto ns.OK);

    nudNumMin.Value = nudNumMax.Value - 1;
    }

    _currParam.Gene rationData.Numb erMin = (int)nudNumMin. Value;
    ValueChanged(tr ue);
    }

    private void nudNumMax_Value Changed(object sender, EventArgs e)
    {
    if (nudNumMax.Valu e <= nudNumMin.Value )
    {
    MessageBox.Show ("La valeur maximale doit être supérieure à
    la valeur minimale.", "Valeur invalide ", MessageBoxButto ns.OK);

    nudNumMax.Value = nudNumMin.Value + 1;
    }
    _currParam.Gene rationData.Numb erMax = (int)nudNumMax. Value;
    ValueChanged(tr ue);
    }
    Ressource pédagogique SC@LPA.


  • Pascal

    #2
    Re: how to test if a selected index has just been change ?

    Hello and thanks for this quick response.

    I had thinking of "a flag?" able to "switch on or off" a piece of code : i
    will dig that and 'll try to find a piece of code that explain how to do
    that in C# that i am discovering this year.
    Do you know a place to read how to set a flag ?

    Thanks to inform me about usenet : if i understood after this "--" only
    signature must remain ?

    pascal

    Just add a flag to your class that you can set while updating the
    project based on the ListBox selection change. Set the flag before you
    adjust the numeric controls, and clear it after you're done. Then, in the
    numeric control event handlers, don't do any range checking if the flag is
    set.
    >
    By the way, next time you post code, you might consider not putting it in
    your signature (that is, following the specific Usenet-standard signature
    delimiter "-- "). Some newsreaders will format it differently, making it
    harder to read, and other newsreaders will strip it out altogether, making
    it impossible to read. Obviously, that may limit your audience somewhat
    (especially the latter problem :) ).
    >
    Pete
    --
    Ressource pédagogique SC@LPA.

    Zone d'échange entre tous les partenaires du projet : Espagne, France, Italie, Roumanie.

    Le blog du site de ressources pour l'école élémentaire: scalpa.info



    Comment

    • Peter Duniho

      #3
      Re: how to test if a selected index has just been change ?

      On Wed, 02 Jul 2008 01:02:34 -0700, Pascal <scalpaSansSpam @scalpa.info>
      wrote:
      Hello and thanks for this quick response.
      >
      I had thinking of "a flag?" able to "switch on or off" a piece of code :
      i will dig that and 'll try to find a piece of code that explain how to
      do that in C# that i am discovering this year.
      Do you know a place to read how to set a flag ?
      By "flag" I simply mean a variable of type "boolean". Sorry I was
      imprecise. The word "flag" is commonly used, but I admit that it would
      have been better to just use the type name.
      Thanks to inform me about usenet : if i understood after this "--" only
      signature must remain ?
      Almost. The space after the two hyphens is important. But yes, after "--
      " (note the space), only a signature should appear.

      Pete

      Comment

      • Pascal

        #4
        Is it a good flag ?

        hello Pete

        I did this without success.... don't know why... any idea ?
        some where at the beginning of the code boolean declaration :

        private bool flagCheckIcomeF romLstBx = false ;
        private string _strFile = string.Empty; // loaded file name
        (including full path
        private bool _boChanged = false; // any changes made on
        the current parameters
        private bool _boInitilizing = true; // true = initialization
        running, suppress redrawing
        //..... then if index of listbox changes i turn "on" the flag
        private void lbxMaze_Selecte dIndexChanged( object sender, EventArgs e )
        {
        int iSelected = lbxMaze.Selecte dIndex;
        bool flagCheckIcomeF romLstBx = true;

        if( iSelected >= 0 )
        {
        MazeItemData item = lbxMaze.Items[ iSelected ] as
        MazeItemData;
        _currParam = item;
        SetMazeGroupCon trols();
        if( Properties.Sett ings.Default.Ma rkCurrentMaze )
        ValueChanged( false );
        }

        HandleMazeListB uttons();
        }

        //.... then i test if I come from the listbox or not

        private void nudNumMin_Value Changed(object sender, EventArgs e)
        {
        if (flagCheckIcome FromLstBx == false)
        {
        if (nudNumMin.Valu e >= nudNumMax.Value )
        {
        MessageBox.Show ("La valeur minimale doit être inférieure
        à la valeur maximale.", "Valeur invalide ", MessageBoxButto ns.OK);
        nudNumMin.Value = nudNumMax.Value - 1;
        }

        _currParam.Gene rationData.Numb erMin = (int)nudNumMin. Value;
        ValueChanged(tr ue);
        }
        else
        {
        _currParam.Gene rationData.Numb erMin = (int)nudNumMin. Value;
        ValueChanged(tr ue);
        flagCheckIcomeF romLstBx = false;
        }
        }

        private void nudNumMax_Value Changed(object sender, EventArgs e)
        {
        if (flagCheckIcome FromLstBx == false)
        {
        if (nudNumMin.Valu e >= nudNumMax.Value )
        {
        MessageBox.Show ("La valeur minimale doit être inférieure
        à la valeur maximale.", "Valeur invalide ", MessageBoxButto ns.OK);

        nudNumMin.Value = nudNumMax.Value - 1;
        }

        _currParam.Gene rationData.Numb erMax = (int)nudNumMax. Value;
        ValueChanged(tr ue);
        }
        else
        {
        _currParam.Gene rationData.Numb erMax = (int)nudNumMax. Value;
        ValueChanged(tr ue);
        flagCheckIcomeF romLstBx = false;
        }
        }


        --
        Ressource pédagogique SC@LPA.

        Zone d'échange entre tous les partenaires du projet : Espagne, France, Italie, Roumanie.

        Le blog du site de ressources pour l'école élémentaire: scalpa.info


        Comment

        • Peter Duniho

          #5
          Re: Is it a good flag ?

          On Wed, 02 Jul 2008 23:51:23 -0700, Pascal <scalpaSansSpam @scalpa.info
          wrote:
          hello Pete
          >
          I did this without success.... don't know why... any idea ?
          some where at the beginning of the code boolean declaration :
          The basic problem is that you are redeclaring the flag as a local variable
          in the "lbxMaze_Select edIndexChanged( )" method, hiding the private class
          field. So the private class field is never actually changed.

          For what it's worth, I wouldn't write "flagCheckIcome FromLstBx == false".
          Just write "!flagCheckIcom eFromLstBx" instead.

          Also, I wouldn't actually set the flag in the
          "lbxMaze_Select edIndexChanged( )" method, because you don't call
          "SetMazeGroupCo ntrols()" unless there's actually a selection. When you
          wind up with nothing selected, you'll also wind up disabling your
          validation for the next time the user actually changes the control value
          (assuming they don't change the ListBox selection before that).

          Since it's specifically the method "SetMazeGroupCo ntrols()" that will
          cause the validation to occur, IMHO that's the method in which you should
          set the flag.

          Pete

          Comment

          • Pascal

            #6
            did it !

            Hello Pete

            It took me a lot of time to understand the way to do it, but now it works
            fine.
            Thanks a lot spending time to help me.

            here is the code perhaps it can help someone else !

            at the beginning of form1.cs
            #region variables

            private bool flagCheckIcomeF romLstBx = true;

            private void nudNumMin_Value Changed(object sender, EventArgs e)
            {
            if (flagCheckIcome FromLstBx == false)
            {
            if (nudNumMin.Valu e >= nudNumMax.Value )
            {
            MessageBox.Show ("La valeur minimale doit être inférieure
            à la valeur maximale.", "Valeur invalide ", MessageBoxButto ns.OK);
            nudNumMin.Value = nudNumMax.Value - 1;
            }

            _currParam.Gene rationData.Numb erMin = (int)nudNumMin. Value;
            ValueChanged(tr ue);
            }
            else
            {
            _currParam.Gene rationData.Numb erMin = (int)nudNumMin. Value;
            ValueChanged(tr ue);
            flagCheckIcomeF romLstBx = false;
            }
            }

            private void nudNumMax_Value Changed(object sender, EventArgs e)
            {
            if (flagCheckIcome FromLstBx == false)
            {
            if (nudNumMin.Valu e >= nudNumMax.Value )
            {
            MessageBox.Show ("La valeur minimale doit être inférieure
            à la valeur maximale.", "Valeur invalide ", MessageBoxButto ns.OK);

            nudNumMin.Value = nudNumMax.Value - 1;
            }

            _currParam.Gene rationData.Numb erMax = (int)nudNumMax. Value;
            ValueChanged(tr ue);
            }
            else
            {
            _currParam.Gene rationData.Numb erMax = (int)nudNumMax. Value;
            ValueChanged(tr ue);
            flagCheckIcomeF romLstBx = false;
            }
            }

            private void SetMazeGroupCon trols()
            {
            _boInitilizing = true;
            flagCheckIcomeF romLstBx = true;

            // misc
            chbNewRow.Check ed = _currParam.NewR ow;
            nudMazeCount.Va lue = (decimal)_currP aram.Count;

            // generation
            nudMazeWidth.Va lue =
            (decimal)_currP aram.Generation Data.CarreWidth ;
            nudNumMin.Value = (decimal)_currP aram.Generation Data.NumberMin;
            nudNumMax.Value = (decimal)_currP aram.Generation Data.NumberMax;
            nudCellSize.Val ue = (decimal)_currP aram.ExerciseDa ta.CellSize;

            // Exercise parameters
            chbExerciceShow Cible.Checked =
            _currParam.Exer ciseData.ShowCi ble;
            pnlExerciceCibl eColor.BackColo r =
            _currParam.Exer ciseData.CibleC olor;
            chbExerciseShow Numbers.Checked =
            _currParam.Exer ciseData.ShowNu mbers;
            pnlExerciseNumb erColor.BackCol or =
            _currParam.Exer ciseData.Number Color;
            chbExerciseShow Solution.Checke d =
            _currParam.Exer ciseData.ShowSo lution;
            pnlExerciseSolu tionColor.BackC olor =
            _currParam.Exer ciseData.Soluti onColor;
            chbExerciseShow Walls.Checked =
            _currParam.Exer ciseData.ShowWa lls;
            nudExerciseWall Width.Value =
            (decimal)_currP aram.ExerciseDa ta.WallWidth;

            // Solution parameters
            chbSolutionShow Cible.Checked =
            _currParam.Solu tionData.ShowCi ble;
            pnlSolutionCibl eColor.BackColo r =
            _currParam.Solu tionData.CibleC olor;
            chbSolutionShow Numbers.Checked =
            _currParam.Solu tionData.ShowNu mbers;
            pnlSolutionNumb erColor.BackCol or =
            _currParam.Solu tionData.Number Color;
            chbSolutionShow Solution.Checke d =
            _currParam.Solu tionData.ShowSo lution;
            pnlSolutionSolu tionColor.BackC olor =
            _currParam.Solu tionData.Soluti onColor;
            chbSolutionShow Walls.Checked =
            _currParam.Solu tionData.ShowWa lls;
            nudSolutionWall Width.Value =
            (decimal)_currP aram.SolutionDa ta.WallWidth;

            _boInitilizing = false;
            }

            All the code is from daniel marcel Camenzind who made a beautiful and handy
            soft that save me a lot of time in my job see here "labyrintha les"
            http://www.scalpa.info/new_logiciels.php and for fun and learning C# i try
            to clone it to do another soft "Carrés magiques".
            Thanks to both of you sharing your knowledge.
            pascal

            Comment

            Working...