How to set the CategoryAttribute of a PropertyGrid to the SelectedItem of a list












0















I have a Windows Forms project in which I am trying to set the attributes of a PropertyGrid with properties of the item I selected from a ListBox. I have a ListBox called listBox1 with two values (called "temporary" and "temporary2") and a PropertyGrid called propertyGrid1.



Here is my code:



using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Diagnostics;

namespace SortListProperties
{
public partial class MainForm : Form
{
public class SortProperties
{
public string Name;
[CategoryAttribute(listBox1.SelectedItem.ToString()), DescriptionAttribute("Description of item goes here")]
public string Info
{
get
{
return Name;
}

set
{
Name = value;
}
}
}
public MainForm()
{
InitializeComponent();
var properties = new SortProperties();
propertyGrid1.SelectedObject = properties;
}
}
}


Here is the initializing code:



    namespace SortListProperties
{
partial class MainForm
{
private System.ComponentModel.IContainer components = null;
public System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.PropertyGrid propertyGrid1;

protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}

private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.listBox1 = new System.Windows.Forms.ListBox();
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.SuspendLayout();

this.listBox1.FormattingEnabled = true;
this.listBox1.Items.AddRange(new object {"temporary", "temporary2"});
this.listBox1.Location = new System.Drawing.Point(12, 50);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(146, 238);
this.listBox1.TabIndex = 2;

this.propertyGrid1.Location = new System.Drawing.Point(164, 24);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.Size = new System.Drawing.Size(272, 261);
this.propertyGrid1.TabIndex = 3;

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(448, 337);
this.Controls.Add(this.propertyGrid1);
this.Controls.Add(this.listBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "MainForm";
this.Text = "Get Item Properties Test";
this.ResumeLayout(false);
this.PerformLayout();

}
}
}


When I run the program, I get this error:




(15,23) : Error(CS0120): An object reference is required for the non-static field, method, or property 'SortListProperties.MainForm.listBox1'




This is what I expect



If someone could point out how to get the PropertyGrid to set the category name to the selected item of the list, that would be great. Thanks!










share|improve this question



























    0















    I have a Windows Forms project in which I am trying to set the attributes of a PropertyGrid with properties of the item I selected from a ListBox. I have a ListBox called listBox1 with two values (called "temporary" and "temporary2") and a PropertyGrid called propertyGrid1.



    Here is my code:



    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Diagnostics;

    namespace SortListProperties
    {
    public partial class MainForm : Form
    {
    public class SortProperties
    {
    public string Name;
    [CategoryAttribute(listBox1.SelectedItem.ToString()), DescriptionAttribute("Description of item goes here")]
    public string Info
    {
    get
    {
    return Name;
    }

    set
    {
    Name = value;
    }
    }
    }
    public MainForm()
    {
    InitializeComponent();
    var properties = new SortProperties();
    propertyGrid1.SelectedObject = properties;
    }
    }
    }


    Here is the initializing code:



        namespace SortListProperties
    {
    partial class MainForm
    {
    private System.ComponentModel.IContainer components = null;
    public System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.PropertyGrid propertyGrid1;

    protected override void Dispose(bool disposing)
    {
    if (disposing) {
    if (components != null) {
    components.Dispose();
    }
    }
    base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
    System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
    this.listBox1 = new System.Windows.Forms.ListBox();
    this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
    this.SuspendLayout();

    this.listBox1.FormattingEnabled = true;
    this.listBox1.Items.AddRange(new object {"temporary", "temporary2"});
    this.listBox1.Location = new System.Drawing.Point(12, 50);
    this.listBox1.Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(146, 238);
    this.listBox1.TabIndex = 2;

    this.propertyGrid1.Location = new System.Drawing.Point(164, 24);
    this.propertyGrid1.Name = "propertyGrid1";
    this.propertyGrid1.Size = new System.Drawing.Size(272, 261);
    this.propertyGrid1.TabIndex = 3;

    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(448, 337);
    this.Controls.Add(this.propertyGrid1);
    this.Controls.Add(this.listBox1);
    this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
    this.Name = "MainForm";
    this.Text = "Get Item Properties Test";
    this.ResumeLayout(false);
    this.PerformLayout();

    }
    }
    }


    When I run the program, I get this error:




    (15,23) : Error(CS0120): An object reference is required for the non-static field, method, or property 'SortListProperties.MainForm.listBox1'




    This is what I expect



    If someone could point out how to get the PropertyGrid to set the category name to the selected item of the list, that would be great. Thanks!










    share|improve this question

























      0












      0








      0








      I have a Windows Forms project in which I am trying to set the attributes of a PropertyGrid with properties of the item I selected from a ListBox. I have a ListBox called listBox1 with two values (called "temporary" and "temporary2") and a PropertyGrid called propertyGrid1.



      Here is my code:



      using System;
      using System.Collections.Generic;
      using System.Drawing;
      using System.Windows.Forms;
      using System.ComponentModel;
      using System.Diagnostics;

      namespace SortListProperties
      {
      public partial class MainForm : Form
      {
      public class SortProperties
      {
      public string Name;
      [CategoryAttribute(listBox1.SelectedItem.ToString()), DescriptionAttribute("Description of item goes here")]
      public string Info
      {
      get
      {
      return Name;
      }

      set
      {
      Name = value;
      }
      }
      }
      public MainForm()
      {
      InitializeComponent();
      var properties = new SortProperties();
      propertyGrid1.SelectedObject = properties;
      }
      }
      }


      Here is the initializing code:



          namespace SortListProperties
      {
      partial class MainForm
      {
      private System.ComponentModel.IContainer components = null;
      public System.Windows.Forms.ListBox listBox1;
      private System.Windows.Forms.PropertyGrid propertyGrid1;

      protected override void Dispose(bool disposing)
      {
      if (disposing) {
      if (components != null) {
      components.Dispose();
      }
      }
      base.Dispose(disposing);
      }

      private void InitializeComponent()
      {
      System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
      this.listBox1 = new System.Windows.Forms.ListBox();
      this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
      this.SuspendLayout();

      this.listBox1.FormattingEnabled = true;
      this.listBox1.Items.AddRange(new object {"temporary", "temporary2"});
      this.listBox1.Location = new System.Drawing.Point(12, 50);
      this.listBox1.Name = "listBox1";
      this.listBox1.Size = new System.Drawing.Size(146, 238);
      this.listBox1.TabIndex = 2;

      this.propertyGrid1.Location = new System.Drawing.Point(164, 24);
      this.propertyGrid1.Name = "propertyGrid1";
      this.propertyGrid1.Size = new System.Drawing.Size(272, 261);
      this.propertyGrid1.TabIndex = 3;

      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(448, 337);
      this.Controls.Add(this.propertyGrid1);
      this.Controls.Add(this.listBox1);
      this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
      this.Name = "MainForm";
      this.Text = "Get Item Properties Test";
      this.ResumeLayout(false);
      this.PerformLayout();

      }
      }
      }


      When I run the program, I get this error:




      (15,23) : Error(CS0120): An object reference is required for the non-static field, method, or property 'SortListProperties.MainForm.listBox1'




      This is what I expect



      If someone could point out how to get the PropertyGrid to set the category name to the selected item of the list, that would be great. Thanks!










      share|improve this question














      I have a Windows Forms project in which I am trying to set the attributes of a PropertyGrid with properties of the item I selected from a ListBox. I have a ListBox called listBox1 with two values (called "temporary" and "temporary2") and a PropertyGrid called propertyGrid1.



      Here is my code:



      using System;
      using System.Collections.Generic;
      using System.Drawing;
      using System.Windows.Forms;
      using System.ComponentModel;
      using System.Diagnostics;

      namespace SortListProperties
      {
      public partial class MainForm : Form
      {
      public class SortProperties
      {
      public string Name;
      [CategoryAttribute(listBox1.SelectedItem.ToString()), DescriptionAttribute("Description of item goes here")]
      public string Info
      {
      get
      {
      return Name;
      }

      set
      {
      Name = value;
      }
      }
      }
      public MainForm()
      {
      InitializeComponent();
      var properties = new SortProperties();
      propertyGrid1.SelectedObject = properties;
      }
      }
      }


      Here is the initializing code:



          namespace SortListProperties
      {
      partial class MainForm
      {
      private System.ComponentModel.IContainer components = null;
      public System.Windows.Forms.ListBox listBox1;
      private System.Windows.Forms.PropertyGrid propertyGrid1;

      protected override void Dispose(bool disposing)
      {
      if (disposing) {
      if (components != null) {
      components.Dispose();
      }
      }
      base.Dispose(disposing);
      }

      private void InitializeComponent()
      {
      System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
      this.listBox1 = new System.Windows.Forms.ListBox();
      this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
      this.SuspendLayout();

      this.listBox1.FormattingEnabled = true;
      this.listBox1.Items.AddRange(new object {"temporary", "temporary2"});
      this.listBox1.Location = new System.Drawing.Point(12, 50);
      this.listBox1.Name = "listBox1";
      this.listBox1.Size = new System.Drawing.Size(146, 238);
      this.listBox1.TabIndex = 2;

      this.propertyGrid1.Location = new System.Drawing.Point(164, 24);
      this.propertyGrid1.Name = "propertyGrid1";
      this.propertyGrid1.Size = new System.Drawing.Size(272, 261);
      this.propertyGrid1.TabIndex = 3;

      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(448, 337);
      this.Controls.Add(this.propertyGrid1);
      this.Controls.Add(this.listBox1);
      this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
      this.Name = "MainForm";
      this.Text = "Get Item Properties Test";
      this.ResumeLayout(false);
      this.PerformLayout();

      }
      }
      }


      When I run the program, I get this error:




      (15,23) : Error(CS0120): An object reference is required for the non-static field, method, or property 'SortListProperties.MainForm.listBox1'




      This is what I expect



      If someone could point out how to get the PropertyGrid to set the category name to the selected item of the list, that would be great. Thanks!







      c# winforms listbox propertygrid






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 7:41









      DelkarixDelkarix

      117




      117
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442496%2fhow-to-set-the-categoryattribute-of-a-propertygrid-to-the-selecteditem-of-a-list%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442496%2fhow-to-set-the-categoryattribute-of-a-propertygrid-to-the-selecteditem-of-a-list%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?