Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Properties Window File Dialog

Properties Window File Dialog

Scheduled Pinned Locked Moved C#
question
4 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thrakazog
    wrote on last edited by
    #1

    Hi All, I'm creating a custom control that has a property of type String that takes a file path. When this property is being edited from the Properties Window I would like to open the File Dialog so the user can browse to the one they want. How can I do this? Thanks.

    M T D 3 Replies Last reply
    0
    • T thrakazog

      Hi All, I'm creating a custom control that has a property of type String that takes a file path. When this property is being edited from the Properties Window I would like to open the File Dialog so the user can browse to the one they want. How can I do this? Thanks.

      M Offline
      M Offline
      Mbah Dhaim
      wrote on last edited by
      #2

      you can try this sample code

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Drawing.Design;
      using System.Windows.Forms.Design;
      using System.ComponentModel;

      namespace testproperty
      {
      public class TestClass
      {
      public TestClass()
      {
      }
      private int id;
      [System.ComponentModel.Category("Main")]
      [System.ComponentModel.DisplayName("ID of Class")]
      public int Id
      {
      get { return id; }
      set { id = value; }
      }

          private string fileName;
          \[System.ComponentModel.Category("Others")\]
          \[System.ComponentModel.DisplayName("FileName to load")\]
          \[BrowsableAttribute(true)\]
          \[EditorAttribute(typeof(OpenFileDialogEditor), typeof(System.Drawing.Design.UITypeEditor))\]
          public string FileName
          {
              get { return fileName; }
              set { fileName = value; }
          }
      }
      
      \[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")\] 
      public class OpenFileDialogEditor : System.Drawing.Design.UITypeEditor
      {
          public OpenFileDialogEditor() { }
          public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
          {
              return UITypeEditorEditStyle.Modal;
          }
          public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
          {               
              using (System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog())
              {
                  if (value != null && !string.IsNullOrEmpty(value.ToString()))
                  {
                      try { ofd.FileName = value.ToString(); }
                      catch { }
                  }
                  if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                  {
                      return ofd.FileName;
                  }
                  return string.Empty;
              }
          }
      }
      

      }

      look the FileName property of TestClass hope it helps

      dhaim programming is a hobby that make some money as side effect :)

      1 Reply Last reply
      0
      • T thrakazog

        Hi All, I'm creating a custom control that has a property of type String that takes a file path. When this property is being edited from the Properties Window I would like to open the File Dialog so the user can browse to the one they want. How can I do this? Thanks.

        T Offline
        T Offline
        Thomas Stockwell
        wrote on last edited by
        #3

        One of my articles might be able to help you with customizing the design-time events of user controls, it can be found here[^].

        Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my Blog

        1 Reply Last reply
        0
        • T thrakazog

          Hi All, I'm creating a custom control that has a property of type String that takes a file path. When this property is being edited from the Properties Window I would like to open the File Dialog so the user can browse to the one they want. How can I do this? Thanks.

          D Offline
          D Offline
          Dr Emmett Brown
          wrote on last edited by
          #4

          VS uses a property grid to show the properties of a control. You have to define the editors to use in your property, so when the grid access your class (by reflection), it knows what editor to use. The editors must be of the type UITypeEditor. You can sub-class it to create your own editors. The file browser is the System.Windows.Forms.Design.FileNameEditor class. You define the editor in this way:

          [Editor(typeof(System.Windows.Forms.Design.FileNameEditor),typeof(System.Drawing.Design.UITypeEditor))]
          public string YourProperty
          {
          ...

          You can find more information on this MSDN article. BTW, you need to link the System.Design library to use the filename editor.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups