# .NET and More > WPF, WCF, WF >  [RESOLVED] WPF Marquee control

## obi1kenobi

In a recent WinForms project, I used a marquee control and I really liked the outcome, so now I'd like to create one for WPF. Unfortunately, the hobby project I'm working on is my first WPF project, so I'm at a loss as to how to proceed...

Has anyone created such a control for WPF? Or, if not, can anyone point me in the right direction?

----------


## DeanMc

Heres a useful link: http://jobijoy.blogspot.com/2008/08/...e-control.html

----------


## obi1kenobi

It seems great, although I do have one question... Is it possible to make the marquee text start entering from the right as soon as it starts disappearing from the left? You know, like those news marquees on TV - they don't have an empty patch when the entire text moves through it once...

----------


## DeanMc

I imagine so but that would mean keeping track of each letter to see if it is visible and if not to place it on the other side of the control!

----------


## obi1kenobi

If there is no other way, then so be it... How can I do it?

----------


## DeanMc

There is storyboarding but I have not much on this, Chris128 may be able to help better!

----------


## obi1kenobi

Ok then, I hope he'll come by  :Smilie:  Thanks for your help  :Smilie:

----------


## obi1kenobi

I've been working on the code provided in the link, however I've discovered several rather worrying flaws, the most prominent being that when the text exits the bound of the marquee control, it is still drawn! :S This means that this marquee may only be used for cases where the marquee stretched from one end of the form to the other... :S Instead of overlaying other controls over the sides of the marquee (the obvious workaround), is there a more systematical way of solving this issue?

----------


## chris128

What exactly do you want to 'marquee' ? I mean is this just for a one off or do you want to create this marquee control so that you can then use it in several projects and stick basically any control in it and then make that control appear to marquee across the area of your marquee control? Also will it always be the same size or does it need to be resizable?

----------


## obi1kenobi

If possible, I'd like to create a reusable, resizable control which may hold any type of control within and animate it.

----------


## chris128

I'll see if I can put something together but Im not promising anything  :Smilie:

----------


## obi1kenobi

Thanks mate  :Smilie:  Much appreciated  :Smilie:

----------


## chris128

Hmm tis harder than I thought!
Have you had a look at this? http://social.msdn.microsoft.com/For...4-4ee3bf55a679
They are only talking about animating a textblock but I would assume you could get it working for anything, I just havent had chance to try it yet since I found that link

----------


## obi1kenobi

I'm new to WPF so I have only a faint idea as to what a TextBlock is... But I completely agreed with this part of the thread:



> For a true headline scenario, this isn't very useful... at least on CNN the marquees are routinely much longer than the screen width for a single headline.


since it is exactly the reason I had in mind in the first place (post #3).

I'll have a look at it, it may be exactly what I need. Thanks a lot for the link!

----------


## Arrow_Raider

I have created a marquee that tiles the content so it works at all sizes. You can give it any kind of content you want, but note that the user will not be able to interact with the content (for example, if you make the content a button, the user won't be able to click the button).

----------


## obi1kenobi

Why not?

----------


## chris128

Nicely done Arrow_Raider  :Wink: 

obi1 - I _think_ the reason why you cant interact with the content is because he is using a VisualBrush to basically project the controls onto the surface of the rectangle. So what you are seeing moving is not technically the controls, just their appearance rendered by the visual brush (VisualBrushes are actually very useful in a lot of situations).

There must be some way to get it working with interactive content though..

----------


## obi1kenobi

But the click should be caught by the rectangle, right? Since chasing the text doesn't seem very attractive...

----------


## Arrow_Raider

Yes, you can catch the marquee mousedown event, but just remember that this event is raised when any non-transparent area of the marquee is clicked on.

----------


## chris128

> But the click should be caught by the rectangle, right? Since chasing the text doesn't seem very attractive...


Ah yes but thats not what we were talking about when we said interactive content. In WPF you can put pretty much any control inside any other control, so we were talking about if you had a few buttons scrolling in this marquee then you wouldnt be able to capture the individual button clicks.
If you just want to have one click anywhere on the marquee do the same thing then thats not a problem  :Smilie:

----------


## obi1kenobi

Oh yeah, it seems so obvious when I read post #15 now...  :Smilie:  Thanks a lot for all your help guys, I look forward to testing it as soon as I get home  :Big Grin:

----------


## obi1kenobi

I tried philipsh's solution from the link you provided and it seemed really good. I also tried Arrow_Raider's solution and I really liked the effect. So I wonder if it would be possible to tile several textblock controls next to each other and have them move to the beginning of the line as soon as they are scrolled out of view by the animation...

----------


## chris128

Huh? Isnt that what already happens?

----------


## obi1kenobi

Arrow_Raider's code does that however exactly as you pointed out, clicks are not captured. I meant being able to line up several textblocks in a news ticker-like fashion and then be able to redirect the user to the exact piece of news they clicked - the best of both worlds.

----------


## chris128

Oh why what does the other one do that I sent you a link to? I thought that did the same thing

----------


## obi1kenobi

You mean in post #2? Well it doesn't repeat the text like Arrow_Raider's control, does it?

----------


## Arrow_Raider

You can't display a control multiple times. It only allows one visual to be displayed. To display it more than once, a visual brush must be used. You may be able to create new instances that are copies of the control and load an array of duplicates into an itemscontrol, but this seems hack-like and cludgy to me.

----------


## chris128

Arrow - he is not talking about displaying the same textblock over and over, he means having different textblocks in the same marquee (if thats what you were referring to anyway)

----------


## obi1kenobi

Yes, chris128 is right, that's what I meant. I believe I can put something together from all the code in this thread and the links chris provided. Thank you both for your help.  :Smilie:

----------


## Arrow_Raider

If you want to have multiple textblocks, you can use an itemscontrol to do that.

----------


## obi1kenobi

Ok I finally created a marquee which displays the effect described in post #3 using parts from both Arrow_Raider and the link provided by chris128. I must warn you though, the code is messy since I have limited experience with C# but I didn't want to port everything to VB... I'm open to suggestions as to how to improve the control  :Smilie: 

MarqueeControl - Custom.zip

----------


## obi1kenobi

I'm having some issues with the marquee control, as soon as I put it in a project I want to use it in, I get the following runtime error: InvalidOperationException - '_brushTransform' name cannot be found in the name scope of 'System.Windows.Shapes.Rectangle'.

Any ideas?

----------


## chris128

What code are you using? The code in that solution you attached in the previous post?

----------


## obi1kenobi

Yes, but the part that errors out is copy-pasted from Arrow_Raider's solution.

----------


## chris128

Well your solution file above works fine for me and I assume it does for you, so how are you 'putting it in another project' exactly?

----------


## obi1kenobi

I moved all controls to a WPF control library project, compiled a dll and then referenced it in my current project. That resulted in a cascading failiure and in the end I couldn't even add the control to the form. Then I ported all relevant code from the marquee to VB using Reflector and added the VB code files directly into the project. And now as soon as the storyboard is started, the error message above pops up. I triple-checked the code I ported to VB and as far as I can see it is completely identical to the C# code. I can attach it here if you'd like to take a look at it as well.

----------


## chris128

I dont know why you wouldnt of been able to just make a DLL and reference it... but yeah can you post your current code here?

----------


## obi1kenobi

Here it is  :Smilie: 

MarqueeVB.zip

----------


## obi1kenobi

I just rebuilt the DLL and added it to the project (after making a backup copy) and this time the app starts up just fine but the marquee doesn't scroll, the text just stands still. What gives? When I set the duration property, the storyboard is supposed to start, yet there is no animation... :S

----------


## chris128

Well I've built it into a DLL and added it to a VB project successfully... but it just doesnt seem to do anything :S

----------


## obi1kenobi

And yet in the solution I posted above - "MarqueeControl - Custom", the marquee was working exactly as it was supposed to. The code I used was the same in both instances... :S

----------


## chris128

No I meant I used the first one and just compiled the Marquee control class (and the rectangle converter bit) into a DLL. I've now added a reference to that into a new WPF project and created an instance of it on my window using pretty much the exact same XAML as in your project, and then copied the code that sets the text and width etc into the Window_Loaded event but it just doesnt show anything when I run it. No errors or anything, just a blank window :S

----------


## obi1kenobi

Ok I am definitely going to commit suicide... I've had headaches for two days over this silly thing, only to find out that instead the MarqueeContent property, I've been using the Content property, both via code and via XAML... XC

I'm really sorry for the confusion this has caused... The code now works fine... Thank you for your patience =)

----------


## chris128

lol im glad you got it working but I WAS using the MarqueeContent property and still couldnt get it to work!  :Smilie:

----------


## obi1kenobi

That's weird :S On my pc it works just fine now...

----------

