Creating the Customers Form

Create the user interface for the Customers form.

  1. In Solution Explorer, under Solution 'SUP101' (1 project), right-click the SUP101 project and select Add > New Item.
  2. In the Add New Item Categories section, select Windows Forms. From the Templates section, select Windows Form. Enter Customers as the form name and click Add.

    vs_new_windows_form
    An empty form, Customers, displays on the Customers.cs [Design] tab.
  3. From the Toolbox, drag and drop three buttons onto the form.
  4. Select each button, and in the Properties view, change the Text of the buttons to:
    • button1 – InitializeApplication
    • button2 – LoadData
    • button3 – Update Customer
  5. In the Toolbox, under Common Device Controls, drag ListView and drop it onto the Customers form.
    Add ListView
  6. In the Toolbox, under Common Device Controls, drag Textbox and drop it onto the Customers form.
  7. In the Customers form, click the ListView control, then in the Properties pane, set FullRowSelect to True.
    FullRowSelect
  8. In the Customers form, select the Textbox, then in the Properties pane, set these properties:
    • Multi-line – True
    • Read-only – True
    • Scrollbars – Vertical


    vs_textbox_properties

  9. Arrange the controls on the Customers form so they look like this:

    vs_customers_screen
  10. Save the Customers.cs form.
  11. In Solution Explorer, in the SUP101 project, right-click Customers.cs, then select View Code.
  12. Replace the code with the source code from the Customers.cs file you downloaded from the Sybase SyBooks Online Infocenter site, also provided below:
    using System;
    
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using SUP101;
    using Sybase.Mobile;
    using Sybase.Persistence;
    using Sybase.Collections;
    
    namespace SUP101
    {
        public delegate void DelegateAddString(String s);
        public delegate void DelegateRefreshItem(long i);
        public partial class Customers : Form
        {
            private const String USERNAME = "supAdmin";
            private const String PASSWORD = "s3pAdmin";
            private const String HOST = "...";
            private const int PORT = 5001;
            private const int TIMEOUT = 600;
            private System.Collections.Generic.Dictionary<string, long> IdToSK = new System.Collections.Generic.Dictionary<string, long>();
            internal static System.Collections.Generic.Dictionary<long, int> SKToIndex = new System.Collections.Generic.Dictionary<long, int>();
    
            DelegateAddString m_DelegateAddString;
    
            DelegateRefreshItem m_refreshItem;
    
            public Customers()
            {
                InitializeComponent();
                m_DelegateAddString = new DelegateAddString(this.AddString);
                m_refreshItem = new DelegateRefreshItem(this.RefreshListItem);
            }
    
            internal void RefreshListItem(long sk)
            {
                int index = SKToIndex[sk];
                listView1.BeginUpdate();
                ListViewItem item = listView1.Items[index];
                String id = item.Text;
    
                Customer thisCustomer = Customer.FindByPrimaryKey(Int32.Parse(id));
                item.SubItems[1].Text = thisCustomer.Fname;
                item.SubItems[2].Text = thisCustomer.Lname;
                listView1.EndUpdate();
            }
    
            private void AddString(String s)
            {
                textBox1.Text += s + "\r\n";
                textBox1.SelectionStart = textBox1.Text.Length;
                textBox1.ScrollToCaret();
                textBox1.Refresh();
            }
    
            private void InitializeApplication_Click(object sender, EventArgs e)
            {
                Sybase.Mobile.Application app = Sybase.Mobile.Application.GetInstance();
                app.ApplicationIdentifier = "SUP101";
                MyCallbackHandler.textBox1 = textBox1;
                SUP101DB.RegisterCallbackHandler(new MyCallbackHandler());
                SUP101DB.SetApplication(app);
                SUP101DB.GetSynchronizationProfile().ServerName = HOST;
    
                ConnectionProperties connProps = app.ConnectionProperties;
                LoginCredentials loginCredentials = new LoginCredentials(USERNAME, PASSWORD);
    
                connProps.LoginCredentials = loginCredentials;
                connProps.ServerName = HOST;
                connProps.PortNumber = PORT;
    
                if (app.RegistrationStatus != RegistrationStatus.REGISTERED)
                {
                    AddString("Application registering ... ");
                    app.RegisterApplication(TIMEOUT);
                    AddString("Application registered");
                }
                else
                {
                    AddString("Connecting to server ...");
                    app.StartConnection(TIMEOUT);
                    AddString("Connected to server");
                }
    
                if (!SUP101DB.IsSynchronized("default"))
                {
                    SUP101DB.DisableChangeLog();
                    AddString("Package synchronizing ...");
                    SUP101DB.Synchronize(); // Initial Synchronize
    
                    ISynchronizationGroup sg = SUP101DB.GetSynchronizationGroup("default");
                    sg.EnableSIS = true;
                    sg.Save();
                    SUP101DB.Synchronize();
                    AddString("Package synchronized");
                }
                SUP101DB.EnableChangeLog();
                AddListView();
            }
    
            private void LoadData_Click(object sender, EventArgs e)
            {
                if (Sybase.Mobile.Application.GetInstance().RegistrationStatus == RegistrationStatus.REGISTERED)
                {
                    Cursor.Current = Cursors.WaitCursor;
                    AddString("Loading data from database ...");
                    AddDataToListView();
                    Cursor.Current = Cursors.Default;
                }
                else
                {
                    AddString("Application is not initialized!");
                }
            }
    
            private void AddListView()
            {
                this.listView1.Clear();
                listView1.Columns.Add("Id", listView1.Width / 4, HorizontalAlignment.Left);
                listView1.Columns.Add("First Name", listView1.Width / 3, HorizontalAlignment.Center);
                listView1.Columns.Add("Last Name", listView1.Width / 3, HorizontalAlignment.Right);
                listView1.View = View.Details;
                listView1.FullRowSelect = true;
            }
    
            private void AddDataToListView()
            {
                this.listView1.Clear();
                listView1.Columns.Add("Id", listView1.Width / 4,
                HorizontalAlignment.Left);
                listView1.Columns.Add("First Name", listView1.Width /
                3, HorizontalAlignment.Center);
                listView1.Columns.Add("Last Name", listView1.Width / 3,
                HorizontalAlignment.Right);
                listView1.View = View.Details;
                listView1.FullRowSelect = true;
    
    
                Query query = new Query();
                query.Select("x.fname, x.lname, x.surrogateKey, x.id");
                query.From("Customer", "x");
                query.OrderBy("id", SortOrder.ASCENDING);
    
                int index = 0;
                QueryResultSet rs = SUP101DB.ExecuteQuery(query);
                while (rs.Next())
                {
                    String fname = rs.GetString(1);
                    String lname = rs.GetString(2);
                    long sk = rs.GetLong(3);
                    String id = rs.GetString(4);
    
                    ListViewItem item = new ListViewItem(id);
                    item.SubItems.Add(fname);
                    item.SubItems.Add(lname);
                    listView1.Items.Add(item);
    
    
                    IdToSK.Add(id, sk);
                    SKToIndex.Add(sk, index++);
                }
            }
            private void update_Click(object sender, EventArgs e)
            {
                if (listView1.FocusedItem != null)
                {
                    Program.setCustomer(listView1.FocusedItem.Text);
                    Program.getForm2().Visible = true;
                    Program.getForm1().Visible = false;
                }
                else
                    MessageBox.Show("Please select a row");
            }
            private void Send_Click(object sender, EventArgs e)
            {
                Customer.SubmitPendingOperations();
            }
            public class MyCallbackHandler : Sybase.Persistence.DefaultCallbackHandler
            {
                public static TextBox textBox1 = new TextBox();
    
                private void invokeDelegate(long i)
                {
                    Customers f = Program.getForm1();
                    f.Invoke(f.m_refreshItem, new Object[] { i });
                }
    
                public override SynchronizationAction OnSynchronize(GenericList<ISynchronizationGroup> groups, SynchronizationContext context)
                {
    
                    if (context.Status == SynchronizationStatus.FINISHING || context.Status == SynchronizationStatus.ASYNC_REPLAY_UPLOADED)
                    {
                        Query query = new Query();
                        GenericList<IChangeLog> changeLogs = SUP101DB.GetChangeLogs(query);
                        foreach (IChangeLog changeLog in changeLogs)
                        {
                            if (changeLog.EntityType == EntityType.Customer)
                            {
                                invokeDelegate(changeLog.SurrogateKey);
                            }
                        }
                    }
    
                    return SynchronizationAction.CONTINUE;
                }
            }
        }
    }
    
  13. Be sure the bolded code line matches the Sybase Unwired Platform Admin login, password, and host you indicated during installation.
    private const String USERNAME = "supAdmin";
      private const String PASSWORD = "s3pAdmin";
      private const String HOST = "…";
    
  14. Click the Customers.cs[Design] tab to go back to the Customers form design view to add event handlers to the buttons.
    1. Click the InitializeApplication button on the form.
    2. In the Properties view for the button, click the Event icon (lightning bolt).
    3. Next to the Click data binding, select InitializeApplication_Click.
    4. Repeat these steps for each button, selecting these events for each Click data binding.
      • LoadData – LoadData_Click
      • Update Customer – update_Click
  15. Save your changes.