14 Replies - 1889 Views - Last Post: 26 April 2012 - 11:45 PM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Can You Create Custom Events Within A GUI Object Like A Rich Text Box?

Posted 24 April 2012 - 04:30 PM

So I'm looking for a solution but I'm not sure how to do it. I've seen custom events for someone's own class, but I'm looking for an event that could be fired or a solution with a RichTextBox.

So I'm programming a text editor and there's a problem. If I set the selectionfont within my rich text box to say bold by check marking a box then it turns to bold which is fine, but what happens and hopefully people can understand this is is a section of text hasn't been turned to bold and the user clicks or moves back there then the text starts typing as non-bold

but the checkbox remains checked which at the very least is dorky, but then the user has to un-check and re-check the box to get it to start typing bold again.

One event that DOES work is using the OnLocationchange which you could then use something like

if (txtbox.SelectionFont.Bold == true && checkbox.checked == false)
{
    checkbox.checked = true;
}



So in the event the user changes location to text anywhere that is bold and the box isn't checked it automatically gets checked again:: if you notice modern day office programs will select and un-select bold etc. as you click around.

The problem I see with using the OnLocationchange event is you're constantly firing that event which seems to be resource heavy am I right? So anytime you do ANYTHING within that RichTextBox it's constantly checking to see if the SelectionText is bold etc. but that seems inefficient?

That said there doesn't exist and event; but could you create a onselectionFontChange event. This would only fire an event when the selection text changes not every time the location changes?

Also what are other solutions to this problem?

Thanks in advance guys.

Is This A Good Question/Topic? 0
  • +

Replies To: Can You Create Custom Events Within A GUI Object Like A Rich Text Box?

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,191
  • Joined: 02-June 10

Re: Can You Create Custom Events Within A GUI Object Like A Rich Text Box?

Posted 24 April 2012 - 04:44 PM

I think the easiest solution is to stop using checkboxes.
Just have a button for "Bold" and have it toggle the selected text: If bold then becomes unbold, if unbold becomes bold.

Now you don't have a synchronization issue between your toolbar and your selected text.

Otherwise, couldn't you just wire to the .Selectionchanged event of the RTB? That's what is happening, right? The selection is changing when the user moves right and de-selects the block that is bold.
http://msdn.microsof...ionchanged.aspx
Was This Post Helpful? 1
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Can You Create Custom Events Within A GUI Object Like A Rich Text Box?

Posted 24 April 2012 - 05:53 PM

Have you considered using the FontDialog control?
Was This Post Helpful? 1
  • +
  • -

#4 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Re: Can You Create Custom Events Within A GUI Object Like A Rich Text Box?

Posted 24 April 2012 - 06:13 PM

Thanks guys. No what fontdialog control? There's many ways to change the font but the point is something has to be pressed if the user is going to change something and if a user then clicks on text within that rich text box that isn't bold then the selection font changes to not being bold whilst the control button box nob etc will still show bold is on.

Also is there a way to create custom controls within a GUI element like a rich text box? I'm not even saying that's the right solution to this but I'm just curious?
Was This Post Helpful? 0
  • +
  • -

#5 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Can You Create Custom Events Within A GUI Object Like A Rich Text Box?

Posted 24 April 2012 - 06:22 PM

You could build a class to inherit from RichTextBox and add things there.
So you don't know how to change the font? You should seriously look at System.Drawing.Font.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,191
  • Joined: 02-June 10

Re: Can You Create Custom Events Within A GUI Object Like A Rich Text Box?

Posted 24 April 2012 - 06:38 PM

Did you know there is a tutorial here for making a custom text editor?
Complete with handling fonts and issues like the one you described.

http://www.dreaminco...l-step-by-step/
Was This Post Helpful? 1
  • +
  • -

#7 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Re: Can You Create Custom Events Within A GUI Object Like A Rich Text Box?

Posted 24 April 2012 - 08:27 PM

I think people might be missing what I am saying on this one but +1 for the tutorial I will definitely have a look thanks. I know how to change text that's NOT THE ISSUE the issue I'm dealing with is complex it appears and almost hard to explain. If you just change a textbox's font or style to bold you change everything within it so it's only logical for an editor to just change selection font but the problem arises like I said when you click somewhere else in the program that doesn't have bold text and thereby the selection font just by going there in the textbox automatically will change the fontstyle back to normal WITHOUT CHANGING the GUI ELEMENT THAT ALLOW THE USER TO SET IT.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,191
  • Joined: 02-June 10

Re: Can You Create Custom Events Within A GUI Object Like A Rich Text Box?

Posted 24 April 2012 - 08:43 PM

Quote

the problem arises like I said when you click somewhere else in the program


I think you are so focused on trying to add in your own event you are ignoring the events already in the RTB that you can use.

Have you tried using the .Selectionchangede vent to trigger your GUI update?
That's what happens when the user deselects text: The selection changes. When they click elsewhere in the RTB, and the selected text becomes deselected that will raise a .Selectionchanged event.

If the selection size is 0 then none of your boxes really should be checked - or you have to make a decision about which side of the cursor to honor, because it could be different on the left than the right
Was This Post Helpful? 1
  • +
  • -

#9 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Re: Can You Create Custom Events Within A GUI Object Like A Rich Text Box?

Posted 24 April 2012 - 10:53 PM

View PosttlhIn`toq, on 24 April 2012 - 08:43 PM, said:

Quote

the problem arises like I said when you click somewhere else in the program


I think you are so focused on trying to add in your own event you are ignoring the events already in the RTB that you can use.

Have you tried using the .Selectionchangede vent to trigger your GUI update?
That's what happens when the user deselects text: The selection changes. When they click elsewhere in the RTB, and the selected text becomes deselected that will raise a .Selectionchanged event.

If the selection size is 0 then none of your boxes really should be checked - or you have to make a decision about which side of the cursor to honor, because it could be different on the left than the right


Well like I said friend I did successfully use the locationchange event within the Rich Text Box. This Does work by simply checking the new selection font and then automatically checking or unchecking the bold italic etc. Do you see what I'm saying? As soon as you click somewhere else in a textbox if that text is bold then the selection font turns bold AUTOMATICALLY so if you have a button switch etc. that controls the bold and it's off it should be immediately be checked back to on.

This idea of mine works but it concerns me because location changes happen even as the user is typing just normal text like I'm doing in here ;). Meaning that it will keep firing that event over and over and over most of the time meaninglessly just to see if selectionfont for that textbox has changed because the user has arrowed or down, or clicked where the text is different than the current text the user is in, and thereby changing the status of a checkbox or button etc because the selection font has changed.

I can't emphasize that enough if you have some text for instance at the top of a textbox that was typed in BOLD and you're typing below it in NORMAL font and all of a sudden you see a word you misspelled in the BOLD TEXT as soon as you click up there to correct it the selection font changes to bold automatically. That is when they click up there or move the clinking vertical line i.e. the cursor to the BOLD.

Am I right to think it is then bad to use that event which continuously fires for most of the time probably no reason?

This post has been edited by adn258: 24 April 2012 - 10:59 PM

Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,191
  • Joined: 02-June 10

Re: Can You Create Custom Events Within A GUI Object Like A Rich Text Box?

Posted 25 April 2012 - 11:04 AM

There's really nothing wrong with it. The event fires regardless. Just like KeyPressed, KeyDown and so on.

Unless you are doing a massive amount of pressing in your handler it shouldn't degrade performance in the slightest.
Was This Post Helpful? 0
  • +
  • -

#11 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Re: Can You Create Custom Events Within A GUI Object Like A Rich Text Box?

Posted 25 April 2012 - 07:15 PM

View PosttlhIn`toq, on 25 April 2012 - 11:04 AM, said:

There's really nothing wrong with it. The event fires regardless. Just like KeyPressed, KeyDown and so on.

Unless you are doing a massive amount of pressing in your handler it shouldn't degrade performance in the slightest.


Oh ok? So even though the event changes every time you keypress that doesn't matter? I will consider that then friend also I might just use the default font panel now I have two options that will/would work.

Also just on a total curiosity note I've been looking at like

Tutorial

I'm curious about them and just for curiosity purposes I have been able to make simple custom events like the one provided in the tutorial above but what about adding events to things like a RichTextBox beyond what is already provided like OnLocationchange? Is that possible and how would you do it? I haven't the foggiest idea:: This is just because I'm curious as I think it's something I'd like to know how to do?

This post has been edited by adn258: 25 April 2012 - 07:16 PM

Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,191
  • Joined: 02-June 10

Re: Can You Create Custom Events Within A GUI Object Like A Rich Text Box?

Posted 25 April 2012 - 07:36 PM

You can't easily just add to existing classes. What you can do is make your own class, inheriting from the RTB and add all the properties, methods and events you like.

The link you provided is ok. Maybe I'm prejudice, but I like the tutorials I wrote better. Plus the second two show how to make custom controls, do inheritance etc.

Quick and easy custom events

Bulding an application - Part 1

Building an application - Part 2
Was This Post Helpful? 1
  • +
  • -

#13 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Re: Can You Create Custom Events Within A GUI Object Like A Rich Text Box?

Posted 26 April 2012 - 01:47 PM

Helpful as heck. Thanks for that. I get how those work but I don't how you'd extend a RTB class etc. with your own custom events that doesn't make sense to me friend. IS there like an example you could give or something you could point me too? I wouldn't know where to start on that one.

This post has been edited by tlhIn`toq: 26 April 2012 - 02:10 PM
Reason for edit:: No need to quote the entire previous message

Was This Post Helpful? 0
  • +
  • -

#14 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,191
  • Joined: 02-June 10

Re: Can You Create Custom Events Within A GUI Object Like A Rich Text Box?

Posted 26 April 2012 - 02:14 PM

public class customRTB : RichTextBox
{

  // Now make your custom events and properties and methods.
  // Use the events tutorial:
  // http://www.dreamincode.net/forums/topic/176796-quick-and-easy-custom-events/

        public event EventHandler Sproing;
        public void RaiseSproing()
        {
            EventHandler handler = Sproing;
            if (handler != null) handler(this, EventArgs.Empty);
        }


}




If what you really need is an explaination of the concepts of classes as the question suggests, maybe this will help:

Think of objects in coding just as you would objects in the real world.

A Dodge Ram is an object.
It is made up of smaller objects: Engine, doors, tires
Each of those is made up of smaller objects: Bolts, pistons, etc.

Objects in coding can inherit from each other, usually from the general to the specific.

  • Class vehicle
    • Class truck : vehicle
      • Class Ram : Truck
        • class 2500FWD : Ram


A class is the blueprint for instanciating (making an instance of) the object.

DodgeRam is a class describing how to make an instance, but itself is not an actual thing you can interact with.

myDodgeRam is an instance of a the class DodgeRam

I can do things with the object instance myDodgeRam

myDodgeRam.SerialNumber = 123456789;
myDodgeRam.FillUpTank();
float fuelLevel = myDodgeRam.FuelTankPercentageFull;
if (myDodgeRam.IsReady) myDodgeRam.StartEngine();


Anything defined in the base class is available to a child.

class truck : vehicle
{

    public float FuelTankPercentageFull
    {
       get; set;
    }
}

class DodgeRam : truck
{
    // I don't have to define a FuelTankPercentageFull here because I inherit it from my parent
}


Methods defined as virtual in the base class can be overridden by the child class (at least in .NET languages like C#). This is often to account for more specific needs.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Do something to start the engine
       return true; // No checks or requirements
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return true;
        }
        return false;
    }
}


You can even have a child class call the base classes methods which is often the smart way to go.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Notice there are no safety checks before trying to start up.
       try
       {
          // Do something to start the engine
          return true; // because we succeeded
       }
       catch(exception error)
       {
           return false; // because there was an error
       }
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return base.StartEngine();
            // Now all the electrical work is in the base class
            // and not repeated in every child.
        }
    }
}

Was This Post Helpful? 1
  • +
  • -

#15 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Re: Can You Create Custom Events Within A GUI Object Like A Rich Text Box?

Posted 26 April 2012 - 11:45 PM

No I understand objects friend and I'm getting better with understanding that sort of thing and I use virtual classes etc. all the time and override them, but I don't see how you would set new events for a rich text box. I tried doing that already via using something like what you said; I tried tinkering with it creating my own class and using : Richtextbox

or my textbox name as a child class but I don't understand the syntax and schematics of how you would do those specific events. Events for your tutorial simple handy things like that I get and I get objects for the most part.... Thanks for your help by the way +1 dude
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1