PDA

View Full Version : مبتدی: چگونه با حروف بین دو دکمه تغییر کنم؟



ricerock
یک شنبه 04 آبان 1393, 21:14 عصر
سلام. من یه سوال در مورد تغییر بین دکمه ها دارم. برنامه ای که ساختم 3 دکمه button داره و میخوام وقتی برنامه را اجرا میکنم بین دکمه ها با w و s حرکت کنم یعنی اگر 3 دکمه زیر هم است با s به سمت پایین حرکت کنم و با w به سمت بالا.در ضمن وقتی بین دکمه ها حرکت میکنم میخوام مثلا یه چیزی دورش باشه که نشون بده الان رو کدوم دکمه هستیم. ممنون میشم راهنمایی کنید...

pedram.11
دوشنبه 05 آبان 1393, 01:40 صبح
سلام
public class CustomContainer : Panel
{
public CustomContainer()
{
}
public void Init()
{
SetPositions();
if (FocusedCtr == null)
FocusedCtr = this.Controls[0];
Control parent;
while (!((parent = this.Parent) is Form)) ;
Form parent_form = (Form)parent;
parent_form.KeyPress += parent_form_KeyPress;
}
Control FocusedCtr;
void SetPositions()
{
foreach (Control ctrl in this.Controls)
{
if (ctrl.Focused)
FocusedCtr = ctrl;
}
}
void parent_form_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case 'w':
GoUp();
break;
case 'd':
GoRight();
break;
case 's':
GetDown();
break;
case 'a':
GoLeft();
break;
}
}
bool HM(Control a, Control b)
{
return ((a.Location.X >= b.Location.X && a.Location.X <= (b.Location.X + b.Width)) ||
(a.Location.X + a.Width) >= b.Location.X && (a.Location.X + a.Width) <= (b.Location.X + b.Width));
}
bool VM(Control a, Control b)
{
return ((a.Location.Y >= b.Location.Y && a.Location.Y <= (b.Location.Y + b.Height)) ||
(a.Location.Y + a.Height) >= b.Location.Y && (a.Location.Y + a.Height) <= (b.Location.Y + b.Height));
}
void GoUp()
{
int minDist = 9999999;
Control ctrl = null;
int buf;
foreach (Control itm in this.Controls)
{
if (itm == FocusedCtr)
continue;
if (HM(itm, FocusedCtr))
if ((buf = FocusedCtr.Location.Y - itm.Location.Y) >= 0 && buf < minDist)
{
minDist = buf;
ctrl = itm;
}
}
if (ctrl != null)
{
ctrl.Focus();
FocusedCtr = ctrl;
}
}


void GoRight()
{
int minDist = 9999999;
Control ctrl = null;
int buf;
foreach (Control itm in this.Controls)
{
if (itm == FocusedCtr)
continue;
if (VM(itm, FocusedCtr))
if ((buf = itm.Location.X - FocusedCtr.Location.X) >= 0 && buf < minDist)
{
minDist = buf;
ctrl = itm;
}
}
if (ctrl != null)
{
ctrl.Focus();
FocusedCtr = ctrl;
}
}
void GetDown()
{
int minDist = 9999999;
Control ctrl = null;
int buf;
foreach (Control itm in this.Controls)
{
if (itm == FocusedCtr)
continue;
if (HM(itm, FocusedCtr))
if ((buf = itm.Location.Y - FocusedCtr.Location.Y) >= 0 && buf < minDist)
{
minDist = buf;
ctrl = itm;
}
}
if (ctrl != null)
{
ctrl.Focus();
FocusedCtr = ctrl;
}
}
void GoLeft()
{
int minDist = 9999999;
Control ctrl = null;
int buf;
foreach (Control itm in this.Controls)
{
if (itm == FocusedCtr)
continue;
if (VM(itm, FocusedCtr))
if ((buf = FocusedCtr.Location.X - itm.Location.X) >= 0 && buf < minDist)
{
minDist = buf;
ctrl = itm;
}
}
if (ctrl != null)
{
ctrl.Focus();
FocusedCtr = ctrl;
}
}
}

این کنترل رو به فرم اضافه کنید و کنترل ها رو بهش اضافه کنید. درضمن در فرم پراپرتی KeyPreview رو True قرار بدید و بعدش متد Init کلاس رو اجرا کنید:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
this.customContainer1.Init();
}
}



سورس پروژه: