'2011-03-24 Support Windows Client & All Application. example delphi, visual foxpro, etc
'Dim s As String = FormsAuthentication.GetRedirectUrl("userName", False)
'Result = "/default.aspx"
If Request.QueryString("UserName") <> Nothing AndAlso Request.QueryString("Password") <> Nothing Then
Dim strUserName As String = Request.QueryString("UserName") '***** byebye Decrypt
Dim strPassword As String = Request.QueryString("Password") '***** byebye Decrypt
' ******************************************************************************************
' Protect Decomplier Algorithm with C++ for Encrypt UserName and Password
' validation here and check the username and password in the database
' *******************************************************************************************
If FormsAuthentication.Authenticate(strUserName, strPassword) Then 'Check in web.config with SHA1/More
FormsAuthentication.SetAuthCookie(strUserName, False, FormsAuthentication.FormsCookiePath)
End If
FormsAuthentication.RedirectFromLoginPage(strUserName, False, FormsAuthentication.FormsCookiePath)
' Create cookie and return it
Dim ticket As New FormsAuthenticationTicket(1,
strUserName,
DateTime.Now,
DateTime.Now.AddDays(7),
False,
New Guid().ToString(),
FormsAuthentication.FormsCookiePath)
' Encrypt the ticket.
Dim encTicket As String = FormsAuthentication.Encrypt(ticket) '& "|" & strUserName
'Fixed bug Redirect (For Windows Application)
Response.RedirectPermanent("Default.aspx?EncryptCookieToWebClient=" & encTicket) 'Root
End If
ผมขอถามหน่อยว่า ผมจะส่งพารามิเตอร์ generic List(Of T) ให้กับ User Control มันต้องทำอย่างไร?
อันนี้ Source Code
Code (VB.NET)
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
<ToolboxBitmap(GetType(TextBox))> _
Public Class usrCtrlTextBoxButton : Inherits TextBox
Private ReadOnly _button As Button 'ปุ่มค้นหา
Private Property _TextBox As TextBox 'แสดงผลลัพธ์จาการค้นหา (Memory/Database)
Private _lst As IList 'generic พารามิเตอร์
<Browsable(True), _
EditorBrowsable(EditorBrowsableState.Always), _
Description("The items with sub items that should be displayed"), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
<System.ComponentModel.DefaultValue(GetType(IList), ""), System.ComponentModel.Category("Appearance")> _
Public Property lst As IList 'IEnumerable(Of Object)() / Any Type
Get
Return _lst 'ไอ้ตรงนี้แหละ ยังทำไม่ได้
End Get
Set(value As IList)
_lst = value 'ไอ้ตรงนี้แหละ ยังทำไม่ได้
End Set
End Property
Public Sub New()
_button = New Button() With {.Cursor = Cursors.Hand, .TabStop = False}
AddHandler _button.SizeChanged, Sub(o, e)
OnResize(e)
End Sub
AddHandler _button.Click, AddressOf btn_Click
Me.Controls.Add(_button)
End Sub
Public Custom Event ButtonClick As EventHandler
AddHandler(value As EventHandler)
AddHandler _button.Click, value
End AddHandler
RemoveHandler(value As EventHandler)
RemoveHandler _button.Click, value
End RemoveHandler
RaiseEvent(sender As Object, e As EventArgs)
'TODO
End RaiseEvent
End Event
''' <summary>
''' Find Data in Memory/Database.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub btn_Click(sender As Object, e As EventArgs)
MsgBox("Click in WL_Controls.usrCtrlTextBoxButton")
Me.Focus()
End Sub
''' <summary>
''' Display Description from Search.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<System.ComponentModel.DefaultValue(GetType(TextBox), "TextCharacter"), System.ComponentModel.Category("Appearance")> _
Public Property TextBox() As TextBox
Get
Return _TextBox
End Get
Set(ByVal value As TextBox)
_TextBox = value
End Set
End Property
Protected Overrides Sub OnResize(e As EventArgs)
MyBase.OnResize(e)
_button.Size = New Size(28, Me.ClientSize.Height + 2)
_button.Image = Global.WL_Controls.My.Resources.Resources.PersonWoman2
_button.Location = New Point(Me.ClientSize.Width - _button.Width - 1, -1)
' Send EM_SETMARGINS to prevent text from disappearing underneath the button
SendMessage(Me.Handle, &HD3, New System.IntPtr(2), New System.IntPtr(_button.Width << 16))
End Sub
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Private Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wp As IntPtr, lp As IntPtr) As IntPtr
End Function
''' <summary>
''' ค้นหาข้อมูลจาก Memory/Database
''' </summary>
''' <param name="e"></param>
''' <remarks></remarks>
Protected Overrides Sub OnTextChanged(e As EventArgs)
Dim found As Boolean = True
If 1 = 1 Then 'Search in Memory
Else 'Search in Database Select * From Where ก็ว่ากันไป
End If
If found Then
Me.TextBox.Text = Me.Text
Else
Me.TextBox.Text = String.Empty
End If
MyBase.OnTextChanged(e)
End Sub
Public Enum TextBoxDisplayManager As Byte
TextCharacter = 0
TextNumber = 1
TextInteger = 2
End Enum
Public Enum TextBoxDisplayPrecisionScale As Byte
None = 0
One = 1
Two = 2
Tree = 3
Four = 4
Five = 5
Six = 6
Seven = 7
Eight = 8
Nine = 9
Ten = 10
End Enum
Public Enum TextBoxDisplayOnEnterMoveNext As Byte
Yes
No
End Enum
Public Enum TextBoxDisplayIsEnabledBinding As Byte
Yes
No
End Enum
#Region " Variables "
Private _DisplayManager As TextBoxDisplayManager
Private _DisplayPrecisionScale As TextBoxDisplayPrecisionScale
Private _DisplayOnEnterMoveNext As TextBoxDisplayOnEnterMoveNext
Private _DisplayIsEnabledBinding As TextBoxDisplayIsEnabledBinding
#End Region
#Region " Properties "
<System.ComponentModel.DefaultValue(GetType(TextBoxDisplayManager), "TextCharacter"), System.ComponentModel.Category("Appearance")> _
Public Property DisplayManager() As TextBoxDisplayManager
Get
Return _DisplayManager
End Get
Set(ByVal value As TextBoxDisplayManager)
_DisplayManager = value
If Me._DisplayManager = TextBoxDisplayManager.TextNumber Then
MyBase.TextAlign = HorizontalAlignment.Right
End If
End Set
End Property
<System.ComponentModel.DefaultValue(GetType(TextBoxDisplayPrecisionScale), "None"), System.ComponentModel.Category("Appearance")> _
Public Property DisplayPrecisionScale() As TextBoxDisplayPrecisionScale
Get
Return _DisplayPrecisionScale
End Get
Set(ByVal Value As TextBoxDisplayPrecisionScale)
_DisplayPrecisionScale = Value
If Me._DisplayManager = TextBoxDisplayManager.TextNumber Then
MyBase.TextAlign = HorizontalAlignment.Right
Dim dblSubMybaseText As Double
If String.IsNullOrEmpty(MyBase.Text) OrElse Not IsNumeric(MyBase.Text) Then
dblSubMybaseText = 0
Else
dblSubMybaseText = CDbl(MyBase.Text)
End If
FormatText(dblSubMybaseText)
ElseIf Me.DisplayManager = TextBoxDisplayManager.TextInteger Then
MyBase.Text = "0"
Else
MyBase.Text = String.Empty
End If
End Set
End Property
<System.ComponentModel.DefaultValue(GetType(TextBoxDisplayOnEnterMoveNext), "Yes"), System.ComponentModel.Category("Appearance")> _
Public Property DisplayOnEnterMoveNext() As TextBoxDisplayOnEnterMoveNext
Get
Return _DisplayOnEnterMoveNext
End Get
Set(ByVal value As TextBoxDisplayOnEnterMoveNext)
_DisplayOnEnterMoveNext = value
End Set
End Property
<System.ComponentModel.DefaultValue(GetType(TextBoxDisplayIsEnabledBinding), "Yes"), System.ComponentModel.Category("Appearance")> _
Public Property DisplayIsEnabledBinding() As TextBoxDisplayIsEnabledBinding
Get
Return _DisplayIsEnabledBinding
End Get
Set(ByVal value As TextBoxDisplayIsEnabledBinding)
_DisplayIsEnabledBinding = value
End Set
End Property
#End Region
'Protected Overrides Sub OnBindingContextChanged(ByVal e As System.EventArgs)
' MyBase.OnBindingContextChanged(e)
'End Sub
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
If Me._DisplayManager = TextBoxDisplayManager.TextNumber Then
If e.KeyChar < "0" Or e.KeyChar > "9" Then
If AscW(e.KeyChar) = Keys.Back Then
e.Handled = False
Else
If e.KeyChar = "." And MyBase.Text.Contains(".") = False Then
e.Handled = False
ElseIf e.KeyChar = "-" And MyBase.Text.Contains("-") = False Then
e.Handled = False
Else
e.Handled = True
End If
End If
End If
ElseIf Me._DisplayManager = TextBoxDisplayManager.TextInteger Then
If e.KeyChar < "0" Or e.KeyChar > "9" Then
If AscW(e.KeyChar) = Keys.Back Then 'Backspace
e.Handled = False
Else
e.Handled = True
End If
End If
End If
If AscW(e.KeyChar) = Keys.Enter AndAlso _DisplayOnEnterMoveNext = TextBoxDisplayOnEnterMoveNext.Yes Then
SendKeys.Send("{TAB}")
End If
End Sub
Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
MyBase.SelectionStart = 0
MyBase.SelectionLength = MyBase.Text.Length
End Sub
Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
If Me._DisplayManager = TextBoxDisplayManager.TextNumber Then
Dim dblSubMybaseText As Double
If String.IsNullOrEmpty(MyBase.Text) OrElse Not IsNumeric(MyBase.Text) Then
dblSubMybaseText = 0
Else
dblSubMybaseText = CDbl(MyBase.Text)
End If
FormatText(dblSubMybaseText)
End If
End Sub
Public Sub ResetValue()
If Me._DisplayManager = TextBoxDisplayManager.TextNumber Then
FormatText(0)
ElseIf Me.DisplayManager = TextBoxDisplayManager.TextInteger Then
MyBase.Text = "0"
Else
MyBase.Text = String.Empty 'Character
End If
End Sub
Private Sub FormatText(ByVal dblSubMybaseText As Double)
Select Case Me._DisplayPrecisionScale
Case TextBoxDisplayPrecisionScale.None
MyBase.Text = String.Format("{0:N0}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.One
MyBase.Text = String.Format("{0:N1}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Two
MyBase.Text = String.Format("{0:N2}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Tree
MyBase.Text = String.Format("{0:N3}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Four
MyBase.Text = String.Format("{0:N4}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Five
MyBase.Text = String.Format("{0:N5}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Six
MyBase.Text = String.Format("{0:N6}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Seven
MyBase.Text = String.Format("{0:N7}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Eight
MyBase.Text = String.Format("{0:N8}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Nine
MyBase.Text = String.Format("{0:N9}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Ten
MyBase.Text = String.Format("{0:N10}", dblSubMybaseText)
Case Else
MyBase.Text = String.Format("{0:N0}", dblSubMybaseText)
End Select
End Sub
'ปิดเสียง ปี๊ด เมื่อกดปุ่ม Enter
'Private Sub SetBeepOff(ByVal senser As Control, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
' If e.KeyCode = Keys.Enter Then
' e.SuppressKeyPress = True
' SendKeys.Send("{Tab}")
' End If
'End Sub
'Protected Overrides Sub OnBindingContextChanged(ByVal e As System.EventArgs)
' MyBase.OnBindingContextChanged(e)
'End Sub
End Class
'How To Install SignalR Client
'PM> Install-Package SignalR.Client -Version 0.6.0
'Install-Package Microsoft.AspNet.SignalR.Client -pre
'2013-05-14
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Threading.Tasks
Imports Microsoft.AspNet.SignalR.Client
Imports Microsoft.AspNet.SignalR.Client.Hubs
Imports System.DirectoryServices
Imports System.DirectoryServices.AccountManagement
Public Class frmMain : Inherits Form
Private Delegate Sub SetTextCallback(text As String)
Private _connectionPersistent As Connection 'PersistentConnection
'Private _connectionHub As HubConnection() 'HubConnection
Private _ChatData As New ChatData()
Shared ConnectedUsers As New List(Of UserDetail)()
Shared CurrentMessage As New List(Of MessageDetail)() 'Class/MessageDetail.vb
Dim Fmt As New StringFormat() With {.LineAlignment = StringAlignment.Center}
Public Sub New()
InitializeComponent()
'ระวัง Version Client และ Server ทุกฯตัว
'ระวังติด <authentication mode="Forms">
'ระวังติด <authorization>
'Domain : www.goodluck.smpksme.com (จะต้องไม่มี http:// นำหน้า)
'SignalR Website : http://www.xxx.yyy.com/echo
'Authenticate Website : http://www.xxx.yyy.com/CreateASPXAUTHCookie.aspx
'
End Sub
Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
Dim ss As New Bitmap(txtDomain.Width, txtDomain.Height)
txtDomain.DrawToBitmap(ss, New Rectangle(0, 0, txtDomain.Width, txtDomain.Height))
MyBase.OnPaint(e)
End Sub
Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'TextBox1.Text = "_connection between Head Office and branch, Branch to Branch."
End Sub
Private Sub frmMain_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
btnSend.PerformClick()
End If
End Sub
#Region "ConnectToSignalR"
Private Sub UrlSet_Click(sender As System.Object, e As System.EventArgs) Handles UrlSet.Click
txtDomain.Text = "www.xxx.yyy.com"
txtSignalWebsite.Text = "http://www.xxx.yyy.com/echo"
'txtAuthWebsite.Text = "http://www.xxx.yyy.com/CreateASPXAUTHCookie.aspx"
txtAuthWebsite.Text = "http://www.xxx.yyy.com/LoginG.aspx"
'~
'Real Setup
'Domain : www.xxx.yyy.com (จะต้องไม่มี http:// นำหน้า)
'SignalR Website : http://www.xxx.yyy.com/echo
'Authenticate Website : http://www.xxx.yyy.com/CreateASPXAUTHCookie.aspx
'2013-16-10 Change to
'Authenticate Website : http://www.xxx.yyy.com/LoginG.aspx
'Web.config
'<authorization>
' <deny users="?" />
'</authorization>
'deny = * means deny everyone
'deny = ? means deny unauthenticated users
'allow users="dan,matthew"
End Sub
'ระวัง
'Auten
'Error : Too many automatic redirections were attempted.
Private Sub btnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
Me.Cursor = Cursors.WaitCursor
Try
Call ConnectToSignalR()
btnSend.Enabled = True
btnDisconnect.Enabled = True
btnConnect.Enabled = False
PictureBox1.Image = Global.WL_SignalRClient.My.Resources.Resources.cn01
Catch ex As Exception
PictureBox1.Image = Global.WL_SignalRClient.My.Resources.Resources.cn02
MsgBox("Error : " & ex.Message) 'MessageBox.Show(ex.InnerException.Message)
End Try
Me.Cursor = Cursors.Default
End Sub
Private Sub ConnectToSignalR(Optional ByVal intExpired As Integer = 480)
Dim varDomainName As String = If(txtDomain.Text.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase), txtDomain.Text.Substring(7), txtDomain.Text)
' Connect to the Server service
_connectionPersistent = New Connection(txtSignalWebsite.Text) 'Microsoft.AspNet.SignalR.Client
' Fire _connection_Received when message comes in
AddHandler _connectionPersistent.Received, AddressOf _connection_Received
' Create a Cookie
Dim objCookie As New Cookie()
' Set the values
objCookie.Domain = varDomainName 'If(txtDomain.Text.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase), txtDomain.Text.Substring(7), txtDomain.Text)
objCookie.Expires = DateTime.Now.AddMinutes(intExpired) ' 8 ชั่วโมงการทำงาน
'objCookie.HttpOnly = False 'Default False
objCookie.Name = "ASPXAUTH"
'สำคัญเหมือนกัน
'objCookie.Path = "~/"
objCookie.Path = "/"
objCookie.Secure = False
objCookie.Value = GetCookie()
' The Forms Auth Ticket. Create a Cookie container and put the Cookie inside
_connectionPersistent.CookieContainer = New CookieContainer()
_connectionPersistent.CookieContainer.Add(objCookie)
' Start the _connection
'_connection.GroupsToken = txtGroupName.Text 'ใส่ตัวนี้แล้ว Windows Client รับ Message ไม่ได้งงฉิบหาย
'_connectionPersistent.GroupsToken
'_connectionPersistent.Credentials = CredentialCache.DefaultCredentials
_connectionPersistent.Start().Wait()
Try
If _connectionPersistent.ConnectionId IsNot Nothing Then
'Send UserName And GroupName
Call SendData(txtGroupName.Text, txtUserName.Text, String.Empty, "NewUser", "S", String.Empty) 'Fixed SignalR send the group name on connected.
If ConnectedUsers.Find(Function(x) x.ConnectionId = _connectionPersistent.ConnectionId) Is Nothing Then
ConnectedUsers.Add(New UserDetail() With {.ConnectionId = _connectionPersistent.ConnectionId, .UserName = txtUserName.Text, .GroupName = txtGroupName.Text})
End If
End If
Catch ex As Exception
'TODO
End Try
End Sub
'More...
'More...
'More...
End Class
ปล. C++ ผมเขียนไม่เป็นหรอก ผมโม้ไปอย่างนั้นแหละ (ผมให้น้องเขาเขียนให้)
Private Sub FormatText(ByVal dblSubMybaseText As Double)
Select Case Me._DisplayPrecisionScale
Case TextBoxDisplayPrecisionScale.None
MyBase.Text = String.Format("{0:N0}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.One
MyBase.Text = String.Format("{0:N1}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Two
MyBase.Text = String.Format("{0:N2}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Tree
MyBase.Text = String.Format("{0:N3}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Four
MyBase.Text = String.Format("{0:N4}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Five
MyBase.Text = String.Format("{0:N5}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Six
MyBase.Text = String.Format("{0:N6}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Seven
MyBase.Text = String.Format("{0:N7}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Eight
MyBase.Text = String.Format("{0:N8}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Nine
MyBase.Text = String.Format("{0:N9}", dblSubMybaseText)
Case TextBoxDisplayPrecisionScale.Ten
MyBase.Text = String.Format("{0:N10}", dblSubMybaseText)
Case Else
MyBase.Text = String.Format("{0:N0}", dblSubMybaseText)
End Select
End Sub
มันต้องเป็นแบบนี้ Code (VB.NET)
Private Sub FormatText(ByVal dblSubMybaseText As Double)
MyBase.Text = String.Format("{0:N" & Me._DisplayPrecisionScale & "}", dblSubMybaseText)
End Sub
แบบนี้แหล่มครับพี่
User ใช้ง่ายขึ้นเยอะ(เอาใจใส่ user)
คนเขียนโปรแกรมอาจจะไม่รู้
แต่ user ส่วนใหญ่มักจะมีข้อขัดแย้งเวลาใช้งานโปรแกรมบางโปรแกรม
บางโปรแกรมก็เหมือนขายยาเบื่อให้ User กิน(จน User ต้องเบื่อไปเลย)
บางครั้งการใช้งานโปรแกรมก็เหมือนต้องฝืนทำ
เป็นเหตุผลที่บางโปรแกรมที่ราคา 5 ล้านกว่าๆแต่ก็ยังต้องเขียนซ่อมทีละแสนอยู่ตลอดเวลา
ดังนั้นทุกโปรแกรมที่ผมเขียนจึงอ้างถึง user แต่ละคน(ดึงเอา อัตลักษณ์ ของ User มาใช้)
ผลคือ user ไม่บ่นว่าใช้ยากอีกต่อไป เพียงแค่ อัพ ขึ้นระบบ User ก็ทำงานต่อได้ทันทีโดยไม่ต้องมานั่งสอนกันอีก(ผมก็แค่เอาชีวิตประจำวันของ user มาจัดเรียงในรูปแบบโค้ด)
var bb;
function po(){bb=ro();}
function ro(){return parseInt(so("floatnumber",6));}
function so(ac, bc)
{
var cc = oo(ac);
return (cc === false)?bc:cc;
}
function oo(kb)
{
if (document.cookie.length > 0)
{
ob = document.cookie.indexOf(kb + "=");
if (ob != -1)
{
ob = ob + kb.length + 1;
pb = document.cookie.indexOf(";", ob);
if (pb == -1) pb = document.cookie.length;
return unescape(document.cookie.substring(ob, pb));
}
}
return "";
}
function uo(_b,X){X=(!X?6:X);return Math.round(_b*Math.pow(10,X))/Math.pow(10,X);}