ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น

ThaiSEOBoard.comพัฒนาเว็บไซต์Programmingมีปัญหาในการใช้ MDI โดยภาษา C#
หน้า: [1]   ลงล่าง
พิมพ์
ผู้เขียน หัวข้อ: มีปัญหาในการใช้ MDI โดยภาษา C#  (อ่าน 2335 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
MadKiller
Newbie
*

พลังน้ำใจ: 0
ออฟไลน์ ออฟไลน์

กระทู้: 4



ดูรายละเอียด
« เมื่อ: 30 สิงหาคม 2011, 17:04:52 »

//FormMain
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project_1
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            FormLogin f = new FormLogin(this);
            f.Show();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            toolStrip1.Enabled = false;
        }
    }
}

//FormLogin
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace Project_1
{
    public partial class FormLogin : Form
    {
        public FormLogin(FormMain containerForm)
        {
            InitializeComponent();
            MdiParent = containerForm;
        }
        SqlConnection Conn = new SqlConnection();

        private void FormLogin_Load(object sender, EventArgs e)
        {
            DBConnString Connstring = new DBConnString();
            string strConn = Connstring.strConn;

            if (Conn.State == ConnectionState.Open)
            {
                Conn.Close();
            }

            Conn.ConnectionString = strConn;
            Conn.Open();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT * FROM Employee");
            sb.Append(" WHERE (EmpUsername='" + txtUsername.Text.Trim() + "')");
            sb.Append(" AND (EmpPassword='" + txtPassword.Text.Trim() + "')");

            string sqlLogin;
            sqlLogin = sb.ToString();

            SqlCommand com = new SqlCommand();
            SqlDataReader dr;

            com.CommandType = CommandType.Text;
            com.CommandText = sqlLogin;
            com.Connection = Conn;

            dr = com.ExecuteReader();

            if (dr.HasRows)
            {
                this.Hide();
            }
            else
            {
                MessageBox.Show("Username หรือ Password ที่คูณป้อนไม่ถูกต้อง !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtUsername.Text = "";
                txtUsername.Focus();
                txtPassword.Text = "";
            }

            dr.Close();
            dr.Dispose();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

คือจะทำให้คำสั่ง toolStrip1.Enabled = false; ในหน้า FormMain เปลี่ยนเป็น true เมื่อทำการกดปุ่มตกลงใน FormLogin นะครับต้องทำยังไง
ในภาษา VB เคยใช้คำสั่ง FormMenu.ToolStrip1.Enabled = True นี้อ่ะครับโดยการระบุชื่อฟอร์มไว้ด้านหน้าในฟอร์มของ FormLogin
แต่ C# ไม่รู้ว่าต้องใช้คำสั่งว่าไงครับ ต้องเพิ่ม Code ตรงไหนบ้าง
« แก้ไขครั้งสุดท้าย: 30 สิงหาคม 2011, 18:52:07 โดย MadKiller » บันทึกการเข้า
m2studio
ก๊วนเสียว
*

พลังน้ำใจ: 32
ออฟไลน์ ออฟไลน์

กระทู้: 353



ดูรายละเอียด เว็บไซต์
« ตอบ #1 เมื่อ: 30 สิงหาคม 2011, 17:07:21 »

ใช้ this แทนได้ป่าวครับ
this.ToolStrip1.Enabled = True
บันทึกการเข้า

ร้านค้าออนไลน์แห่งใหม่เปิดให้บริการแล้ว

ร้านค้าออนไลน์
Ecommerce
กระเป๋าสะพาย
กระเป๋าแฟชั่น
Hikkie
ก๊วนเสียว
*

พลังน้ำใจ: 16
ออฟไลน์ ออฟไลน์

กระทู้: 325



ดูรายละเอียด
« ตอบ #2 เมื่อ: 30 สิงหาคม 2011, 17:34:36 »

ผมก็ร้างๆ ไปแล้วนะ ลองดูเป็นตัวอย่างนะครับ น่าจะประมาณนี้

   [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Form2 login = new Form2();
        if (login.ShowDialog() == DialogResult.Yes)
        {
            Application.Run(new Form1());
        }
    }




  if (dr.HasRows)
            {
//ถ้า login ผ่าน
this.DialogResult = DialogResult.Yes;
        this.Close();

            }

//ถ้า login ไม่ผ่าน
this.DialogResult = DialogResult.No;
this.Close();
บันทึกการเข้า
MadKiller
Newbie
*

พลังน้ำใจ: 0
ออฟไลน์ ออฟไลน์

กระทู้: 4



ดูรายละเอียด
« ตอบ #3 เมื่อ: 30 สิงหาคม 2011, 18:31:38 »

ขอบคุณทั้ง 2 ท่านมากเลยนะครับ

แต่ยังไม่สามารถทำได้เลยครับ

 Tongue Tongue Tongue
บันทึกการเข้า
execter
ก๊วนเสียว
*

พลังน้ำใจ: 24
ออฟไลน์ ออฟไลน์

กระทู้: 212



ดูรายละเอียด เว็บไซต์
« ตอบ #4 เมื่อ: 30 สิงหาคม 2011, 19:15:06 »

โค๊ด:

    class FormLogin
    {
       public bool Result { get; set }

       private void btnOk_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT * FROM Employee");
            sb.Append(" WHERE (EmpUsername='" + txtUsername.Text.Trim() + "')");
            sb.Append(" AND (EmpPassword='" + txtPassword.Text.Trim() + "')");

            string sqlLogin;
            sqlLogin = sb.ToString();

            SqlCommand com = new SqlCommand();
            SqlDataReader dr;

            com.CommandType = CommandType.Text;
            com.CommandText = sqlLogin;
            com.Connection = Conn;

            dr = com.ExecuteReader();

            if (dr.HasRows)
            {
                this.Hide();

                // เก็บค่าไว้เพื่อใช้ใน FormMain
                this.Result = true;

            }
            else
            {
                MessageBox.Show("Username หรือ Password ที่คูณป้อนไม่ถูกต้อง !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtUsername.Text = "";
                txtUsername.Focus();
                txtPassword.Text = "";

                this.Result = false;

            }

            dr.Close();
            dr.Dispose();
        }
    }


    class FormMain
    {
         public FormMain()
        {
            InitializeComponent();
            FormLogin f = new FormLogin(this);
            f.Show();

            // เช็คค่าที่ส่งมาจาก FormLogin
            if(f.Result) {
                toolStrip1.Enabled = true;               
            }

        }
    }
บันทึกการเข้า

Mubaza Thailand
bonshington
ก๊วนเสียว
*

พลังน้ำใจ: 47
ออฟไลน์ ออฟไลน์

กระทู้: 376



ดูรายละเอียด
« ตอบ #5 เมื่อ: 31 สิงหาคม 2011, 18:06:56 »

ตอบตรงนี้แทนได้มั๊ยอ่ะ
โค๊ด:
DBConnString Connstring = new DBConnString();
string strConn = Connstring.strConn;

if (Conn.State == ConnectionState.Open)
{
  Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();

เละโคด ถ้า conn เปิดอยู่ ก็ให้ปิด แล้วเปิดใหม่ กรณีที่ถ้าไม่ได้ใช้ conenction pooling ไม่เจ๊งหรอคับ ถ้าเจอ multi session?

แนะนำให้เขียนแยกเป็น static class ต่างหาก เหมือนเป็นอีก library นึง แล้วเรียกใช้เอาจะดีกว่า รองรับ multi session

ประมาณนี้

โค๊ด:
public static class db{
  private static string connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["..."].ConnectionString;

  public static ... Execute(...){
   
    // ใช้ interface เพราะถ้าเปลี่ยน db จะได้ไม่ต้องแก้ code
    IDbConnection connection = ...;
    IDbCommand cmd = connection.CreateCommand();

    try{
      cmd.Connection.Open();
      cmd.Execute...;
      return ...;
    }
    finally{
      // ใส่ try catch ด้วยก็ดี ส่วนถ้าเป็นกรณี connection pooling บรรทัดนี้ จะไม่มีผล(pooling ไม่มีปิด conenction)
      cmd.Connection.Close();
    }
  }
}

เวลาใช้ก็ db.Execute(...)

หรือถ้าเป็น .Net 4.0 แล้วอยากให้มันเร็วๆก็
(new Action[] {
  new Action(() => { db.Execute(...1...) }),
  new Action(() => { db.Execute(...2...) }),...
})
.AsParallel()
.ForAll(code => code());

ถ้าจะเอาผลลัพธ์ ก็เปลี่ยนAction เป็น Func<TResult> แล้วหา List มารับ
« แก้ไขครั้งสุดท้าย: 31 สิงหาคม 2011, 18:18:30 โดย bonshington » บันทึกการเข้า
MadKiller
Newbie
*

พลังน้ำใจ: 0
ออฟไลน์ ออฟไลน์

กระทู้: 4



ดูรายละเอียด
« ตอบ #6 เมื่อ: 31 สิงหาคม 2011, 19:54:05 »

พึ่งเขียน C# ได้อาทิตย์เดียวเองครับ

ยังไม่ค่อยเข้าใจโครงสร้างของมันเลย

ยังไงก็ขอบคุณครับที่แนะนำแล้วก็ยังทำอันเก่าไม่ได้เลยครับ

ขอความช่วยเหลือหน่อยนะครับสำหรับคำตอบที่ได้รับมาทุกอย่าง

ผมจะลองเอาไปศึกษาดูเพิ่มเติมแล้ว Apply ใช่ดูนะครับ

 wanwan011 wanwan011 wanwan011
บันทึกการเข้า
bonshington
ก๊วนเสียว
*

พลังน้ำใจ: 47
ออฟไลน์ ออฟไลน์

กระทู้: 376



ดูรายละเอียด
« ตอบ #7 เมื่อ: 01 กันยายน 2011, 01:36:49 »

C# เป็นภาษาเชิงวัตถุ (OOP) ถ้าเราคิดเชิงวัตถุ จะสามาระสร้าง app ที่โครงสร้างดีๆได้ เช่น

โค๊ด:
 private void btnOk_Click(object sender, EventArgs e)
       {
            StringBuilder sb = สร้าง query

           SqlCommand com = สร้าง sql

            dr = com.ExecuteReader();

            if (dr.HasRows)
            {
                this.Hide();

                // เก็บค่าไว้เพื่อใช้ใน FormMain
                this.Result = true;

            }
            else
            {
               ใส่ค่า

            }

            dr.Close();
            dr.Dispose();
        }
    }
จะเห็นว่า เป็นการทำตามขั้น(procedural) 1,2,3 มันไม่ถูกหลัก OOP โดยการมองเชิง OOP จะพยายามคิดให้ทุกอย่างแยกจากกัน เช่น sql ก็แยก(class)ไปต่างหากเลย แล้วมี method call ทีเดียว

ปล string query = string.Format("SELECT * FROM Employee WHERE EmpUsername='{0}' AND EmpPassword='{1}' ", txtUsername.Text.Trim(), txtPassword.Text.Trim());

นอกจาก trim แล้ว ควรเพิ่ม txtPassword.Text.Trim().Replace("'", "''''"); ต่อท้าย เพื่อกัน sql injection
       
บันทึกการเข้า
MadKiller
Newbie
*

พลังน้ำใจ: 0
ออฟไลน์ ออฟไลน์

กระทู้: 4



ดูรายละเอียด
« ตอบ #8 เมื่อ: 02 กันยายน 2011, 14:15:29 »

ขอบคุณครับที่แนะนำแต่ตอนนี้อยากได้อันแรกจริงๆครับ

โปรแกรมยังไม่ไปไหนเลยทำอันแรกยังไม่ได้เลย

 Cry Cry Cry
บันทึกการเข้า
หน้า: [1]   ขึ้นบน
พิมพ์