The code is simple:
namespace Textboxta10_karakterde_1_alt_satira_gecen_program
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int charNumber = 0;
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (charNumber >= 2)
{
MessageBox.Show("Hey");
if (charNumber % (2) == 0)
{
MessageBox.Show("Hey");
textBox1.AppendText(Environment.NewLine);
}
charNumber ;
MessageBox.Show("Hey");
}
}
}
}
It never shows the "Hey" message and it never goes to line below when the charNumber is divisible to 2.
But if I make the code if (charNumber <= 2)
instead of if (charNumber >= 2)
, it does show the "Hey" message as expected and writes the next characters one line below as expected.
I did not understand the problem.
CodePudding user response:
charNumber
is initialized to 0 and will never be anything other than 0 with the posted code, since the increment is inside the outer if block, which only runs when charNumber
>= 2. Maybe you want charNumber ;
outside of that outer if block like this?
int charNumber = 0;
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (charNumber>=2)
{
MessageBox.Show("Hey");
if (charNumber % (2) == 0)
{
MessageBox.Show("Hey");
textBox1.AppendText(Environment.NewLine);
}
MessageBox.Show("Hey");
}
charNumber ;
}