Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

existing object.custom property

Status
Not open for further replies.

q1212

Programmer
Mar 20, 2009
14
RO
My question is how to create this: for an existing object a new custom property?
"existing object"= an object control in general (ex: a textbox)
"new custom property"= by default this control "textbox" have this property:

Code:
textbox.Hide();
...and many other properties off course,but I snipe to this particular one, only.
In what way? Well, I want that a single button to have a custom property write by me, named "myHideShowProperty" that can do 2 things in one package, I mean that when I click once, "Show" that textbox ; and when I click one more time it "Hide" that textbox, then when I click once more it "Show" a textbox,and so on.
In the end, the code I imagine that it must look like this:

Code:
//im sure this is ilogical but this is how I imagine this thing could be:
btn1.myHideShowProperty.show(textbox);
btn1.myHideShowProperty.hide(textbox);

The idea behind all of this is how to write a customized property for an existing object control.
And of course a solution for my problem, if there is one.
though I must mention that I have made a variant and it works:

Code:
// two buttons on top of each other
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Hide();
            button3.Show();
            button3.Text = "second btn";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button3.Hide();
            button1.Show();
            button1.Text = "first btn";
        }
//but is a silly code,no? :)
 
The same btn click event who can switch between 3 strings like "Aa", "Bb","Cc"?
1 click pop up the first string, second click(same btn) pop up the second string, and the third click(same btn) the last string?
And after the third one it begin with the first string, second,etc.
 
i just read through this a couple times, but i'm missing it.

conceptually, what do you want to happen when the button is clicked? from there we may be able to offer ideas on how to implement it.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
actually I find what I searched for and the answer is like this:
Code:
private void button1_Click(object sender, EventArgs e)
        {            
            if (timer1.Enabled == true)
            {
timer1.Enabled = false; button1.Text = "start"; button1.BackColor = Color.Aqua;
            }
else { timer1.Enabled = true; button1.Text = "stop"; button1.BackColor = Color.Gold; }
        }
//"[""/""code""]"

This code is not entirely what I want to know, but stand for a good piece of code though. :)
But I am interested to CHANGE the property of a control (for example control "button1"). I want to CHANGE It's default properties. If you can find something announce me. Thanks.
 
could you provide the actual scenario/code you want? the above code makes sense (although i would manage the styling via CSS and have this work done on the client, not the server.)

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
no problem, but its not finished yet.
:)
Code:
      #region <GlobalDeclarations+Objects>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Counting
{
    public partial class Form1 : Form
    {
        CountingClass XmyObject = new CountingClass();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            lblShow.Text = "default is " + XmyObject.Maximum.ToString();
        }
        #endregion


        private void button1_Click(object sender, EventArgs e)
        {            
            if (timer1.Enabled == true)
            {
                timer1.Enabled = false; button1.Text = "start"; button1.BackColor = Color.Aqua;
            }
            else { timer1.Enabled = true; button1.Text = "stop"; button1.BackColor = Color.Gold; }
        }
        private void timer1_Tick_1(object sender, EventArgs e)
        {
            #region <mystring(Low/High)>
            string mystring;
            if (XmyObject.Value < 12)
            { mystring = " Low"; }
            else { mystring = " High"; }
            #endregion

            
            label1.Text = XmyObject.Value++.ToString() + mystring;
            progressBar1.Value = XmyObject.Value;
            if (progressBar1.Value < XmyObject.Value)
            {
                progressBar1.Value = XmyObject.Min;
            }
          
             
            progressBar1.Maximum = XmyObject.Maximum;

            #region <restartCount>
            if (XmyObject.Value == XmyObject.Maximum)
            {
                Thread.Sleep(500);
                label1.Text = "Repeat";
                XmyObject.Value = XmyObject.Min; 
            }
            #endregion

        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            XmyObject.Maximum = int.Parse(textBox1.Text);
            lblShow.Text = "changed to "+ XmyObject.Maximum.ToString();
        }

        

    }
}

+ class
Code:
using System;
using System.Text;

namespace Counting
{
    class CountingClass
    {
        int min = 0;
        public int Min
        {
            get { return min; }
            set { min = value; }
        }

        int valoare = 0;
        public int Value
        {
            get { return valoare; }
            set { valoare = value;}
        }

        int max = 20;
        public int Maximum
        {
            get { return max; }
            set { max = value; }
        }
      #region <value&Maximum>
       
        #endregion
    }
}
 
this looks fairly straight forward.
a page with a timer.
as the timer elapses you increment the counter, update the form
the timer can be stopped/started with the click of a button.

the CountingClass is useless though. it's just a data container and any value can be changed on a whim. I would encapsulate logic within this class one possible implementation.
Code:
class Counter
{
   public int Maximum {get; private set;}
   public int Current {get; private set;}

   public Counter(int maximum)
   {
       Maximum = maximum;
   }

   public void Increment()
   {
      Current++;
   }

   public void Reset()
   {
      Current = 0;
   }


   public void Next()
   {
      if(Maximum == Current) 
      {
        Reset();
      }
      else
      {
        Increment();
      }
   }
}
now your logic in contained where it needs to be. If the user changes the Maximum value replace the entire counter.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks.
very clear is your class than mine, I must adapt to its style.
thx again.
 
Code:
public int Maximum { get; private set; }
is the shortcut to the default one:
Code:
int maximum;
        public int Maximum 
        {
            get { return maximum; }
            set { maximum = value; }
        }
yes?
 
based on your class how can you make the
"partial class Form1 : Form", code?
I have no idea how to use your class. :(
please help.
thx.
 
yeee..... I did it....here is the code:
Code:
namespace NewCounting  //yeee..... I did it....here is the code:
{  
    public partial class Form1 : Form
    {
        Counter XxmyObject = new Counter(10);
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            lblShow.Text = "default is " + XxmyObject.Maximum.ToString();
        }
        private void button1Start_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
           #region <mystring("low&high")>
            string mystring;
            if (XxmyObject.Current < 5)
            {
                mystring = " low ";
            }
            else { mystring = " high "; }
            #endregion
           label1.Text = XxmyObject.Current.ToString()+mystring ;
           XxmyObject.Next();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            int i = int.Parse(textBox1.Text);
            XxmyObject.Maximum = i;
            
            #region <txt>
            lblShow.Text = "changed to " + XxmyObject.Maximum.ToString();
            #endregion
        }

 
if I dont ask too much ... can you make me another class from which I can learn stuff? (not too advanced though - im a beginner ~relative~).
In continuation/ with the same line of thought; for this project. Something about automatic executions (in general), and some user input (minimal).
thank you.
 
can you make me another class from which I can learn stuff?
our craft is constantly evolving. new languages, paradigms and frameworks are constantly released. While it's good to learn them, knowing anyone of these will not make you a better programmer. it will only make you good at using a specific tool. once that tool is obsolete, so are your skills.

I would recommend learning about language agnostic concepts.
S.O.L.I.D. design principles and Design Patterns in general are a great place to start. If you are looking for .net resources I recommend and
Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Hy again. I have a new question: I want a string to see it flashing when I click a button. It sound crazy but I actually want to adapt it to a led connected to serial port (rs232) (pin3+/pin5gnd) and to make it like a strobe light (at a fv of 100ms for example). But first I want to make a test with that string (if you happen not to be interested about serialPorts)
Thanks

Code:
private void btnTEST_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                //Port3.BreakState = true;//serialport
                label1.Text = "a"; //I want this letter to flash itself
            }
        }
 
this is off topic from the original post. Post a new message to the c# forum. google is also a great search engine which you can use to find code snippets. piecing them together you can create a solution for your scenario.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top