none
superbutton

    Question

  • I created a button with a char on it i click on it and it gets big, how do i make it so that when i click on it again it gets small and if i click on another it get small then the other one gets big. I just want one to get big at a time? Here the code for the super button.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Drawing;
    
    namespace ClassLibrary2
    {
        public class SupButton1 : Button
        {
            private Button button1;
            private bool isSuper = true;
            [Description("Gets or sets whether or not the control is Super")]
            [Category("Super Stuff")]
            [DefaultValue(true)]
            public bool IsSuper
            {
                get { return isSuper; }
                set { isSuper = value; }
            }
    
           
                protected override void OnMouseClick(MouseEventArgs e)
            {
                base.OnMouseClick(e);
                this.Size = new Size(50, 50);
            
            
            }
    
    
    
    
    
    
            
            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(0, 0);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(23, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "╨";
                this.button1.UseVisualStyleBackColor = true;
                this.ResumeLayout(false);
            }
        }
    }

    michael r demulling

    Tuesday, September 25, 2012 11:29 AM

Answers

  • I already suggested using a new class that manages the state of N SuperButtons, such as a SuperButtonCollection, in my first post.  Since this is a new and different question, and this thread is already fairly long, please ask that question in a new post.

    --
    Mike
    Sunday, September 30, 2012 3:46 PM

All replies

  • To make your button toggle sizes, you could do something like this.  If you want two of these to behave similar to a radio button where the selected one is big, then that is a bit more difficult.  If that is what you want you need to have a property "Selected", and change the size when it changes.  You would control this from either the form containing them, or by creating a control collection class that can manage them.
     
      protected override void OnMouseClick(MouseEventArgs e)
      {
       base.OnMouseClick(e);
       if (this.Size.Width == 23 && this.Size.Height == 23)
        this.Size = new Size(50, 50);
       else
        this.Size = new Size(23, 23);
      }

    --
    Mike
    Tuesday, September 25, 2012 12:00 PM
  • I inserted the code you gave me into the supbutton. The supbutton is a dll file i created.What i want to do is put 256 of these buttons on a form but i cant get your code to work , i placed it in the supbutton dll and it gets big when i click on it in the form but wont get small when i click on it the second time. The idea here is to have only one of the 256 buttons get big at one time.

    thanx for your time

    mike


    michael r demulling

    Thursday, September 27, 2012 2:31 AM
  • I have no idea why this would occur for you.  Put a breakpoint at the "if" statement in the code I supplied.  Is it hit after both clicks?  What is the size when it is hit?

    --
    Mike
    Thursday, September 27, 2012 9:00 PM
  • Here is a complete version of your code, that works for me.  You had alot of stuff that was not used.
     
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Drawing;
    namespace WindowsFormsApplication1
    {
     class SuperButton : Button
     {
      private bool isSuper = true;
     
      [Description("Gets or sets whether or not the control is Super")]
      [Category("Super Stuff")]
      [DefaultValue(true)]
      public bool IsSuper
      {
       get { return isSuper; }
       set { isSuper = value; SetSize(); }
      }
     
      void SetSize()
      {
       this.Size = isSuper ? new Size(50, 50) : new Size(23, 23);
      }
     
      protected override void OnMouseClick(MouseEventArgs e)
      {
       base.OnMouseClick(e);
       isSuper = !isSuper;
       SetSize();
      }
     
      protected override void OnLayout(LayoutEventArgs levent)
      {
       base.OnLayout(levent);
       this.Size = isSuper ? new Size(50, 50) : new Size(23, 23);
      }
     
      public SuperButton() { SetSize(); }
     }
    }

    --
    Mike
    Thursday, September 27, 2012 9:29 PM
  • Icopied your code into my dll rebuilt it it still doesnt work the button gets big but wont get small when i click on it the second time. What i got on the formside is three different forms here is the code for the form side. i tried to set a break point and it is not hitting on the click event at all. i set the break point on the else statement and it dont hit. here is the code i have on the form side.

    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Windows.Controls.Primitives;
    using System.Windows.Controls;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Controls.Properties;
    
    
    
    
    
    
    
    
    
    namespace HexCalcChar
    {
        public partial class CharMap : Form
        {
            public CharMap()
            {
                InitializeComponent();
    
            }
    
            private void label1_Click(object sender, EventArgs e)
            {
    
    
    
    
    
            }
    
    
    
            private void CharMap_Load(object sender, EventArgs e)
            {
    
            }
    
              }
             }
    


    michael r demulling

    Friday, September 28, 2012 12:56 PM
  • The code for the CharMap form does not show anything related to the SuperButton.  Is it declared in the .Designer.cs file?  Can you provide that code?

    --
    Mike
    Saturday, September 29, 2012 1:59 AM
  • Here is the code in CharMap.Designer.cs.

    namespace HexCalcChar { partial class CharMap { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.textBox3 = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.supButton11 = new ClassLibrary2.SupButton1(); this.supButton12 = new ClassLibrary2.SupButton1(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(90, 414); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(134, 20); this.textBox1.TabIndex = 0; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(303, 414); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(123, 20); this.textBox2.TabIndex = 2; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(30, 417); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(54, 13); this.label1.TabIndex = 3; this.label1.Text = "DECIMAL"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(230, 417); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(76, 13); this.label2.TabIndex = 5; this.label2.Text = "HECDECIMAL"; // // textBox3 // this.textBox3.Location = new System.Drawing.Point(545, 417); this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(34, 20); this.textBox3.TabIndex = 10; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(461, 420); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(65, 13); this.label3.TabIndex = 11; this.label3.Text = "Last Clicked"; // // supButton11 // this.supButton11.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.supButton11.IsSuper = false; this.supButton11.Location = new System.Drawing.Point(207, 175); this.supButton11.Name = "supButton11"; this.supButton11.Size = new System.Drawing.Size(23, 23); this.supButton11.TabIndex = 12; this.supButton11.Text = "supButton11"; this.supButton11.UseVisualStyleBackColor = true; // // supButton12 // this.supButton12.Location = new System.Drawing.Point(207, 236); this.supButton12.Name = "supButton12"; this.supButton12.Size = new System.Drawing.Size(75, 23); this.supButton12.TabIndex = 13; this.supButton12.Text = "supButton12"; this.supButton12.UseVisualStyleBackColor = true; // // CharMap // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange; this.ClientSize = new System.Drawing.Size(591, 436); this.Controls.Add(this.supButton12); this.Controls.Add(this.supButton11); this.Controls.Add(this.label3); this.Controls.Add(this.textBox3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Location = new System.Drawing.Point(900, 450); this.Name = "CharMap"; this.StartPosition = System.Windows.Forms.FormStartPosition.Manual; this.Text = "CharMap"; this.Load += new System.EventHandler(this.CharMap_Load); this.ResumeLayout(false); this.PerformLayout(); } private void supButton11_Click(object sender, System.EventArgs e) { throw new System.NotImplementedException(); } #endregion private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.Label label3; private ClassLibrary2.SupButton1 supButton11; private ClassLibrary2.SupButton1 supButton12;

    Here is the code from the supbutton dll file.

    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Drawing;
    namespace HexCalcChar
    {
        class SuperButton : Button
        {
            private bool isSuper = true;
    
    
    
            [Description("Gets or sets whether or not the control is Super")]
            [Category("Super Stuff")]
            [DefaultValue(true)]
            public bool IsSuper
            {
                get { return isSuper; }
                set { isSuper = value; SetSize(); }
            }
    
    
    
            void SetSize()
            {
                this.Size = isSuper ? new Size(50, 50) : new Size(23, 23);
            }
    
    
    
            protected override void OnMouseClick(MouseEventArgs e)
            {
                base.OnMouseClick(e);
                isSuper = !isSuper;
                SetSize();
            }
    
    
    
            protected override void OnLayout(LayoutEventArgs levent)
            {
                base.OnLayout(levent);
                this.Size = isSuper ? new Size(50, 50) : new Size(23, 23);
            }
           
            public SuperButton() { SetSize(); }
    
            private void InitializeComponent()
            {
                this.SuspendLayout();
                this.ResumeLayout(false);
    
            }
        }
    }



    michael r demulling

    Saturday, September 29, 2012 10:32 AM
  • With two changes, your code works for me fine.  Without these changes, the code 1) does not compile, and 2) raises the possibility of where the error lies.
     
    In two places within the designer.cs file, you have:
     
       this.supButton11 = new ClassLibrary2.SupButton1();
       this.supButton12 = new ClassLibrary2.SupButton1();
     
    and later
     
       private ClassLibrary2.SupButton1 supButton11;
       private ClassLibrary2.SupButton1 supButton12;
    The class name is SuperButton, not SupButton1.  When I changed both to "SuperButton" the code compiles and the buttons work as you seem to expect.
     
    Your code should not compile based on the code provided unless you have another class somewhere named SupButton1 in ClassLibrary2.
    --
    Mike
    Saturday, September 29, 2012 12:25 PM
  • I figured it out. I wasnt rebuiding it when i made the changes to the dll file. Now it works when i click on it. When i checked the properties the grow and shrink was on grow only. I think that had something to it to. thank you so much for your help. My next question is how do i make it so that there is only one button big at a time. I already know the long way which is to make 255 small and then make the one big for the click event. in other words make a click event for each of the 256 buttons then in each click event make the other 255 small and that click event big. Maybe there is another way you can think of?

    michael r demulling

    Sunday, September 30, 2012 12:00 PM
  • By the way i still couldnt get your dll code to compile the only one i could get to work was my original dll which i provided the code for you. Your code has missing info on size and a few other errors i couldnt correct.Thanks later.


    michael r demulling

    i got the code to compile that you provided, but, it wont load into the toolbox. it says there is no components in the dll to load into the toolbox?

    later

    michael r demulling

     
    Sunday, September 30, 2012 12:47 PM
  • I already suggested using a new class that manages the state of N SuperButtons, such as a SuperButtonCollection, in my first post.  Since this is a new and different question, and this thread is already fairly long, please ask that question in a new post.

    --
    Mike
    Sunday, September 30, 2012 3:46 PM