How do I reliably set a value for a checkbox field?

Q: One thing we are noticing in our generated PDF's that check boxes
sometimes render properly (i.e., appear checked) and sometimes they do
not. The code snippet is as follows:

                if (type == Field.Type.e_check) // just to be sure
                {
                    if (check == true)
                        field.SetValue("Yes");
                    else
                        field.SetValue("No");
                }

type is Field.Type and check is a boolean passed into the method that
handles check boxes. The only difference in our entire implementation
over what your samples suggest is in how we are refreshing
appearances.

Again, this does not occur all the time and in some cases code always
sets a check value in one of the fields irrespective of the data. In
other words, irrespective of underlying data or logic, there are a few
check boxes that are always checked (for ex: if(condition) then check
that, else check this). The problem is that this is very inconsistent.
Sometimes they appear fine, other times they do not. There is one
field (gender: Male/Female) that almost always works; this is the
first check box on the PDF, but others on the form, below this, have
inconsistent appearances.

Any ideas?
------
A: I believe that the following article should answer your question:

http://groups.google.com/group/pdfnet-sdk/browse_thread/thread/6cc3671abb568a9/9668095e624c839e
(or search for "toggle checkbox" in this forum).

Q: I use the following code to set the value for checkboxes however
check-boxes are not checked. Am I missing something?

static void SetCheckField(PDFDoc _doc, string name, bool check) {
  Field field = null;
  if ((field = _doc.GetField(name)) != null) {
   Field.Type type = field.GetType();
   if (type == Field.Type.e_check) { // just to be sure
   if (check == true){
      field.SetValue(GetOnState(field));
   }
   else {
      field.SetValue("Off");
   }
  }
}
}

static string GetOnState(Field fld) {
   Obj ap_dict;
   try {
      ap_dict = fld.GetSDFObj().Get("AP").Value().Get("N").Value();
   }
   catch (Exception e){
      return "Yes";
   }
   DictIterator itr = ap_dict.GetDictIterator();
   for (; itr.HasNext(); itr.Next()) {
      if (itr.Key().GetName().ToLower().Equals("off")) continue;
      else return itr.Key().GetName();
   }
   return "Yes";
}
-----
A: The problem is that pdfdoc.RefreshFieldAppearances() or
field.RefreshAppearance() is not called.

To go around this you need to call RefreshAppearance() after setting
the check field. For example:

static void SetCheckField( string name, bool check ) {
Field field = null;
if ((field = _doc.GetField(name)) != null) {
   ...
   field.RefreshAppearance();
}
}

Typically a PDFNet users call pdfdoc.RefreshFieldAppearances() just
before saving the file instead of refreshing each field individually.
It seems that you are not doing this (perhaps because you want to
delegate generation of some form field appearances to Acrobat?). In
this case you should call field.RefreshAppearance() on individual
checkboxes (fields) which need to be refreshed.