Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Fading in/out a text box in VB10


Reply
 
Thread Tools Display Modes
  #1  
Old 06-02-2010, 06:13 PM
brandoncrossman brandoncrossman is offline
Newcomer
 
Join Date: May 2010
Posts: 6
Default Fading in/out a text box in VB10


Anyone got any ideas on how i would go about fading in/out a textbox? Because only forms have opacity properties i was thinking of something like setting the background color to transparent and setting the forecolor of the text to the same as the background color. Then using a timer i could slowly change it to black, etc. Any ideas how i would code that? Or any other ways to accomplish this task? Thanks.

Last edited by brandoncrossman; 06-02-2010 at 06:18 PM.
Reply With Quote
  #2  
Old 06-03-2010, 12:04 PM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Wigan, UK
Posts: 1,676
Default

In all honesty you will probably find it a struggle to get that kind of visual effect working in a typical winforms app as you will really be battling against the system. You might consider WPF as a platform as this kind of thing is trivial to implement on that platform.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 06-03-2010, 12:09 PM
Roger-U Roger-U is offline
Regular
 
Join Date: Apr 2008
Location: Surrey, UK
Posts: 74
Default

You could try putting the textbox on a borderless (sub)form ...
just a quick thought, it should be workable.. i think
Reply With Quote
  #4  
Old 06-03-2010, 03:18 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,415
Default

I'm a fool so I tried out a solution. Here's a trip report.

WPF
Easy peasy. I wrote maybe 6 lines of code to create a DoubleAnimation towards 0 or 1 and start it.

Windows Forms
I had to look at a previous post for some guidance. My plan was to create a control that can fade in or out, then create a UserControl that hosts a TextBox and that fading control. The idea is that on a form with a solid background, fading the background in over a text box is going to look like the text box is fading out.

The fading control was relatively easy, but it's only because I've written something like it before. It took about 200 lines of code and requires knowledge of how to generate the correct opacity value via linear interpolation of the elapsed time vs. desired duration. It also performs cruddy because short-fuse timers are murder on the CPU, but at least it didn't flicker.

The user control presented some problems and ultimately was the reason I quit. Apparently even though the fading control will refresh itself from within the form, the user control didn't know about this and wouldn't show the animation. I could have solved this by giving the fader an event that the user control listened to, and there's probably a couple of other solutions. After spending an hour or so on the fader, I decided it just wasn't worth finding out why WinForms wasn't doing what I wanted.

My solution wouldn't have supported any kind of overlaying of the "fading" textboxes anyway. It was more of a fun experiment than a serious try. If you want transparency and fading, use WPF. WinForms is built on GDI+, which was built on GDI. GDI was designed for a world with dramatically less powerful hardware. It's best at rendering rectangles that don't have transparency, and relatively decent at rendering controls with transparency if they don't overlap. Start asking for alpha blending and overlapping controls and you start having to resort to dirty hacks in WinForms.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 06-03-2010, 10:12 PM
brandoncrossman brandoncrossman is offline
Newcomer
 
Join Date: May 2010
Posts: 6
Default

Quote:
Originally Posted by AtmaWeapon View Post
I'm a fool so I tried out a solution. Here's a trip report.

WPF
Easy peasy. I wrote maybe 6 lines of code to create a DoubleAnimation towards 0 or 1 and start it.

Windows Forms
I had to look at a previous post for some guidance. My plan was to create a control that can fade in or out, then create a UserControl that hosts a TextBox and that fading control. The idea is that on a form with a solid background, fading the background in over a text box is going to look like the text box is fading out.

The fading control was relatively easy, but it's only because I've written something like it before. It took about 200 lines of code and requires knowledge of how to generate the correct opacity value via linear interpolation of the elapsed time vs. desired duration. It also performs cruddy because short-fuse timers are murder on the CPU, but at least it didn't flicker.

The user control presented some problems and ultimately was the reason I quit. Apparently even though the fading control will refresh itself from within the form, the user control didn't know about this and wouldn't show the animation. I could have solved this by giving the fader an event that the user control listened to, and there's probably a couple of other solutions. After spending an hour or so on the fader, I decided it just wasn't worth finding out why WinForms wasn't doing what I wanted.

My solution wouldn't have supported any kind of overlaying of the "fading" textboxes anyway. It was more of a fun experiment than a serious try. If you want transparency and fading, use WPF. WinForms is built on GDI+, which was built on GDI. GDI was designed for a world with dramatically less powerful hardware. It's best at rendering rectangles that don't have transparency, and relatively decent at rendering controls with transparency if they don't overlap. Start asking for alpha blending and overlapping controls and you start having to resort to dirty hacks in WinForms.
hmm. i know nothing about wpf. plus ive pretty much finished coding my entire application in winform. So can you use wpf in winform? or would i have to recode the entire application in wpf?
Reply With Quote
  #6  
Old 06-04-2010, 09:58 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,415
Default

After a little bit of experimentation, I would not recommend going down that path. First, whether you have to rewrite the application in WPF or not.

You're not the first person who has ever had to face the possibility of rewriting their UI. Microsoft's tools do not discourage you from placing application logic inside your forms (one could argue they encourage it!) When you do this, you will almost always end up coupling application logic with a specific UI. What do I mean? "Application logic" refers to all of the actions that your application takes that has nothing to do with the UI. This is concepts like "select a customer", "save a file", or "send an email". Another way to look at it is to imagine the application with a different UI; the things that it still does are probably application logic.

When you implement your application logic as part of your UI, you end up coupling application logic to that specific UI. For example, when you implement "select a customer" in a form, you will likely see it as "what happens when lstCustomers.SelectedIndex changes". This is suboptimal for a few reasons. The first is the problems it causes when you want to provide multiple ways to change the selection; what if you add a search dialog that presents an alternate mode of selection? This puts you in danger of duplicating code which has its own set of problems. The next big problem happens when you want to change something about your UI. Maybe you bought a third-party ListBox that has more features and you want to replace your current list box with it. If this third-party ListBox has different properties and events than the built-in ListBox, you'll have to rewrite your code. The same can happen if you decide to move from a ListBox to a ListView or some other control. Finally, it's hard to test application logic that's coupled with the UI. Automated tests can help discover bugs and validate that none exist, but it is very difficult to run automated tests against a UI.

The solution would take up too much discussion, but I can summarize. Software engineering has produced presentation model patterns. These are techniques for separating application logic from the UI in a way that makes it possible to change the UI without changing application logic. A popular pattern for WinForms is the MVC, or Model-View-Controller, pattern. In this pattern, you tend to have three types of classes associated with a form. The Model is the data that the form will work with; you'll likely have many Model classes. The View is something that can display the data; this is the form itself. The Controller contains all of the application logic for a particular View. I'd love to demonstrate, so if you're curious please ask.

Why the heck did I spend 3 paragraphs off-topic like this? If you were using a presentation model pattern it would be much easier to switch from WinForms to WPF. (This is ignoring the fact that you have no experience with WPF; it's very different.) With a presentation model pattern, "select a customer" would expect the UI to figure out the UI details and provide the selected customer to a controller or other application logic class. This means you can replace the UI with anything that knows how to send the selected customer to the application logic. I've used these patterns to make applications that have both a scriptable console UI and a GUI with great success.

Fine. But what about hosting WPF content in WinForms?
It's doable, but I wouldn't recommend it. First theory, then practice.

To host WPF content in WinForms, you must start by creating a WPF UserControl with the content you want to host. The ElementHost control (look for the "WPF Interoperability" tab in the toolbox) is a Windows Forms control that can host WPF content. After you place the element host on the form, you set its Child property to an instance of the WPF user control you created. The designer helps you with this process via smart tags. After you tell the element host what to host, you can work with the hosted content as if it were a control in Windows Forms.

In this example, I created a FadingTextBox that adds a FadeOut() and FadeIn() to the WPF text box. I hosted this in a user control named FadingTextBoxContainer and provided a couple of properties to make it easier to use. The ElementHost is configured to have a transparent background in the hopes that when the textbox fades the whole thing will be transparent.

Here's my opinions.
  • It's very shaky in the VS 2008 designer. Veterans of hosting ActiveX controls in WinForms won't be surprised at this. Once I dropped the ElementHost, the designer became unreliable. Every time I build the project, the designer quits loading until I close VS and delete some temporary files it creates. If you rely heavily on the designer, you'll have a hard time using this technique. It's much better in VS2010; I am leaving this point here because many people still use VS 2008.
  • I have some application stability concerns. The application threw an exception when I closed it a couple of times. I have no idea if this is a fluke or something to be concerned about; you should test extensively if you use it.
  • It's still not true transparency. No matter how you slice it, ElementHost is still a Windows Forms control and still subject to the halfway-supported transparency. I'm pleased to see that it works against an image, but as the example shows you can't see controls it overlaps. WPF handles this just fine, in WinForms it takes monumental effort. If you need overlapping, it's still a dead-end.

In short, I think it's shoving a square peg into a round hole. I can't name an application built on GDI or WinForms that uses animation or transparency effectively. It's probably best to just do without the fade.

*edit*
My original attachment was for VS 2008, I added a VS 2010 project.
Attached Files
File Type: zip WpfFadingInWinForms.zip (14.1 KB, 4 views)
File Type: zip WpfFadingInWinForms (2010).zip (14.5 KB, 17 views)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.

Last edited by AtmaWeapon; 06-04-2010 at 10:03 AM.
Reply With Quote
  #7  
Old 06-04-2010, 02:47 PM
brandoncrossman brandoncrossman is offline
Newcomer
 
Join Date: May 2010
Posts: 6
Default

Thanks for the advice. Giving this thought, i probably should of created my program with flash, and then embeded it in a winform. But i'll just have to do without the fading, i dont want to have to deal with any stability concerns. I dont understand why microsoft cant include an opacity property for the controls themselves.
Reply With Quote
  #8  
Old 06-04-2010, 02:53 PM
lordofduct's Avatar
lordofduct lordofduct is offline
Centurion
 
Join Date: May 2010
Location: south florida yo
Posts: 120
Default

Quote:
Originally Posted by brandoncrossman View Post
Thanks for the advice. Giving this thought, i probably should of created my program with flash, and then embeded it in a winform. But i'll just have to do without the fading, i dont want to have to deal with any stability concerns. I dont understand why microsoft cant include an opacity property for the controls themselves.
because it's an expensive job, and GDI isn't new fangle technology. It's been around since back when we had computers that couldn't even push more then 16-bit colour... like my family computer back in high school. Ugh... stupid hewlett packard... BURN BURN I SAY!
__________________
I'm a turtle... woooo
Reply With Quote
Reply

Tags
transparency


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->