existing object.custom property

q12

Newcomer
Joined
Jun 24, 2009
Messages
10
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? :)
 
You could create your own button control by inheriting from the existing button class and then adding any functionality you require.

Not entirely sure what you are trying to do from your sample though. Are you just looking to have one button that will update a textbox to one of two states depending on how often it is clicked?
 
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.
 
Easiest way would be to inherit from the button class and then internally track the number of clicks and the textbox to be modifying. Something like the following should get you started...
C#:
class ButtonSample : Button
    {
        private int _ClickCount;
        public TextBox TextBoxThing { get;  set; }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            if (TextBoxThing == null)
                return;
            
            if (_ClickCount >= 2)
                _ClickCount = 0;

            switch (_ClickCount)
            {
                case 0:
                    TextBoxThing.Text = "First text string";
                    break;
                case 1:
                    TextBoxThing.Text = "Second text string";
                    break;
                case 2:
                    TextBoxThing.Text = "Third text string";
                    break;
            }
            _ClickCount++;
        }
    }
 
As far as I see, you are on the right track, impressive; and I am very pleased with the results.
But is a little problem: Im not that advanced than you, please write the implementation in the form for me. This is as far as I can think, of implement it by myself:

private void Form1_Load(object sender, EventArgs e)
//(this is correct? to put it in "Form1_Load" ?)
{
ButtonSample mybtn = new ButtonSample();
textBox1.Text = mybtn.TextBoxThing.Text; (this one I'm not sure is correct)

}
 
this make sense "mybtn.TextBoxThing = textBox1;" because I point to something specific(textBox1) but I cant figure out how to poit to button1.
I must assign somehow the mybtn obj to the button1 control.
how to do that?
thanks.
 
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


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
}
}
 
Back
Top