Creating the Customers Form

Create the user interface for the Customers form.

  1. In Solution Explorer, under Solution 'SMP101' (1 project), right-click the SMP101 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.cs as the form name and click Add.

    vs_new_windows_form
    An empty form, Customers, appears 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 – Initialize Application
    • button2 – Load Data
    • button3 – Update Customer
  5. In the Toolbox, under Common Controls, drag ListView and drop it onto the Customers form.
    Add ListView
  6. In the Toolbox, under Common 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:
    • Multiline – True
    • ReadOnly – True
    • ScrollBars – Vertical


    vs_textbox_properties

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

    Customers form
  10. Save the Customers.cs form.
  11. In Solution Explorer, in the SMP101 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 SAP Community Network (SCN) Web site, also provided below:
    Edit the bolded code lines to match the SAP Mobile Platform Admin login, password, and host you indicated during installation.
    using System;
    using System.Linq;
    
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using SMP101Windows;
    using Sybase.Mobile;
    using Sybase.Persistence;
    using Sybase.Collections;
    
    namespace SMP101Windows
    {
        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 = "SMP101";
                MyCallbackHandler.textBox1 = textBox1;
                SMP101DB.RegisterCallbackHandler(new MyCallbackHandler());
                SMP101DB.SetApplication(app);
                SMP101DB.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 (!SMP101DB.IsSynchronized("default"))
                {
                    SMP101DB.DisableChangeLog();
                    AddString("Package synchronizing ...");
                    SMP101DB.Synchronize(); // Initial Synchronize
    
                    ISynchronizationGroup sg = SMP101DB.GetSynchronizationGroup("default");
                    sg.EnableSIS = true;
                    sg.Save();
                    SMP101DB.Synchronize();
                    AddString("Package synchronized");
                }
                SMP101DB.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", Sybase.Persistence.SortOrder.ASCENDING);
    
                int index = 0;
                QueryResultSet rs = SMP101DB.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 = SMP101DB.GetChangeLogs(query);
                        foreach (IChangeLog changeLog in changeLogs)
                        {
                            if (changeLog.EntityType == EntityType.Customer)
                            {
                                invokeDelegate(changeLog.SurrogateKey);
                            }
                        }
                    }
    
                    return SynchronizationAction.CONTINUE;
                }
            }
        }
    }
    
  13. Click the Customers.cs[Design] tab to return to the Customers form design view to add event handlers to the buttons.
    1. Click the Initialize Application button on the form.
    2. In the Properties view for the button, click the Event icon (lightning bolt).
    3. Next to Click, 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
  14. Save your changes.