Creating the User Interface for the Windows Mobile Device Application

Goal: Create the user interface for an application that runs on a Windows Mobile device, and accesses the database mobile business objects.

Note:

This procedure includes code snippets you can copy and paste for your project. Click SUP_WM_Custom_Dev_Tutorial_code.zipto access the text files that include the code snippets for the CustomerSample, CustomerList, and CustomerSampleScreen C# files.

If you are viewing this guide as a PDF, you can locate the referenced document at http://infocenter.sybase.com/. Go to Sybase Unwired Platform 1.5.5 > Tutorial: Windows Mobile Application Development using Custom Development to launch this PDF.

  1. Open the generated Visual Studio solution in Visual Studio 2008.
  2. In the Solution Explorer, right-click the SampleApp project and select Add > Windows Form.
  3. In the Add New Item dialog, select Windows Form from the Categories and Templates, enter Customers as the form name, and click Add.

    vs_new_windows_form

    An empty form, Customers, displays on the Customer.cs [Design] tab.

  4. Repeat Step 2 to add another Windows Form to the SampleApp project, and name this one Customer Details.
  5. Click the Customers.cs [Design] tab to go back to the Customers form.
  6. From the Toolbox, drag and drop four buttons from SUP Device Controls onto the form.
  7. Select each button, and in the Properties view, change the Text of the buttons to:
    • button1 – Subscribe
    • button2 – Refresh
    • button3 – Update
    • button4 – Send
  8. In the Toolbox, select ListView from Common Device Controls, and drag and drop it onto the Customers form.
    Add ListView
  9. In the Toolbox, select Textbox from Common Device Controls, and drag and drop it onto the Customers form.
  10. In the Customers form, click the ListView, then in the Properties pane, set FullRowSelect to True.
    FullRowSelect
  11. In Customers, select the Textbox, then in the Properties pane, set these properties:
    • Scrollbars – Vertical
    • Read-only – True
    • Multi-line – True


    vs_textbox_properties

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

    vs_customers_screen
  13. Save the Customers.cs form.
  14. Click the Customer Details.cs [Design] tab.
  15. From the Toolbox, drag and drop three labels onto the Customer Details form. Align them on the left side of the form. In the Properties view, in the Text field, rename the labels Cust ID, First Name, and Last Name.
  16. From the Toolbox, drag and drop three text boxes onto the Customer Details form and align them to the right of each of the three labels.
  17. Select the textbox next to Cust ID and, in the Properties view, change the Read-only property to True.
  18. From the Toolbox, drag and drop a button from SUP Device Controls onto the Customer Details form below the labels and text boxes, and in the Properties view, in the Text field, rename the button to Submit.
    The form will look like this:


    vs_customer_details_screen

  19. In the Solution Explorer, right-click Program.cs, and select View Code.
  20. Add the following code:
    /// <summary>
    /// The main entry point for the application.
    /// </summary>private static Customers _form1 = new Form1();
    using System;
    
    using SampleApp;
    using System.Windows.Forms;
    
    namespace SampleApp
    {
        static class Program
        {
            private static Customers _form1 = new Customers();
            private static Customer_Details _form2 = new Customer_Details();
            private static string _custid;
    
            public static string getCustomer()
            {
                return _custid;
            }
    
            public static void setCustomer(string custid)
            {
                _custid = custid;
            }
    
            public static Customers getForm1()
            {
                return _form1;
            }
    
            public static Customer_Details getForm2()
            {
                return _form2;
            }
    
            static void Main(string[] args)
            {
                Application.Run(_form1);
            }
        }
    }
    
    
  21. In Solution Explorer, in the SampleApp project, right-click Customers.cs and select View Code.
  22. Add the following code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using SUP101;
    
    namespace SampleApp
    {
        public delegate void DelegateAddString(String s);
        public partial class Customers : Form
        {
            DelegateAddString m_DelegateAddString;
            public Customers()
            {
                InitializeComponent();
                m_DelegateAddString = new DelegateAddString(this.AddString);
            }
    
            private void AddString(String s)
            {
                textBox1.Text += s + "\r\n";
                textBox1.SelectionStart = textBox1.Text.Length;
                textBox1.ScrollToCaret();
                textBox1.Refresh();
            }
    
            private void Subscribe_Click(object sender, EventArgs e)
            {
                //This checkes to see if the database exists, then deletes it if it does.
                if (SUP101DB.DatabaseExists())
                    
                {
                    SUP101DB.DeleteDatabase();
                    AddString("Database deleted");
                }
                else
                {
                    //This creates the database.
                    SUP101DB.CreateDatabase();
                    
                }
                //To receive callbacks, you must register a CallBackHandler  
                //with the generated database class, the entity class, or both. This
                //code creates and registers a handler to receive callbacks. 
                MyCallbackHandler callback = new MyCallbackHandler();
                MyCallbackHandler.textBox1 = textBox1;
                SUP101DB.RegisterCallbackHandler(callback);
                //This operation starts background synchronization for the database class.
                SUP101DB.StartBackgroundSynchronization();
                AddString("Background synchronization started");
                try
                {
                    //This calls SUP101, the package database, user name and password to connect to the Unwired Server.
                    SUP101DB.LoginToSync("supAdmin", "s3pAdmin");
                }
                catch (Exception ex)
                {
                    List<LogRecordImpl> lrs = LogRecordImpl.FindWithQuery(new Sybase.Persistence.Query());
                    AddString("log record count=" + lrs.Count);
                    for (int i = 0; i < lrs.Count; i++)
                    {
                        AddString(lrs[i].Message);
                    }
                }
                //For message-based replication, before you can synchronize MBO changes with the server, you must subscribe to the 
                //mobile application package deployed on the server by calling SampleAppDB.subscribe(). This also downloads certain data 
                //to devices for those that have default values. otify the server of your subscription to a specific package. 
                //If you are not subscribed to the SUP101 package,a message will tell you to subscribe first. 
                SUP101DB.Subscribe();
                AddString("Subscribe request sent");
                AddListView();
            }
    
            private void refresh_Click(object sender, EventArgs e)
            {
                if (SUP101DB.IsSubscribed())
                {
                    Cursor.Current = Cursors.WaitCursor;
                    AddString("Refresh data");
                    AddDataToListView();
                    Cursor.Current = Cursors.Default;
                }
                else
                {
                    AddString("Please Subscribe first");
                }
            }
    
            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;
    
                List<Customer> c = Customer.FindAll();
                if (c.Count > 0)
                {
                    for (int i = 0; i < c.Count; i++)
                    {
                        ListViewItem item = new ListViewItem(c[i].Id.ToString());
                        item.SubItems.Add(c[i].Fname);
                        item.SubItems.Add(c[i].Lname);
                        listView1.Items.Add(item);
                    }
                }
            }
    
            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(string s)
                {
                    Customers f = Program.getForm1();
                    f.Invoke(f.m_DelegateAddString, new Object[] { s });
                }
                // Called when login fails.
                public override void OnLoginFailure()
                
                {
                    invokeDelegate("Login failed");
                }
                //Called when a replay request succeeds.
                public override void OnReplaySuccess(object o)
                {
                    invokeDelegate("Operation replay successful");
                }
                //Called when a replay request fails.
                public override void OnReplayFailure(object o)
                {
                    invokeDelegate("Operation Replay failed");
                }
                //Called when the last import message is successfully processed regarding the subscribe request.
                public override void OnImportSuccess()
                {
                    invokeDelegate("Import successful");
                }
            }
        }
    
    }
  23. Click the Customers.cs [Design] tab to go back to the Customers form design view.
  24. Add the event handlers to the buttons:
    1. Click the Subscribe button on the form, and in the Properties view for the button, click the Event icon (lightning bolt), then next to the Click databinding, select Subscribe_Click from the list.
      vs_add_event_button
    1. Repeat this step for each button, selecting these events for each Click databinding:
      • Refresh – refresh_Click
      • Update – update_Click
      • Send – Send_Click
  25. In Solution Explorer, in the SampleApp project, right-click Customer Details.cs and select View Code.
  26. Add the following code:
    using System;
    
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    using SUP101;
    
    namespace SampleApp
    {
        public partial class Customer_Details : Form
        {
            Customer thisCustomer;
            public Customer_Details()
            {
                InitializeComponent();
            }
            private void submit_Click(object sender, EventArgs e)
            {
                thisCustomer.Fname = textBox2.Text;
                thisCustomer.Lname = textBox3.Text;
               //When called, the Save method determines, internally, if it should
              //insert or update data to the client database.
                thisCustomer.Save();
                Program.getForm1().Visible = true;
                Program.getForm2().Visible = false;
            }
    
            private void AddDataToForm()
            {
                textBox1.Text = Program.getCustomer();
                int id = Int32.Parse(Program.getCustomer());
              //Retrieves data from the local database.
                thisCustomer = Customer.FindByPrimaryKey(id);
                textBox2.Text = thisCustomer.Fname;
                textBox3.Text = thisCustomer.Lname;
            }
    
            private void Customer_Details_Load(object sender, EventArgs e)
            {
                AddDataToForm();
            }
    
            private void Customer_Details_paint(object sender, PaintEventArgs e)
            {
                AddDataToForm();
            }
        }
    }
    
           
    
  27. In the Customer_Details.cs [Design] view, click the Submit button and in the Properties view for the button, add the submit_Click event to the Submit button.
  28. Add events to the Customer Details.cs:
    1. Click the Customer Details.cs [Design] tab
    2. In the Properties view for Customer_Details.cs, click the Events icon (lightning bolt).
    3. In Load, add the Customer_Details_Load event.
    4. In Paint, add the Customer_Details_paint event.

      vs_add_events_customer_details
  29. In the main menu, select File > Save All.
  30. Add the generated C# object API project (from the previous section) into the SampleApp solution. This helps ensure the project includes DLL references needed for compilation.
  31. Build the project by pressing Control+Shift+B.