|  | 
	                
  
    |  |  
    | 
        
        ทำการกำหนดสิทธิ์ในการเลือกใช้ ToolStripButton ไว้ในฟอร์มล็อกอืน ไม่เห็นมีอะไรเกิดขึ้น?     |  
    |  |  
 
	
		|  |  |  |  |  
		|  |  | 
          
            | Code (VB.NET) 
 Imports System.Data
Imports System.Data.OleDb
Module Module1
    Public con As OleDbConnection
    Public da As OleDbDataAdapter
    Public ds As DataSet
    Public cmd As OleDbCommand
    Public rd As OleDbDataReader
    Public str As String
    Public Sub connection()
        str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\admim\Documents\ฐานข้อมูล5.accdb"
        con = New OleDbConnection(str)
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
    End Sub
End Module
Public Class frmlogin
    Private Sub Login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        connection()
        If (txtuser.Text = "") Then
            MessageBox.Show("กรุณากรอกUsername", "Login", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            txtuser.Focus()
            Exit Sub
        End If
        If (txtpass.Text = "") Then
            MessageBox.Show("กรุณากรอกPassword", "Login", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            txtpass.Focus()
            Exit Sub
        End If
        If txtuser.Text = "user1" And txtpass.Text = "1234" Then
            MessageBox.Show("คุณคือ user1", "Login", MessageBoxButtons.OK)
            Call user1login()
        ElseIf txtuser.Text = "user2" And txtpass.Text = "4567" Then
            MessageBox.Show("คุณคือ user2", "Login", MessageBoxButtons.OK)
            Call user2login()
        ElseIf txtuser.Text = "user3" And txtpass.Text = "7890" Then
            MessageBox.Show("คุณคือ user3", "Login", MessageBoxButtons.OK)
            Call user3login()
        Else
            MsgBox("username หรือ password ไม่ถูกต้อง")
        End If
    End Sub
    Private Sub user1login()
        Dim frmpro As New frmproject
        frmproject.Show()
        Me.Hide()
        frmpro.ToolStripButton1.Visible = True
        frmpro.ToolStripButton2.Visible = False
        frmpro.ToolStripButton3.Visible = False
        frmpro.ToolStripButton4.Visible = True
    End Sub
    Private Sub user2login()
        Dim frmpro As New frmproject
        frmproject.Show()
        Me.Hide()
        frmpro.ToolStripButton1.Visible = False
        frmpro.ToolStripButton2.Visible = True
        frmpro.ToolStripButton3.Visible = False
        frmpro.ToolStripButton4.Visible = True
    End Sub
    Private Sub user3login()
        Dim frmpro As New frmproject
        frmproject.Show()
        Me.Hide()
        frmpro.ToolStripButton1.Visible = False
        frmpro.ToolStripButton2.Visible = True
        frmpro.ToolStripButton3.Visible = True
        frmpro.ToolStripButton4.Visible = True
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If MessageBox.Show("คุณต้องการออกจากระบบ?", "Login", MessageBoxButtons.YesNo) = DialogResult.Yes Then
            Application.Exit()
        End If
        'ใช้แทน me.close() เพราะต้องการมีกล่องข้อความถามว่า คุณต้องการออกจากระบบ?
    End Sub
คือเมื่อuser1 loginเข้ามา มีแต่.ToolStripButton1กับ.ToolStripButton4ที่ใช้งานได้
 
 
 
 Tag : .NET, Ms Access, VS 2012 (.NET 4.x)
 
 
 |  
            | 
 ประวัติการแก้ไข
 2015-09-01 15:13:45
 |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2015-09-01 15:13:17 | By :
                          peemes101 | View :
                          1121 | Reply :
                          11 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |  
		|  |  |  |  |  
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | โค้ดน่าจะโอเคแล้วนะครับ ไม่ได้ตรงไหนเหรอครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-09-01 15:48:56 | By :
                            Freedom |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | คงต้องเอาโค้ดของฟอร์ม frmproject  มาดูนะครับ หรือมั้ยก็ส่งค่า รหัสไปให้อีกแล้วตรวจสอบในอีกฟอร์มน่าจะดีกว่านะครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-09-01 16:18:04 | By :
                            Mi |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | มันไม่เป็นในสิ่งที่ต้องการคือ เมื่อ user1เข้ามามันกดToolStripButtonได้ทุกปุ่มเลย
 แต่สิ่งที่ต้องการคือกดที่เฉพาะToolStripButton1กับToolStripButton4เท่านั้น ส่วนToolStripButtonที่เหลือเป็นenable
 และเมื่อกดที่ToolStripButton มันโชว์ฟอร์มเดียวเท่านั้น โดยไม่ซ้อนกัน
 
 
 อันนี้เป็นโค้ดฟอร์มfrmproject
 Code (VB.NET)
 
 Imports System.Data
Imports System.Data.OleDb
Public Class frmproject
    Private Sub frmproject_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If Not (Me.ActiveMdiChild Is Nothing) Then
            Me.ActiveMdiChild.Close()
        End If
        
    End Sub
    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        Dim frm As Form 'ให้ frm เป็น Form ธรรมดา
        'ค้นหาฟอร์มลูกทั้งหมดก่อนว่ามี Form1 หรือยัง
        For Each frm In Me.MdiChildren
            If (TypeOf frm Is Form1) Then
                frm.Activate() 'ถ้าพบ สั่ง Activate เพื่อให้วินโดว์นั้นแสดงขึ้นมาอยู่ข้างบนสุด
                Exit Sub       'บรรลุความประสงค์ของ procedure นี้แล้ว.
            End If
        Next
        Dim f1 As New Form1 'ให้สร้างอ๊อบเจ็กต์ฟอร์มใหม่ เพื่อเปิดฟอร์มตามปกติ
        f1.MdiParent = Me   'กำหนดให้เป็นฟอร์มลูกของฟอร์มปัจจุบัน
        f1.Show()           'สั่งให้แสดงบนหน้าจอ
        
    End Sub
    Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
        Dim frm1 As Form 'ให้ frm เป็น Form ธรรมดา
        'ค้นหาฟอร์มลูกทั้งหมดก่อนว่ามี Form1 หรือยัง
        For Each frm1 In Me.MdiChildren
            If (TypeOf frm1 Is Form2) Then
                frm1.Activate() 'ถ้าพบ สั่ง Activate เพื่อให้วินโดว์นั้นแสดงขึ้นมาอยู่ข้างบนสุด
                Exit Sub       'บรรลุความประสงค์ของ procedure นี้แล้ว.
            End If
        Next
        Dim f2 As New Form2 'ให้สร้างอ๊อบเจ็กต์ฟอร์มใหม่ เพื่อเปิดฟอร์มตามปกติ
        f2.MdiParent = Me   'กำหนดให้เป็นฟอร์มลูกของฟอร์มปัจจุบัน
        f2.Show()           'สั่งให้แสดงบนหน้าจอ
        
    End Sub
    Private Sub ToolStripButton3_Click(sender As Object, e As EventArgs) Handles ToolStripButton3.Click
        Dim frm2 As Form 'ให้ frm เป็น Form ธรรมดา
        'ค้นหาฟอร์มลูกทั้งหมดก่อนว่ามี Form1 หรือยัง
        For Each frm2 In Me.MdiChildren
            If (TypeOf frm2 Is Form3) Then
                frm2.Activate() 'ถ้าพบ สั่ง Activate เพื่อให้วินโดว์นั้นแสดงขึ้นมาอยู่ข้างบนสุด
                Exit Sub       'บรรลุความประสงค์ของ procedure นี้แล้ว.
            End If
        Next
        Dim f3 As New Form3 'ให้สร้างอ๊อบเจ็กต์ฟอร์มใหม่ เพื่อเปิดฟอร์มตามปกติ
        f3.MdiParent = Me   'กำหนดให้เป็นฟอร์มลูกของฟอร์มปัจจุบัน
        f3.Show()           'สั่งให้แสดงบนหน้าจอ
    End Sub
    Private Sub ToolStripButton4_Click(sender As Object, e As EventArgs) Handles ToolStripButton4.Click
        MessageBox.Show("ออกจากระบบ", "Exit", MessageBoxButtons.OK)
Me.Close()
    End Sub
 |  
              | 
 ประวัติการแก้ไข
 2015-09-02 00:27:51
 2015-09-02 00:39:34
 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-09-01 21:20:21 | By :
                            peemes101 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ลองสร้าง form ใหม่มาครับ 
 น่าจะมาจากตั้งค่าไรซักอย่าง
 ผมเอาโค๊ดไปเล่นดูใช้ได้หมดครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-09-02 10:14:16 | By :
                            lamaka.tor |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอบคุณคุณ Freedom มากๆ ตอนนี้สามารถทำได้แล้ว 
 แต่อยากถามอีกอย่างคือ
 
 มันติดที่เมื่อuser2 สามารถกดปุ่ม ToolStripButton2กับ ToolStripButton4ได้
 ซึ่งจะโชว์ฟอร์มย่อยขึ้นมา ซึ่งถ้ากด ToolStripButton2อยู่ แล้วจะเปลี่ยนไปกด ToolStripButton4 ฟอร์มมันจะซ้อนกันและฟอร์มย่อยมันเลื่อนได้
 
 โดยสิ่งที่ต้องการคือ ถ้ากด ToolStripButton2อยู่ แล้วจะเปลี่ยนไปกด ToolStripButton4
 อยากให้โชว์แค่ฟอร์มของปุ่ม ToolStripButton4เท่านั้น ส่วน ฟอร์มของปุ่มToolStripButton2 ก็ปิดไป และฟอร์มย่อยไม่สามารถเลื่อนได้
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-09-02 15:47:08 | By :
                            peemes101 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | Code (VB.NET) 
 Public Class Form3
    Private Sub Form3_Move(sender As Object, e As EventArgs) Handles MyBase.Move
        If Not (Me.Location.X = 0 And Me.Location.Y = 0) Then
            Me.Location = New Point(0, 0)
        End If
    End Sub
End Class
 Code (VB.NET)
 
 Public Class Form2
    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        OpenFrm()
    End Sub
    Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
    End Sub
    Private Sub ToolStripButton3_Click(sender As Object, e As EventArgs) Handles ToolStripButton3.Click
        OpenFrm()
    End Sub
    Private Sub OpenFrm()
        If Not (Me.ActiveMdiChild Is Nothing) Then
            Me.ActiveMdiChild.Close()
        End If
        Dim frm As New Form3
        frm.MdiParent = Me
        frm.StartPosition = FormStartPosition.Manual
        frm.Location = New Point(0, 0)
        frm.Show()
    End Sub
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.IsMdiContainer = True
        If Not (Me.ActiveMdiChild Is Nothing) Then
            Me.ActiveMdiChild.Close()
        End If
    End Sub
End Class
 ประมาณนี้เอาไปปรับใช้นะครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-09-02 16:49:55 | By :
                            Mi |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ใช้ไม่ได้อะ โดยมันก็ยังซ้อนกันอยู่ดีและเลื่อนได้อยู่ดี 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-09-02 22:13:31 | By :
                            peemes101 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | งั้นเอางี้นะ Code (VB.NET)
 
 Private Sub Form3_Move(sender As Object, e As EventArgs) Handles MyBase.Move
        If Not (Me.Location.X = 0 And Me.Location.Y = 0) Then
            Me.Location = New Point(0, 0)
        End If
End Sub
 โค้ดนี้ใช้ในฟอร์มลูกนะครับมันจะสั่งเมื่อมีการเลื่อนฟอร์มลูกไปที่ตำแหน่งอื่นให้ไปต้ำแหน่ง 0,0 นะครับ
 
 Code (VB.NET)
 
 If Not (Me.ActiveMdiChild Is Nothing) Then
            Me.ActiveMdiChild.Close()
        End If
 โค้ดนี้เอาไว้ในฟอร์มแม่นะครับ มันจะทำการปิดฟอร์มลูกที่ถูกเลือกอยู่ถ้าจะเปิดที่ละฟอร์มใช้อันนี้ก็ได้แต่ถ้าไม่มั่นใจใช้อันล่างนะครับ
 
 Code (VB.NET)
 
 For Each frmc As Form In Me.MdiChildren
            frmc.Close()
Next
 โค้ดนี้จะค้นหาฟอร์มลูกทั้งหมดที่อยู่ในฟอร์มแม่แล้วทำการปิดนะครับ
 
 ไม่เข้าใจตรงไหนถามได้นะครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-09-02 22:38:04 | By :
                            Mi |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ทำได้แล้ว ขอบคุณคุณGuest มากๆ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-09-03 22:10:09 | By :
                            peemes101 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              |  
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-09-04 09:29:29 | By :
                            mr.win |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  |  |