Search This Blog

Showing posts with label C# Example. Show all posts
Showing posts with label C# Example. Show all posts

Thursday, May 24, 2012

Add date string to a file name

 string logfile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), string.Format("RISCONVDB_{0}.LOG", DateTime.Now.ToFileTime()));

Sunday, August 28, 2011

C# Code to add a registry Key

Discovered that adding Registry keys is slightly more complicated using C# vs VB6...
Needed to define a user and registry security to a key change....

public static bool AddRegKey(string keyName, string valueName) { try { string user = Environment.UserDomainName + "\\" + Environment.UserName; RegistrySecurity rs = new RegistrySecurity(); rs.AddAccessRule(new RegistryAccessRule(user, RegistryRights.ReadKey | RegistryRights.Delete | RegistryRights.WriteKey | RegistryRights.ChangePermissions, InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow)); RegistryKey key = Registry.CurrentUser.OpenSubKey(sDRCRegKey,true); key.SetAccessControl(rs); key.SetValue(keyName, valueName); return true; } catch { return false; } }

Friday, November 26, 2010

Create and populate a DataTable manually.

Create and populate a DataTable manually.

DataTable table = new DataTable();
table.TableName = "SelectedMinOcc";
table.Columns.Add("ID", typeof(Int32));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("LAT", typeof(double));
table.Columns.Add("LONG", typeof(double));

foreach (DataGridViewRow row in dgvSearchResults.SelectedRows)
{
table.Rows.Add(Convert.ToInt32(row.Cells["ID"].Value)
, Convert.ToString(row.Cells["Name"].Value)
, Convert.ToDouble(row.Cells["LAT"].Value)
, Convert.ToDouble(row.Cells["LONG"].Value)
);
}

Saturday, November 6, 2010

Change Context menu items on the fly

The example using the contextMenuStrip on a winforms form.
Purpose is to use a single contextStripMenu but have the list of menu items change based in the control associated with.

cmsMap = ContextMenuStrip control which is as the contextmenu for tabcontrol1 and drcMapControl1.

void cmsMap_Opening(object sender, CancelEventArgs e)
{
   //Set all visible to all menu items to false
   (sender as ContextMenuStrip).Items["moveTabsToTopToolStripMenuItem"].Visible = false;
   (sender as ContextMenuStrip).Items["moveTabsToBottomToolStripMenuItem"].Visible = false;
   (sender as ContextMenuStrip).Items["addLabelToolStripMenuItem"].Visible = false;

  //Based on the sender.SourceControl make specific menu items visible.
  switch ((sender as ContextMenuStrip).SourceControl.Name)
  {
    case "drcMapControl1":
     (sender as ContextMenuStrip).Items["addLabelToolStripMenuItem"].Visible = true;
        break;
    case "tabControl1":
     (sender as ContextMenuStrip).Items["moveTabsToTopToolStripMenuItem"].Visible = true;
     (sender as ContextMenuStrip).Items["moveTabsToBottomToolStripMenuItem"].Visible = true;
     switch (tabControl1.Alignment)
     {
         case TabAlignment.Bottom:
        (sender as ContextMenuStrip).Items["moveTabsToBottomToolStripMenuItem"].Visible = false;
               break;
         case TabAlignment.Top:
         (sender as ContextMenuStrip).Items["moveTabsToTopToolStripMenuItem"].Visible = false;
                break;
         default:
               break;
    }
        break;
    default:
         break;
    }
}