PDA

View Full Version : سوال: فرم در فرم و نمایش فرمی دیگر



saeedjafari
چهارشنبه 06 بهمن 1389, 18:25 عصر
با درود وقت بخیر
در اکثر پروژه ها دیده میشه که فرم لوگینی هست
برای اینکه پس از وارد کردن یوزر و پسور فرم لوگین یا فرم اصلی برنامه ظاهر نشه
چه باید کرد
ممنون میشم

hadi-z
چهارشنبه 06 بهمن 1389, 18:52 عصر
سلام
میشه سوالتونو واضح تر بپرسید
مبهمه

saeedjafari
چهارشنبه 06 بهمن 1389, 18:58 عصر
سلام
میشه سوالتونو واضح تر بپرسید
مبهمه



ببینید
ما یه فرم اصلی داریم که با ران کردن برنامه باز میشه
وقتی یوزر و پس رو میزنیم که به فرمهای دیگه دسترسی داشته باشیم
هنوز این فرم یوزر نیم و پسور بازه

چه باید کرد ؟؟

hadi-z
چهارشنبه 06 بهمن 1389, 19:01 عصر
this.Hide();

از این بعد از باز کردن فرم دوم استفاده کنید

saeedjafari
جمعه 08 بهمن 1389, 11:00 صبح
this.Hide();

از این بعد از باز کردن فرم دوم استفاده کنید


با درود
این دستور رو کجا باید قرار داد ؟؟
فرم ما به صورت منویی هست و با کلیلک روی هر منو یک صفحه باز میشه
حالا میخواهیم فرم قبلی (همون فرم اصلی) نشون داده نشه
با تشکر

MSN_Issue
جمعه 08 بهمن 1389, 11:05 صبح
فرم یوزر-پس رو به این صورت باز کنید :

uform.showdialog();

hamed shahba
جمعه 08 بهمن 1389, 15:49 عصر
سلام
دوست عزیز
به طور مثال
فرم یوزر شما A
باوارد شدن به برنامه فرم Bباز میشود
حال در تابع باتون فرم Aاین کد رو وارد کن

B a1 = new B();
this.Hide();
a1.Show();
موفق باشید

ACorvinus
جمعه 08 بهمن 1389, 16:58 عصر
سلام.


فرم ما به صورت منویی هست و با کلیلک روی هر منو یک صفحه باز میشه


همونطور که در اکثر برنامه های تجاری دیده میشه ابتدا یه فرم کوچیک واسه چک کردن نام کاربری و گذرواژه باز میشه (پس تا اینجا وقتی برنامه رو باز می کنین تنها و تنها یه فرم باز میشه اونم فرم لاگین شما هستش). بعد اگه کاربر، نام کاربری و گذرواژه رو صحیح وارد کرد و روی دکمه Login کلیک کرد اون موقع این فرم (فرم Login) مخفی (Hide) میشه و فرم اصلیتون باز (Show) میشه.

فقط باید توجه داشته یاشین که وقتی دارین برنامه رو میبندین نباید از دستور this.Close() استفاده کنین چون در اینصورت برنامه کاملا بسته نمیشه و دلیلشم اینه که هنوز یه فرم hide شده دارین. و فقط باید از دستور Application.Close() استفاده کنین.

موفق باشین.

ehsanara
جمعه 08 بهمن 1389, 21:48 عصر
دوست عزیز پیشنهاد من چیز دیگه ای هست
فرم اصلی برنامه رو فرم Login قرار نده
فرم دوم روفرم اصلی بزار ولی موقع اجرای برنامه Hide شه و فرم Login نمایش داده شه
در اینصورت بعد از درست بودن User & Pass میتونید فرم Login رو Close کنید
به این ترتیب فرم Login شما بسته میشه و نیازی به Hide شدن هم نداره
اکثر برنامه های بزرگ اینطوری نوشته میشن، اگه دقت کنین از لحاظ امنیتی هم بهتر هست

ACorvinus
جمعه 08 بهمن 1389, 22:58 عصر
سلام.

در مورد این حرفتون مطمئن هستین؟!
اکثر برنامه های بزرگ اینطوری نوشته میشن، اگه دقت کنین از لحاظ امنیتی هم بهتر هست
آیا در این مورد میتونین برای اثبات حرفتون 3 تا از این برنامه های بزرگ رو مثال بزنین؟

ehsanara
شنبه 09 بهمن 1389, 00:17 صبح
آیا در این مورد میتونین برای اثبات حرفتون 3 تا از این برنامه های بزرگ رو مثال بزنین؟

نیازی هست که این کار رو انجام بدم؟
اگه دلیلم غیرمنطقی هست یا روشی که گفتم درست نیست بگید تا اثبات کنم
اگه اشتباهه دلیل اشتباه بودنش رو بگید که من و بقیه یاد بگیریم
فکر نمیکنم گفتن اسم شرکت یا برنامه دلیل اثبات چیزی باشه
زمانی شما میتونید فرمی رو که دیگه نیازی بهش نیست Close هم حافظه نمیگیره هم اینکه فرم کاملا بسته شده و در خیلی از جهات بهتره از اینکه برنامه باز باشه و فرم Hide شده داشته باشه

payman_xxp
شنبه 09 بهمن 1389, 00:23 صبح
سلام
با احترام به همه اساتید و دوستانم عرض شود که استفاده ازformlogin.hide جزو یکی از روشهای غلط در برنامه نویسی و تنبلی برنامه نویس هستش.
-توی main برنامه login را show کنید.
-بعدش چک کنید آیا user pass صحیح هستش یا نه.
-اگر صحیح بود فرم اصلی را run کنید و فرم login را close کنید و اگرکه نه ،فرم لاگین باز میمونه.

هر تعداد فرم یا اسپلش هم که داشته باشید و بخواهید قبل از لاگین نمایش بدین با این روش میشه.
موفق باشید.

tooraj_azizi_1035
شنبه 09 بهمن 1389, 12:41 عصر
سلام،
یکی از کاربرد های کلاس ApplicationContext انجام همین کاری است که شما به آن نیاز دارید. به طور پیش فرض ApplicationContext با رخ دادن رویداد FormClosed اجرای نخ جاری را پایان می دهد. این کلاس برای تعریف دلخواه مراحلی است که پس از طی آنها Message Loop یک Windows Application پایان می پذیرد:



using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;using System.Text;
using System.IO;

namespace MyApplication
{
// A simple form that represents a window in our application
public class AppForm2 : System.Windows.Forms.Form {
public AppForm2(){
this.Size = new System.Drawing.Size(300,300);
this.Text = "AppForm2";
}
}

// A simple form that represents a window in our application
public class AppForm1 : System.Windows.Forms.Form {
public AppForm1(){
this.Size = new System.Drawing.Size(300,300);
this.Text = "AppForm1";
}
}

// The class that handles the creation of the application windows
class MyApplicationContext : ApplicationContext {

private int formCount;
private AppForm1 form1;
private AppForm2 form2;

private Rectangle form1Position;
private Rectangle form2Position;

private FileStream userData;

private MyApplicationContext() { formCount = 0; // Handle the ApplicationExit event to know when the application is exiting.
Application.ApplicationExit += new EventHandler(this.OnApplicationExit);

try {
// Create a file that the application will store user specific data in.
userData = new FileStream(Application.UserAppDataPath + "\\appdata.txt", FileMode.OpenOrCreate);

} catch(IOException e) {
// Inform the user that an error occurred.
MessageBox.Show("An error occurred while attempting to show the application." +
"The error is:" + e.ToString());

// Exit the current thread instead of showing the windows.
ExitThread();
}

// Create both application forms and handle the Closed event
// to know when both forms are closed.
form1 = new AppForm1();
form1.Closed += new EventHandler(OnFormClosed);
form1.Closing += new CancelEventHandler(OnFormClosing);
formCount++;

form2 = new AppForm2();
form2.Closed += new EventHandler(OnFormClosed);
form2.Closing += new CancelEventHandler(OnFormClosing);
formCount++;

// Get the form positions based upon the user specific data.
if (ReadFormDataFromFile()) {
// If the data was read from the file, set the form
// positions manually.
form1.StartPosition = FormStartPosition.Manual;
form2.StartPosition = FormStartPosition.Manual;

form1.Bounds = form1Position;
form2.Bounds = form2Position;
}

// Show both forms.
form1.Show();
form2.Show();
}

private void OnApplicationExit(object sender, EventArgs e) {
// When the application is exiting, write the application data to the
// user file and close it.
WriteFormDataToFile();

try {
// Ignore any errors that might occur while closing the file handle.
userData.Close();
} catch {}
}

private void OnFormClosing(object sender, CancelEventArgs e) {
// When a form is closing, remember the form position so it
// can be saved in the user data file.
if (sender is AppForm1)
form1Position = ((Form)sender).Bounds;
else if (sender is AppForm2)
form2Position = ((Form)sender).Bounds;
}

private void OnFormClosed(object sender, EventArgs e) {
// When a form is closed, decrement the count of open forms.

// When the count gets to 0, exit the app by calling
// ExitThread().
formCount--;
if (formCount == 0) {
ExitThread();
}
}

private bool WriteFormDataToFile(){
// Write the form positions to the file.
UTF8Encoding encoding = new UTF8Encoding();

RectangleConverter rectConv = new RectangleConverter();
String form1pos = rectConv.ConvertToString(form1Position);
String form2pos = rectConv.ConvertToString(form2Position);

byte[] dataToWrite = encoding.GetBytes("~" + form1pos + "~" + form2pos);

try {
// Set the write position to the start of the file and write
userData.Seek(0,SeekOrigin.Begin);
userData.Write(dataToWrite, 0, dataToWrite.Length);
userData.Flush();

userData.SetLength(dataToWrite.Length);
return true;

} catch {
// An error occurred while attempting to write, return false.
return false;
}

}

private bool ReadFormDataFromFile(){
// Read the form positions from the file.
UTF8Encoding encoding = new UTF8Encoding();
String data;

if (userData.Length != 0) {
byte[] dataToRead = new Byte[userData.Length];

try {
// Set the read position to the start of the file and read.
userData.Seek(0, SeekOrigin.Begin);
userData.Read(dataToRead, 0, dataToRead.Length);

} catch (IOException e) {
String errorInfo = e.ToString();
// An error occurred while attempt to read, return false.
return false;
}

// Parse out the data to get the window rectangles
data = encoding.GetString(dataToRead);

try {
// Convert the string data to rectangles
RectangleConverter rectConv = new RectangleConverter();
String form1pos = data.Substring(1,data.IndexOf("~",1)-1);

form1Position = (Rectangle)rectConv.ConvertFromString(form1pos);

String form2pos = data.Substring(data.IndexOf("~",1)+1);
form2Position = (Rectangle)rectConv.ConvertFromString(form2pos);

return true;

} catch {
// Error occurred while attempting to convert the rectangle data.
// Return false to use default values.
return false;
}

} else {
// No data in the file, return false to use default values.
return false;
}
}

[STAThread]
static void Main(string[] args) {

// Create the MyApplicationContext, that derives from ApplicationContext,
// that manages when the application should exit.

MyApplicationContext context = new MyApplicationContext();

// Run the application with the specific context. It will exit when
// all forms are closed.
Application.Run(context);

}
}
}


http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx

saeedjafari
شنبه 09 بهمن 1389, 17:57 عصر
با درود
مممنون از همه دوستان بخصوص آقای "حامد شهبا" بدلیل کمکشون

مشکل حل شد
سپاسگزاری فراوان:تشویق: