message box pops up twice

  • Thread starter Thread starter John Salerno
  • Start date Start date
J

John Salerno

Ok, I'm just messing around with a little project based on a computer
game I'm playing, which should explain some of the references in the
code. My problem is, when I click "OK" in the message box, my computer
beeps again and the message box remains (which I assume means it is
appearing a second time). After I click OK again, it goes away. Why is this?

Also, any advice on how to further generalize the code in the event
handler would be appreciated. At first, I had all the checking in the
event handler, then I realized I should probably write my own method to
check. Basically what I want is if you click the word "Barbarian" while
one of three particular other radio buttons is clicked, I want the
message to appear. At first I had the alignment clear itself, but then I
figured it made more sense to clear the "Barbarian" radio button, since
that's what is being clicked in the first place.





public partial class frmCharacterManager : Form
{
private string incompatibleSelection = "Incompatible Selection";

public frmCharacterManager()
{
InitializeComponent();
}

private void rdoBarbarian_CheckedChanged(object sender,
EventArgs e)
{
string barbarianMessage = "The Barbarian class is
restricted to nonlawful alignments.";

if (CheckLawfulAlignment())
{
rdoBarbarian.Checked = false;
MessageBox.Show(barbarianMessage, incompatibleSelection);
}

}

public bool CheckLawfulAlignment()
{
if (rdoLawfulGood.Checked == true ||
rdoLawfulNeutral.Checked == true || rdoLawfulEvil.Checked == true)
return true;
else
return false;
}
}
 
Hello

Yes, it could be.You can check your check box, so the event will be fired.
If CheckLawfulAlignment() returns true, then you will see your message. But,
the code "rdoBarbarian.Checked = false;" will lead to the new event
"CheckedChanged", so you will see your message second time.
 
Hi,

Why don't you put a breakpoint in the code, then you can see if it's being
called twice
If so probably you are assigning the method twice to the event. you have to
find where, check where you assign it and place BP there too


cheers,
 
Thanks guys. Turns out it *was* being called twice, and the breakpoint
helped me see that.

Ok, I reworked the class and I'd like to see what you think of it now. I
tried to make it as generic as possible until necessary. My questions
this time are: is there a better way to do the Check methods than to
pass it a RadioButton object?

Also, an interesting situation is that the Barbarian and Bard classes (I
know this is weird, but it's something that will keep me interested, as
opposed to products and customers and that type of thing) both have the
same restriction: no radio button with "lawful" can be checked. Is there
a better way to implement this, since they both require the same
functionality, or is this way good?

public partial class frmCharacterManager : Form
{
private string incompatibleSelection = "Incompatible Selection";

public frmCharacterManager()
{
InitializeComponent();
}

private void rdoBarbarian_CheckedChanged(object sender, EventArgs e)
{
CheckBarbarianAlignment(rdoBarbarian);
}

private void rdoLawfulGood_CheckedChanged(object sender, EventArgs e)
{
CheckBarbarianAlignment(rdoLawfulGood);
CheckBardAlignment(rdoLawfulGood);
}

private void rdoLawfulNeutral_CheckedChanged(object sender, EventArgs e)
{
CheckBarbarianAlignment(rdoLawfulNeutral);
CheckBardAlignment(rdoLawfulNeutral);
}

private void rdoLawfulEvil_CheckedChanged(object sender, EventArgs e)
{
CheckBarbarianAlignment(rdoLawfulEvil);
CheckBardAlignment(rdoLawfulEvil);
}

public void CheckBarbarianAlignment(RadioButton radioButton)
{
if (IsBarbarian() && IsLawful())
{
string barbarianMessage = "The Barbarian class is restricted to
nonlawful alignments.";
radioButton.Checked = false;
stsStatusLabel.Text = barbarianMessage;
MessageBox.Show(barbarianMessage, incompatibleSelection);
}
}

public void CheckBardAlignment(RadioButton radioButton)
{
if (IsBard() && IsLawful())
{
string bardMessage = "The Bard class is restricted to nonlawful
alignments.";
radioButton.Checked = false;
stsStatusLabel.Text = bardMessage;
MessageBox.Show(bardMessage, incompatibleSelection);
}
}

public void CheckDruidAlignment(RadioButton radioButton)
{
if (IsDruid() && !(IsNeutral()))
{
string druidMessage = "The Druid class must have a neutral alignment.";
radioButton.Checked = false;
stsStatusLabel.Text = druidMessage;
MessageBox.Show(druidMessage, incompatibleSelection);
}
}

public bool IsBarbarian()
{
if (rdoBarbarian.Checked == true)
return true;

return false;
}

public bool IsBard()
{
if (rdoBard.Checked == true)
return true;

return false;
}

public bool IsLawful()
{
if (rdoLawfulGood.Checked == true || rdoLawfulNeutral.Checked == true
|| rdoLawfulEvil.Checked == true)
return true;

return false;
}



private void rdoBard_CheckedChanged(object sender, EventArgs e)
{
CheckBardAlignment(rdoBard);
}

}
 
Another quick question:

I wrote this code:

public void CheckPaladinAlignment(RadioButton radioButton)
{
if (IsPaladin() && (!IsLawful() || !IsGood()))
{
string paladinMessage = "The Paladin class must have a Lawful Good
alignment.";
radioButton.Checked = false;
stsStatusLabel.Text = paladinMessage;
MessageBox.Show(paladinMessage, incompatibleSelection);
}
}

But the problem is that I get the error message even when no Alignment
radio buttons are checked. I'm sure this has to do with the ! operator,
but I don't know how to fix it.
 
Have you thought about checking to see if one of the radio button has
been checked?
 
cryolitte said:
Have you thought about checking to see if one of the radio button has
been checked?

Yeah, that occurred to me. I'll just switch it around to check for the
other conditions instead of the opposite of the correct conditions.
 
Back
Top