By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,419 Members | 1,394 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,419 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 SetMazeGroupControls()
{
_boInitilizing = true;

// misc
chbNewRow.Checked = _currParam.NewRow;
nudMazeCount.Value = (decimal)_currParam.Count;

// generation
nudMazeWidth.Value =
(decimal)_currParam.GenerationData.CarreWidth;
nudNumMin.Value = (decimal)_currParam.GenerationData.NumberMin;
nudNumMax.Value = (decimal)_currParam.GenerationData.NumberMax;
nudCellSize.Value = (decimal)_currParam.ExerciseData.CellSize;
.......

private void lbxMaze_SelectedIndexChanged( object sender, EventArgs e )
{
int iSelected = lbxMaze.SelectedIndex;

if( iSelected >= 0 )
{
MazeItemData item = lbxMaze.Items[ iSelected ] as
MazeItemData;
_currParam = item;
SetMazeGroupControls();
if( Properties.Settings.Default.MarkCurrentMaze )
ValueChanged( false );
}

HandleMazeListButtons();
}

........
private void nudNumMin_ValueChanged(object sender, EventArgs e)
{

if (nudNumMin.Value >= nudNumMax.Value)
{
MessageBox.Show("La valeur minimale doit Ítre infťrieure ŗ
la valeur maximale.", "Valeur invalide ", MessageBoxButtons.OK);

nudNumMin.Value = nudNumMax.Value - 1;
}

_currParam.GenerationData.NumberMin = (int)nudNumMin.Value;
ValueChanged(true);
}

private void nudNumMax_ValueChanged(object sender, EventArgs e)
{
if (nudNumMax.Value <= nudNumMin.Value)
{
MessageBox.Show("La valeur maximale doit Ítre supťrieure ŗ
la valeur minimale.", "Valeur invalide ", MessageBoxButtons.OK);

nudNumMax.Value = nudNumMin.Value + 1;
}
_currParam.GenerationData.NumberMax = (int)nudNumMax.Value;
ValueChanged(true);
}
http://www.scalpa.info

Jul 2 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
--
http://www.scalpa.info
http://scalpa98.blogspot.com/
http://scalpa-production.blogspot.com/
Jul 2 '08 #2

P: n/a
On Wed, 02 Jul 2008 01:02:34 -0700, Pascal <sc************@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
Jul 2 '08 #3

P: n/a
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 flagCheckIcomeFromLstBx = 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_SelectedIndexChanged( object sender, EventArgs e )
{
int iSelected = lbxMaze.SelectedIndex;
bool flagCheckIcomeFromLstBx = true;

if( iSelected >= 0 )
{
MazeItemData item = lbxMaze.Items[ iSelected ] as
MazeItemData;
_currParam = item;
SetMazeGroupControls();
if( Properties.Settings.Default.MarkCurrentMaze )
ValueChanged( false );
}

HandleMazeListButtons();
}

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

private void nudNumMin_ValueChanged(object sender, EventArgs e)
{
if (flagCheckIcomeFromLstBx == false)
{
if (nudNumMin.Value >= nudNumMax.Value)
{
MessageBox.Show("La valeur minimale doit être inférieure
√* la valeur maximale.", "Valeur invalide ", MessageBoxButtons.OK);
nudNumMin.Value = nudNumMax.Value - 1;
}

_currParam.GenerationData.NumberMin = (int)nudNumMin.Value;
ValueChanged(true);
}
else
{
_currParam.GenerationData.NumberMin = (int)nudNumMin.Value;
ValueChanged(true);
flagCheckIcomeFromLstBx = false;
}
}

private void nudNumMax_ValueChanged(object sender, EventArgs e)
{
if (flagCheckIcomeFromLstBx == false)
{
if (nudNumMin.Value >= nudNumMax.Value)
{
MessageBox.Show("La valeur minimale doit être inférieure
√* la valeur maximale.", "Valeur invalide ", MessageBoxButtons.OK);

nudNumMin.Value = nudNumMax.Value - 1;
}

_currParam.GenerationData.NumberMax = (int)nudNumMax.Value;
ValueChanged(true);
}
else
{
_currParam.GenerationData.NumberMax = (int)nudNumMax.Value;
ValueChanged(true);
flagCheckIcomeFromLstBx = false;
}
}
--
http://www.scalpa.info
http://scalpa98.blogspot.com/
http://scalpa-production.blogspot.com/

Jul 3 '08 #4

P: n/a
On Wed, 02 Jul 2008 23:51:23 -0700, Pascal <sc************@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_SelectedIndexChanged()" method, hiding the private class
field. So the private class field is never actually changed.

For what it's worth, I wouldn't write "flagCheckIcomeFromLstBx == false".
Just write "!flagCheckIcomeFromLstBx" instead.

Also, I wouldn't actually set the flag in the
"lbxMaze_SelectedIndexChanged()" method, because you don't call
"SetMazeGroupControls()" 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 "SetMazeGroupControls()" that will
cause the validation to occur, IMHO that's the method in which you should
set the flag.

Pete
Jul 3 '08 #5

P: n/a
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 flagCheckIcomeFromLstBx = true;

private void nudNumMin_ValueChanged(object sender, EventArgs e)
{
if (flagCheckIcomeFromLstBx == false)
{
if (nudNumMin.Value >= nudNumMax.Value)
{
MessageBox.Show("La valeur minimale doit être inférieure
√* la valeur maximale.", "Valeur invalide ", MessageBoxButtons.OK);
nudNumMin.Value = nudNumMax.Value - 1;
}

_currParam.GenerationData.NumberMin = (int)nudNumMin.Value;
ValueChanged(true);
}
else
{
_currParam.GenerationData.NumberMin = (int)nudNumMin.Value;
ValueChanged(true);
flagCheckIcomeFromLstBx = false;
}
}

private void nudNumMax_ValueChanged(object sender, EventArgs e)
{
if (flagCheckIcomeFromLstBx == false)
{
if (nudNumMin.Value >= nudNumMax.Value)
{
MessageBox.Show("La valeur minimale doit être inférieure
√* la valeur maximale.", "Valeur invalide ", MessageBoxButtons.OK);

nudNumMin.Value = nudNumMax.Value - 1;
}

_currParam.GenerationData.NumberMax = (int)nudNumMax.Value;
ValueChanged(true);
}
else
{
_currParam.GenerationData.NumberMax = (int)nudNumMax.Value;
ValueChanged(true);
flagCheckIcomeFromLstBx = false;
}
}

private void SetMazeGroupControls()
{
_boInitilizing = true;
flagCheckIcomeFromLstBx = true;

// misc
chbNewRow.Checked = _currParam.NewRow;
nudMazeCount.Value = (decimal)_currParam.Count;

// generation
nudMazeWidth.Value =
(decimal)_currParam.GenerationData.CarreWidth;
nudNumMin.Value = (decimal)_currParam.GenerationData.NumberMin;
nudNumMax.Value = (decimal)_currParam.GenerationData.NumberMax;
nudCellSize.Value = (decimal)_currParam.ExerciseData.CellSize;

// Exercise parameters
chbExerciceShowCible.Checked =
_currParam.ExerciseData.ShowCible;
pnlExerciceCibleColor.BackColor =
_currParam.ExerciseData.CibleColor;
chbExerciseShowNumbers.Checked =
_currParam.ExerciseData.ShowNumbers;
pnlExerciseNumberColor.BackColor =
_currParam.ExerciseData.NumberColor;
chbExerciseShowSolution.Checked =
_currParam.ExerciseData.ShowSolution;
pnlExerciseSolutionColor.BackColor =
_currParam.ExerciseData.SolutionColor;
chbExerciseShowWalls.Checked =
_currParam.ExerciseData.ShowWalls;
nudExerciseWallWidth.Value =
(decimal)_currParam.ExerciseData.WallWidth;

// Solution parameters
chbSolutionShowCible.Checked =
_currParam.SolutionData.ShowCible;
pnlSolutionCibleColor.BackColor =
_currParam.SolutionData.CibleColor;
chbSolutionShowNumbers.Checked =
_currParam.SolutionData.ShowNumbers;
pnlSolutionNumberColor.BackColor =
_currParam.SolutionData.NumberColor;
chbSolutionShowSolution.Checked =
_currParam.SolutionData.ShowSolution;
pnlSolutionSolutionColor.BackColor =
_currParam.SolutionData.SolutionColor;
chbSolutionShowWalls.Checked =
_currParam.SolutionData.ShowWalls;
nudSolutionWallWidth.Value =
(decimal)_currParam.SolutionData.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 "labyrinthales"
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

Jul 5 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.