نوشتن کد سی شارپ برنامه جورچین
برای نوشتن کد سی شارپ برنامه جورچین ابتدا ظاهر فرم را طراحی می کنیم.(تذکر:در جلسه اول با محیط ویندوز فرم آشنا شدیم و همچنین کار کردن با دستورات را در آموزش برنامه نویسی تحت کنسول آموزش دادیم)
خصوصیات فرم اصلی را به صورت زیر مشخص می کنیم:
RightToLeft = Yes
ForeColor =Blue
Text =جورچین
MaximizeBox =False
MinimizeBox =False
بعد کنترل ها را از پنجره Toolbax روی فرم قرار می دهیم.
کنترل های مورد نیاز : در اینجا 9 تا کنترل Button مورد نیاز است که فقط Text یکی از آنها را خالی می گذاریم.
برنامه نویسی سی شارپ C#:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
if(button2.Text==””)
{
button2.Text = button1.Text;
button1.Text = “”;
}
if (button4.Text == “”)
{
button4.Text = button1.Text;
button1.Text = “”;
}
}
private void button2_Click(object sender, EventArgs e){
if (button1.Text == “”)
{
button1.Text = button2.Text;
button2.Text = “”;
}
if (button5.Text == “”)
{
button5.Text = button2.Text;
button2.Text = “”;
}
if (button3.Text == “”)
{
button3.Text = button2.Text;
button2.Text = “”;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (button2.Text == “”)
{
button2 .Text = button3.Text;
button3.Text = “”;
}
if (button6.Text == “”)
{
button6.Text = button3.Text;
button3.Text = “”;
}
}
private void button4_Click(object sender, EventArgs e)
{
if (button1.Text == “”)
{
button1.Text = button4.Text;
button4.Text = “”;
}
if (button5.Text == “”)
{
button5.Text = button4.Text;
button4.Text = “”;
}
if (button7.Text == “”)
{
button7.Text = button4.Text;
button4.Text = “”;
}
}
private void button5_Click(object sender, EventArgs e){
if (button2.Text == “”)
{
button2.Text = button5.Text;
button5.Text = “”;
}
if (button4.Text == “”)
{
button4.Text = button5.Text;
button5.Text = “”;
}
if (button6.Text == “”)
{
button6.Text = button5.Text;
button5.Text = “”;
}
if (button8.Text == “”)
{
button8.Text = button5.Text;
button5.Text = “”;
}
}
private void button6_Click(object sender, EventArgs e){
if (button3.Text == “”)
{
button3.Text = button6.Text;
button6.Text = “”;
}
if (button5.Text == “”)
{
button5.Text = button6.Text;
button6.Text = “”;
}
if (button9.Text == “”)
{
button9.Text = button6.Text;
button6.Text = “”;
}
}
private void button7_Click(object sender, EventArgs e)
{
if (button4.Text == “”)
{
button4.Text = button7.Text;
button7.Text = “”;
}
if (button8.Text == “”)
{
button8.Text = button7.Text;
button7.Text = “”;
}
}
private void button8_Click(object sender, EventArgs e)
{
if (button5.Text == “”)
{
button5.Text = button8.Text;
button8.Text = “”;
}
if (button7.Text == “”)
{
button7.Text = button8.Text;
button8.Text = “”;
}
if (button9.Text == “”)
{
button9.Text = button8.Text;
button8.Text = “”;
}
}
private void button9_Click(object sender, EventArgs e)
{
if (button6.Text == “”)
{
button6.Text = button9.Text;
button9.Text = “”;
}
if (button8.Text == “”)
{
button8.Text = button9.Text;
button9.Text = “”;
}
}
}
وقتی برنامه جورچین را اجرا می کنیم فقط عدد ها در کنترل Button جا به جا می شوند اگر بخواهیم خود Button جابجا شود باید یکی از باتن ها را مخفی کنیم پس در ابتدا خصوصیت Visible کنترل Button9 را False می کنیم:
برنامه نویسی:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
—————–پروسیجری که باعث جابجا شدن باتن ها می شود—————– ——– //
private void Vis_btn()
{
if (button1.Text == “”)
button1.Visible = false;
else
button1.Visible = true;
//————————————-
if (button2.Text == “”)
button2.Visible = false;
else
button2.Visible = true;
//————————————-
if (button3.Text == “”)
button3.Visible = false;
else
button3.Visible = true;
//————————————-
if (button4.Text == “”)
button4.Visible = false;
else
button4.Visible = true;
//————————————-
if (button5.Text == “”)
button5.Visible = false;
else
button5.Visible = true;
//————————————-
if (button6.Text == “”)
button6.Visible = false;
else
button6.Visible = true;
//————————————-
if (button7.Text == “”)
button7.Visible = false;
else
button7.Visible = true;
//————————————-
if (button8.Text == “”)
button8.Visible = false;
else
button8.Visible = true;
//————————————-
if (button9.Text == “”)
button9.Visible = false;
else
button9.Visible = true;
}
//———————پایان پروسیجر———– —————-
private void button1_Click(object sender, EventArgs e)
{
if(button2.Text==””)
{
button2.Text = button1.Text;
button1.Text = “”;
}
if (button4.Text == “”)
{
button2.Text = button1.Text;
button1.Text = “”;
}
Vis_btn();//پروسیجر جابجا کردن باتن ها
}
private void button2_Click(object sender, EventArgs e)
{
if (button1.Text == “”)
{
button1.Text = button2.Text;
button2.Text = “”;
}
if (button5.Text == “”)
{
button5.Text = button2.Text;
button2.Text = “”;
}
if (button3.Text == “”)
{
button3.Text = button2.Text;
button2.Text = “”;
}
Vis_btn();//پروسیجر جابجا کردن باتن ها
}
private void button3_Click(object sender, EventArgs e)
{
if (button2.Text == “”)
{
button2 .Text = button3.Text;
button3.Text = “”;
}
if (button6.Text == “”)
{
button6.Text = button3.Text;
button3.Text = “”;
}
Vis_btn();//پروسیجر جابجا کردن باتن ها
}
private void button4_Click(object sender, EventArgs e)
{
if (button1.Text == “”)
{
button1.Text = button4.Text;
button4.Text = “”;
}
if (button5.Text == “”)
{
button5.Text = button4.Text;
button4.Text = “”;
}
if (button7.Text == “”)
{
button7.Text = button4.Text;
button4.Text = “”;
}
Vis_btn();//پروسیجر جابجا کردن باتن ها
}
private void button5_Click(object sender, EventArgs e)
{
if (button2.Text == “”)
{
button2.Text = button5.Text;
button5.Text = “”;
}
if (button4.Text == “”)
{
button4.Text = button5.Text;
button5.Text = “”;
}
if (button6.Text == “”)
{
button6.Text = button5.Text;
button5.Text = “”;
}
if (button8.Text == “”)
{
button8.Text = button5.Text;
button5.Text = “”;
}
Vis_btn();//پروسیجر جابجا کردن باتن ها
}
private void button6_Click(object sender, EventArgs e)
{
if (button3.Text == “”)
{
button3.Text = button6.Text;
button6.Text = “”;
}
if (button5.Text == “”)
{
button5.Text = button6.Text;
button6.Text = “”;
}
if (button9.Text == “”)
{
button9.Text = button6.Text;
button6.Text = “”;
}
Vis_btn();//پروسیجر جابجا کردن باتن ها
}
private void button7_Click(object sender, EventArgs e)
{
if (button4.Text == “”)
{
button4.Text = button7.Text;
button7.Text = “”;
}
if (button8.Text == “”)
{
button8.Text = button7.Text;
button7.Text = “”;
}
Vis_btn();//پروسیجر جابجا کردن باتن ها
}
private void button8_Click(object sender, EventArgs e)
{
if (button5.Text == “”)
{
button5.Text = button8.Text;
button8.Text = “”;
}
if (button7.Text == “”)
{
button7.Text = button8.Text;
button8.Text = “”;
}
if (button9.Text == “”)
{
button9.Text = button8.Text;
button8.Text = “”;
}
Vis_btn();//پروسیجر جابجا کردن باتن ها
}
private void button9_Click(object sender, EventArgs e)
{
if (button6.Text == “”)
{
button6.Text = button9.Text;
button9.Text = “”;
}
if (button8.Text == “”)
{
button8.Text = button9.Text;
button9.Text = “”;
}
Vis_btn();//پروسیجر جابجا کردن باتن ها
}
همانطور که مشاهده می کنید برنامه جورچین به سادگی نوشته شد. در آموزش بعدی قصد داریم کاربرد dialog ها در سی شارپ را آموزش دهیم، با ما همراه باشید.
سلام چطور می تونم کاری کنم هربار با یه چینش جدید بصورت رندم ظاهر بشن؟؟؟؟؟؟؟؟؟ ممنون میشم توضیح بدین
این الان اموزش هست یا فقط کپی کد؟!
بسیار زیبا و عالی گفتید . ممنون از اینکه این کدها را با توضیحات در سایت مفیدتان قرار می دهید . اجر نشر علمتان نزد خدا بزرگ است انشاء الله.
فقط فکر کنم تکه کد زیر اشتباه تایپی دارد
.if (button4.Text == “”)
{
button2.Text = button1.Text;
button1.Text = “”;
}
که مطابق عملکرد سایر کدها بجای تکست باکس ۲ باید تکست باکس ۴ باشد.
با سلام و عرض ادب
بله شما درست می فرمایید اشتباه تایپی است که مجدد به روز رسانی شد .
ممنون از همراهی و دقت شما
سلام
کدی ک برای اخرش بیاد ک بزنه شما موفق شدید چی؟
و اینکه کد نویسی اینکه ما وقتی صفحه رو باط میکنیم بزنه میخواید وارد بشید یا خارج ؟
سلام و ممنون از آموزشهای خوبتون
می خواستم یک مجموعه اعداد را به صورت گردشی ظاهر بشن مثلا عدد ۱۲۳۴۵۶۷۸ را به صورت ۴۵۶۷۸۱۲۳ ظاهر بشن یعنی سه عدد اول بیان در آخر