Client/MainForm.cs
author sl
Fri, 03 Oct 2014 18:43:55 +0200
changeset 65 464486b81635
parent 63 cd9924457275
child 67 6e50baf5a811
permissions -rw-r--r--
Adding current client concept.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 using System.Windows.Forms;
    10 using System.ServiceModel;
    11 using System.ServiceModel.Channels;
    12 using System.Diagnostics;
    13 using SharpDisplay;
    14 
    15 
    16 namespace SharpDisplayClient
    17 {
    18     public partial class MainForm : Form
    19     {
    20         Client iClient;
    21         Callback iCallback;
    22         ContentAlignment Alignment;
    23         TextField iTextFieldTop;
    24 
    25         public MainForm()
    26         {
    27             InitializeComponent();
    28             Alignment = ContentAlignment.MiddleLeft;
    29             iTextFieldTop = new TextField(0);
    30         }
    31 
    32 
    33         private void MainForm_Load(object sender, EventArgs e)
    34         {
    35             iCallback = new Callback(this);
    36             iClient = new Client(iCallback);
    37 
    38             //Connect using unique name
    39             //string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt");
    40             string name = "Client-" + (iClient.ClientCount() - 1);
    41             iClient.SetName(name);
    42             //Text = Text + ": " + name;
    43             Text = "[[" + name + "]]  " + iClient.SessionId;
    44 
    45             //
    46             textBoxTop.Text = iClient.Name;
    47             textBoxBottom.Text = iClient.SessionId;
    48 
    49         }
    50 
    51 
    52 
    53         public delegate void CloseConnectionDelegate();
    54         public delegate void CloseDelegate();
    55 
    56         /// <summary>
    57         ///
    58         /// </summary>
    59         public void CloseConnectionThreadSafe()
    60         {
    61             if (this.InvokeRequired)
    62             {
    63                 //Not in the proper thread, invoke ourselves
    64                 CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
    65                 this.Invoke(d, new object[] { });
    66             }
    67             else
    68             {
    69                 //We are in the proper thread
    70                 if (IsClientReady())
    71                 {
    72                     Trace.TraceInformation("Closing client: " + iClient.SessionId);
    73                     iClient.Close();
    74                     Trace.TraceInformation("Closed client: " + iClient.SessionId);
    75                 }
    76 
    77                 iClient = null;
    78                 iCallback = null;
    79             }
    80         }
    81 
    82         /// <summary>
    83         ///
    84         /// </summary>
    85         public void CloseThreadSafe()
    86         {
    87             if (this.InvokeRequired)
    88             {
    89                 //Not in the proper thread, invoke ourselves
    90                 CloseDelegate d = new CloseDelegate(CloseThreadSafe);
    91                 this.Invoke(d, new object[] { });
    92             }
    93             else
    94             {
    95                 //We are in the proper thread
    96                 Close();
    97             }
    98         }
    99 
   100 
   101         private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
   102         {
   103             CloseConnectionThreadSafe();
   104         }
   105 
   106         public bool IsClientReady()
   107         {
   108             return (iClient != null && iClient.State == CommunicationState.Opened);
   109         }
   110 
   111         private void buttonAlignLeft_Click(object sender, EventArgs e)
   112         {
   113             Alignment = ContentAlignment.MiddleLeft;
   114             textBoxTop.TextAlign = HorizontalAlignment.Left;
   115             textBoxBottom.TextAlign = HorizontalAlignment.Left;
   116         }
   117 
   118         private void buttonAlignCenter_Click(object sender, EventArgs e)
   119         {
   120             Alignment = ContentAlignment.MiddleCenter;
   121             textBoxTop.TextAlign = HorizontalAlignment.Center;
   122             textBoxBottom.TextAlign = HorizontalAlignment.Center;
   123         }
   124 
   125         private void buttonAlignRight_Click(object sender, EventArgs e)
   126         {
   127             Alignment = ContentAlignment.MiddleRight;
   128             textBoxTop.TextAlign = HorizontalAlignment.Right;
   129             textBoxBottom.TextAlign = HorizontalAlignment.Right;
   130         }
   131 
   132         private void buttonSetTopText_Click(object sender, EventArgs e)
   133         {
   134             //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
   135             iTextFieldTop.Text = textBoxTop.Text;
   136             iTextFieldTop.Alignment = Alignment;
   137             iClient.SetText(iTextFieldTop);
   138         }
   139 
   140         private void buttonSetText_Click(object sender, EventArgs e)
   141         {
   142             //iClient.SetText(0,"Top");
   143             //iClient.SetText(1, "Bottom");
   144             //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
   145 
   146             iClient.SetTexts(new TextField[]
   147             {
   148                 new TextField(0, textBoxTop.Text, Alignment),
   149                 new TextField(1, textBoxBottom.Text, Alignment)
   150             });
   151         }
   152 
   153         private void buttonLayoutUpdate_Click(object sender, EventArgs e)
   154         {
   155             //Define a 2 by 2 layout
   156             TableLayout layout = new TableLayout(2,2);
   157             //Second column only takes up 25%
   158             layout.Columns[1].Width = 25F;
   159             //Send layout to server
   160             iClient.SetLayout(layout);
   161         }
   162     }
   163 }