PDA

View Full Version : سوال: چطوری میشه که یه فرم فقط یه بار نمایش داده بشه



neda_dela
شنبه 01 خرداد 1389, 06:54 صبح
سلام دوستان
من می خام وقتی کاربر برنامه رو برای اولین بار اجرا می کنه فرمم نمایش داده بشه و بقیه مواقع دیگه نمایش داده نشه آیا میشه اصلا؟
ممنون:لبخندساده:

Ehsan.Saradar
شنبه 01 خرداد 1389, 07:09 صبح
روش اول :

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;

namespace sample
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Process curr = Process.GetCurrentProcess();
Process[] procs = Process.GetProcessesByName(curr.ProcessName);
foreach (Process p in procs)
{
if ((p.Id != curr.Id) &&
(p.MainModule.FileName == curr.MainModule.FileName))
return;
}

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
}
}
روش دوم :


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace sample
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
bool ok;
System.Threading.Mutex m = new System.Threading.Mutex(true, "test", out ok);
if (!ok)
{
MessageBox.Show("Another instance is already running.");
return;
}

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
}
}

M.YasPro
شنبه 01 خرداد 1389, 07:24 صبح
سلام
من از این روش استفاده می کنم :


private void showForm_Click(object sender, EventArgs e)
{
foreach (Form frm in Application.OpenForms)
{
if (frm.GetType() == typeof(frmCar))
{
return;
}
}
frmCar frmcar = new frmCar(true);
frmcar.Show();
}

موفق باشید .

majid325
شنبه 01 خرداد 1389, 08:15 صبح
احتمالا منظور دوستمون اینه که در ویندوز برای بار اول که برنامه اجرا میشه یه کاری بکنه:
اگه اینطور هست, تو ریجستری یه کلید رو چک کن, بار اول که برنامت اجرا شد مقدارش رو تغییر بده .

اَرژنگ
شنبه 01 خرداد 1389, 09:43 صبح
احتمالا منظور دوستمون اینه که در ویندوز برای بار اول که برنامه اجرا میشه یه کاری بکنه:
اگه اینطور هست, تو ریجستری یه کلید رو چک کن, بار اول که برنامت اجرا شد مقدارش رو تغییر بده .
این روش اصلاً توصیه نمیشه ، چونکه اگر برنامه درست بسته نشد و مقدار در رجیستری قبل از خروج عوض نشد دیگر برنامه با اینکه در حافظه نیست باز نمیشه.

neda_dela
یک شنبه 02 خرداد 1389, 23:46 عصر
روش اول :

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;

namespace sample
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Process curr = Process.GetCurrentProcess();
Process[] procs = Process.GetProcessesByName(curr.ProcessName);
foreach (Process p in procs)
{
if ((p.Id != curr.Id) &&
(p.MainModule.FileName == curr.MainModule.FileName))
return;
}

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
}
}
روش دوم :


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace sample
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
bool ok;
System.Threading.Mutex m = new System.Threading.Mutex(true, "test", out ok);
if (!ok)
{
MessageBox.Show("Another instance is already running.");
return;
}

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
}
}
مرسی دوست من هر دو روشتون رو امتحان کردم
روش اول از process خطا می گرفت اصلا میگه could not be found
روش دوم هم که اصلا عملکرد مد نظر منو نداشت اصلا هیچ تغییری ندیدم

neda_dela
یک شنبه 02 خرداد 1389, 23:47 عصر
سلام
من از این روش استفاده می کنم :


private void showForm_Click(object sender, EventArgs e)
{
foreach (Form frm in Application.OpenForms)
{
if (frm.GetType() == typeof(frmCar))
{
return;
}
}
frmCar frmcar = new frmCar(true);
frmcar.Show();
}

موفق باشید .
مرسی دوست جون
من توی ایونت های فرم هام چیزی به اسم showform ندیدم:متفکر:

M.YasPro
دوشنبه 03 خرداد 1389, 08:49 صبح
اسم یه باتن هست .

neda_dela
دوشنبه 03 خرداد 1389, 14:35 عصر
اسم یه باتن هست .
اوکی
اخه اینطوری نمیشه
ببین من یه نمونه برنامه دیدم که بعد از نصب با اولین اجرا یه فرم ظاهر میشه که با استفاده از اون فرم نام کاربری و رمز عبور رو از کاربر می گرفت و در دفعات بعدی اجرا دیگه اون فرم ظاهر نمی شد
و اولین فرم در دفعات بعدی که نمایش داده میشد فرم ورود نام کاربری و رمز عبور بود
من یه همچین چیزی می خام
میخام که فرم فقط یه بار و فقط در اولین اجرا طاهر بشه یه سری اطلاعات از کاربر بگیره و دیگه نشون داده نشه
کمکم کنید بچه ها:افسرده:

Ehsan.Saradar
دوشنبه 03 خرداد 1389, 15:49 عصر
متاسفانه من ابتدا سوال شما را درست متوجه نشدم اما حالا با توضیحاتی که دادید متوجه شدم روش پیشنهادی من :
1- در settings مربوط به پروژه خود یک متغیر جدید به نام first_time از نوع bool با مقدار اولیه true به شکل زیر اضافه کنید :

http://up.iranblog.com/Files/f75a008ef9df468da22b.jpg

2- در program.cs برنامه خود به صورت زیر بنوسید :



static void Main()
{


Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
bool first_time = global::sample.Properties.Settings.Default.first_t ime;
if (first_time == true)
{
Application.Run(new Form1());
global::sample.Properties.Settings.Default.first_t ime = false;
global::sample.Properties.Settings.Default.Save();
}
else
Application.Run(new Form2());

}توضیحات :
1- sample نام پروزه من است و طبیعتا شما باید به جای آن نام پروژه خود را جایگزین کنید
2- from1 تنها برای اولین بار اجرای برنامه نمایش داده می شود
3- from2 دفعات بعدی نمایش داده می شود => from1 در دفعات بعدی نمایش داده نمی شود