עבור לתוכן

איך משתמשים במקלדת בC#??

Featured Replies

פורסם

מה נשמע?

המורה שלי למחשבים הטילה על כמה תלמידים בכיתה(אני בינהם) להראות לכיתה איך להשתמש בWindows application.

חיפשתי בכל מקום אפשרי ולא מצאתי.

עזרה בבקשה. תודה מראש

פורסם

אם תסביר מה אתה רוצה, אולי נוכל לעזור לך (מקלדת ? Windows application ?)

פורסם

נראה לי שהוא מתכוון למקלדת הוירטואלית שיש בווינדוס

פורסם

אתה מתכוון נגיד שרושמים משהו בתיבת טקסט ולהראות את זה בתור הודעת שגיאה כזה?

במקרה וכן זה משהו כזה :


string text = textBox.Text;
MessegeBox.show(text);

אם לזה התכוונת :s05:

פורסם
  • מחבר

*סליחה על התגובה המאוחרת*

לא...

אני מתכוון שנגיד שיש LABEL שכתוב בה משהו.

ואז לוחצים על 'a' או על חץ שמאלה והLABEL זזה שמאלה איזה 10 פיקסלים.

אני יכול לעשות את זה בCONSOLE APPLICATION ע"י הפקודות


x=Console.ReadKey();
if (x.Key==ConsoleKeys.'a')
ConsoleKeyInfo x=new ConsoleKeyInfo();

ואז כל מיני דברים שיקרו כמו set cursor pisition וכתיבה של משהו שיזוז וכד'.

הקיצר אני צריך פקודות שיקלטו משהו מהמקלדת לתוך משתנה ואז שאני יוכל לבדוק מה זה ולהשתמש.

פורסם

אתה צריך או להירשם לאירוע של KeyPress או להשתמש באירוע עצמו

הנה 2 הדרכים בשבילך



private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
Point p = new Point();
switch (e.KeyChar)
{
case 'w':
p.X = label1.Location.X;
p.Y = label1.Location.Y - 10;
break;
case 'a':
p.X = label1.Location.X-10;
p.Y = label1.Location.Y;
break;
case 's':
p.X = label1.Location.X;
p.Y = label1.Location.Y + 10;
break;
case 'd':
p.X = label1.Location.X+10;
p.Y = label1.Location.Y;
break;
default:
break;
}
label1.Location = p;


}

או


public Form1()
{
KeyPress += new KeyPressEventHandler(Custom_Handler);
InitializeComponent();
}

void Custom_Handler(object sender, KeyPressEventArgs e)
{
Point p = new Point();
switch (e.KeyChar)
{
case 'w':
p.X = label1.Location.X;
p.Y = label1.Location.Y - 10;
break;
case 'a':
p.X = label1.Location.X-10;
p.Y = label1.Location.Y;
break;
case 's':
p.X = label1.Location.X;
p.Y = label1.Location.Y + 10;
break;
case 'd':
p.X = label1.Location.X+10;
p.Y = label1.Location.Y;
break;
default:
break;
}
label1.Location = p;
}

פורסם
  • מחבר

ואללה תודה רבה

המשכתי את התוכנית שרציתי לשים את זה בה וזה מה שיצא:

[left]public class Program : Form
{
// Create an instance of a Label.
private Label label1;
private Button b;
// Constructor
public Program()
{
// Changes the font name and size

this.Font = new Font("MS Sans Serif", 20);

// Label initialization and setup
label1 = new Label();
label1.Location = new Point(24, 16);
label1.Size = new Size(150, 24);
label1.Text = "......";
label1.TextAlign = ContentAlignment.MiddleCenter;

// Add the label to the Form
this.Controls.Add(label1);
b = new Button();
b.Location = new Point(10, 10);
b.Size = new Size(100, 100);
b.Text = "nothing";
b.TextAlign = ContentAlignment.MiddleCenter;
this.Controls.Add(b);



KeyPress += new KeyPressEventHandler(handler);
InitializeComponent();


}



// Main begins program execution


static void Main()
{
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Program());


}

private void InitializeComponent()
{
this.SuspendLayout();
//
// Program
//
this.ClientSize = new System.Drawing.Size(503, 266);
this.Name = "Program";
this.ResumeLayout(false);

}
void handler(object sender, KeyPressEventArgs e)
{
Point p = new Point();
switch (e.KeyChar)
{
case 'a':
{
p.X = label1.Location.X-40;
p.Y=label1 .Location .Y;

break;
}
case 'd':
{
p.X = label1.Location.X+40;
p.Y=label1 .Location .Y;

break;
}

default: break;
}
label1.Location = p;
}


}
}
[/left]

למה זה לא עובד אני לא יודע...

פורסם

הקוד עובר קומפילציה??

אם כן אז תבדוק שאכן יש לך את האירוע ושנרשמת אליו כמו שצריך ושהשם של ה-LABEL אכן תואם לLABEL שאתה רוצה להזיז

פורסם
  • מחבר

מה זאת אומרת יש את האירוע ונרשמת אליו איך שצריך?

כתבתי את הארוע,


{
Point p = new Point();
switch (e.KeyChar )
{
case 'a':
{
p.X = label1.Location.X-40;
p.Y=label1 .Location .Y;

break;
}
case 'd':
{
p.X = label1.Location.X+40;
p.Y=label1 .Location .Y;

break;
}

default: break;
}
label1.Location = p;
}
private void handler(object sender, KeyPressEventArgs e)

ונרשמתי אלי ככה:


InitializeComponent();
KeyPress += new KeyPressEventHandler(handler);

הלייבלים מתאימים.

שאלתי את המורה שלי למחשבים והוא אמר שיכול להיות שהוא לא מזהה את זה אלא אם כן הגדרתי את זה ב-form designer אבל לא נראה לי שזאת בעיה..

פורסם

בטוח שאתה בenglish lower case? אם המקלדת שלך מכוונת לעברית, או שהCaps Lock דולק, זה לא יעבוד.

תוודא שאין לך autoeventwireup=true (מכיר את זה מweb, מניח שיש משהו זהה בwin).

פורסם


labelName.KeyPress+= new KeyPressEventHandler(handler);
InitializeComponent();

זה צריך להיות ככה

פורסם
  • מחבר

אני ינסה גם עם Labelname.keypress. תודה

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים