Settings

Settings can be stored using

SharedPreferences settings;
settings = PreferenceManager.getDefaultSharedPreferences(context);

This will access settings stored for the context which is typically an Activity! To share settings cross Activities use context.getApplicationContext() instead of context. This will share the data,

settings = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

Show a kind of messagebox

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?");

builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
MainActivity.this.finish();
}
});

builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});

builder.setNeutralButton("Test", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{


}
});

AlertDialog alert = builder.create();
alert.show();

Attach items to a listview

A listview shows data provided by an adapter. To create a simple list use the following:

//Find the listview with id 'list' in the resources 
ListView listView = (ListView) findViewById(R.id.list);

//Make a connection through an adapter 
listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, new String[] {"AAA", "BBB", "CCC"}));


Showing time in DataGridView column

To show time in a DataGridView column set the column type to DataGridViewTextBoxColumn.
Set the columns DefaultCellStyle Format to ‘t’. You can do this in the designer or use the following code:

myGridView.Columns[0].DefaultCellStyle.Format = “t”; //0 is the required column, change if needed.

Now you can assign a DateTime to the cell value and time will show in the users preferred format (‘t’).

Check whether a form is valid.

When using modeless forms the user can close  the form manually. Use the created property to check whether this is the case.

Example:

class Form1 : Form
{
   Form2 modelessForm = null;

   //On mouse click show Form2
   void onMouseClick(MouseButtons button, int x, int y)
   {
      if (modelessForm == null || !modelessForm.Created)
      {
         modelessForm = new Form2()
      }

      modelessForm.Show();
   }
}