Witam
Muszę ukończyć edytor tekstowy ale niestety mam pewien problem dotyczący wywołania funkcji KeyPress, która wywołuje się tylko wtedy gdy ComboBox jest zaznaczony (typ lub rozmiar czcionki) a to przeszkadza bo gdy nacisnę cyfrę lub literę na którą zaczyna się rozmiar lub nazwa czcionki to program ją automatycznie zmienia a tego chciałbym uniknąć. Natomiast funkcja KeyDown działa bez problemu.
Jak mam zaradzić temu problemowi?
Dodam, że we właściwościach Form KeyPreview mam ustawione na True.
Załączam screena
Oraz fragment kodu:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
KeyStr = e.KeyCode.ToString();
try
{
font1 = new Font(tsp_TypeFont.SelectedItem.ToString(), Convert.ToSingle(tsp_SizeFont.SelectedItem));
}
catch
{
font1 = new Font("Times New Roman", 12);
tsp_TypeFont.SelectedItem = "Times New Roman";
tsp_SizeFont.SelectedIndex = 12;
}
font1 = new Font(font1, FontStyleText.Style);
gr.FillRectangle(new SolidBrush(pbx_TextDisplayer.BackColor), (PointIndex.X * widthArea), PointIndex.Y * heightArea, widthArea / 5, heightArea);
if (e.KeyCode == Keys.Enter)
{ //obsluga przycisku Enter
PointIndex = new Point(0, PointIndex.Y+1);
cofnijToolStripMenuItem.Enabled = true;
//AllText.Insert(PointIndex.Y + 1, new GapBuffer());
//CommandProcessor.Do(new InsertCommand(text, e.KeyCode, Console.CursorLeft - 1));
}
else if (e.KeyCode == Keys.Back)
{ //obsluga przycisku backspace
if (PointIndex.X - 1 >= 0)
PointIndex = new Point(PointIndex.X - 1, PointIndex.Y);
else if (PointIndex.Y - 1 >= 0)
PointIndex = new Point((pbx_TextDisplayer.Width / widthArea)-1, PointIndex.Y - 1);
gr.FillRectangle(new SolidBrush(pbx_TextDisplayer.BackColor), PointIndex.X * widthArea, PointIndex.Y * heightArea, widthArea, heightArea);
}
else if (e.KeyCode == Keys.Left)
{
if (PointIndex.X - 1 >= 0)
PointIndex = new Point(PointIndex.X - 1, PointIndex.Y);
else if (PointIndex.Y - 1 >= 0)
PointIndex = new Point((pbx_TextDisplayer.Width / widthArea) - 1, PointIndex.Y - 1);
}
else if (e.KeyCode == Keys.Right)
{
if (PointIndex.X + 1 < pbx_TextDisplayer.Width / widthArea)
PointIndex = new Point(PointIndex.X + 1, PointIndex.Y);
else if (PointIndex.Y + 1 < pbx_TextDisplayer.Height / heightArea)
PointIndex = new Point(0, PointIndex.Y + 1);
}
else if (e.KeyCode == Keys.Up)
{
if (PointIndex.Y - 1 >= 0)
PointIndex = new Point(PointIndex.X, PointIndex.Y - 1);
}
else if (e.KeyCode == Keys.Down)
{
if (PointIndex.Y + 1 < pbx_TextDisplayer.Height / heightArea)
PointIndex = new Point(PointIndex.X, PointIndex.Y + 1);
}
else if (e.KeyCode == Keys.Z && e.Modifiers == Keys.Control)
{
}
gr.DrawString("|", new Font("Times New Roman", font1.Size), new SolidBrush(Color.Black), (PointIndex.X * widthArea) - widthArea / 5, PointIndex.Y * heightArea);
pbx_TextDisplayer.Refresh();
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
try
{
font1 = new Font(tsp_TypeFont.SelectedItem.ToString(), Convert.ToSingle(tsp_SizeFont.SelectedItem));
}
catch
{
font1 = new Font("Times New Roman", 12);
}
font1 = new Font(font1, FontStyleText.Style);
if (char.IsDigit(e.KeyChar) || char.IsLetter(e.KeyChar) || char.IsSymbol(e.KeyChar) || SpecialCharacters.Contains(e.KeyChar))
{ //wpisywanie znakow z klawiatury na edytor
//AllText[PointIndex.Y].Expand(PointIndex.X, 1);
//AllText[PointIndex.X].Insert(e.KeyChar.ToString());
cofnijToolStripMenuItem.Enabled = true;
gr.FillRectangle(new SolidBrush(pbx_TextDisplayer.BackColor), (PointIndex.X * widthArea), PointIndex.Y * heightArea, widthArea / 5, heightArea);
gr.DrawString(e.KeyChar.ToString(), font1, new SolidBrush(tsp_ColorFont.BackColor), PointIndex.X * widthArea, PointIndex.Y * heightArea);
if (PointIndex.X + 1 < pbx_TextDisplayer.Width / widthArea)
PointIndex = new Point(PointIndex.X + 1, PointIndex.Y);
else if (PointIndex.Y + 1 < pbx_TextDisplayer.Height / heightArea)
PointIndex = new Point(0, PointIndex.Y + 1);
gr.DrawString("|", new Font("Times New Roman", font1.Size), new SolidBrush(Color.Black), (PointIndex.X * widthArea)-widthArea/5, PointIndex.Y * heightArea); //kursor
}
pbx_TextDisplayer.Refresh();
}