DCSIMG
May 2007 - Posts - Just code - Tamir Khason

May 2007 - Posts

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/t-mobile-hotspot-at-home/]


I have T-Mobile phone number and they send me a lot of sales spam information. I used not even see in. It goes directly into Sales Spam folder by skipping inbox. But sometimes, when I'm really tiered,  I'm looking into this folder and sometimes some of emails there catch my attention. The happened today. When such service will be available in Israel? As for my opinion, never. It's it's really sucks...

image

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/code-snippets-for-wpf-in-vs2005/]


Extremely useful code snippets for DependencyProperty (with and without handler, read-only and not), Routed and Tunneling events were published by SerialSeb and enhanced by Brownie Points. Download and use - this is great time savers.

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/programmatically-drag-and-drop/]


Why I might want to do drag and drop without moving mouse? Well, unit tests - good enough? Anyhow. Today I'll explain quick way to drag&drop from code. Let's create a window with button and textbox. When the button clicked I'll put into D&D bag some string and then put it into textbox by subscribing to Drop event.

  <StackPanel>
    <Button Click="put">Put</Button>
    <TextBox Name="box"></TextBox>
  </StackPanel>

 

And code

public Window1()
        {
            InitializeComponent();
            Drop+=new DragEventHandler(onDrop);
        }

        
        void put(object s, RoutedEventArgs e)
        { 
             DataObject o = new DataObject();
            o.SetData("kuku");
        }
void onDrop(object sender, DragEventArgs e)
        {
            box.Text = e.Data.GetData(DataFormats.StringFormat).ToString();
        }

 

 

Well done.  Now I should call Ole DragFinish? But hrr, first get handler of my control.. Hr... NO WAY! I'll use DragDrop.DoDragDrop static method. It'll do all the rest. So change put method to something like this to get a result you're waiting for

 void put(object s, RoutedEventArgs e)
        { 
             DataObject o = new DataObject();
            o.SetData("kuku");
            DragDrop.DoDragDrop(this, o, DragDropEffects.All);
        }

 

We done.

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/how-to-use-systemdrawingbitmap-hbitmap-in-wpf/]


Actually, the question is: "How to use nice feature of Resource code generation, presented in VS2005 within WPF". Well, that's not really efficient way, but whatever. As you, probably, know in WinForms (.NET2.0) and Visual Studio 2005 there is code generator for system resources. It makes us able to have intellisense support of VS development environment for loading resources from the application.

Actually, the code generator creates a butch of static properties, that gets objects from application ResourceManager. If you'll take a look into Reesources.Designer.cs file, you'll find there internal static readonly properties like this

internal static System.Drawing.Bitmap p1060981 {
            get {
                object obj = ResourceManager.GetObject("p1060981", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }

 

How to get this and load my images (or any other resources) and use then in WPF this way?

image.Source = ResourceHelper1.Properties.Resources.p1060981;

 

Well, you can not and the main reason is, that it returns System.Drawing.Bitmap, which actually, HBitmap and can not be used directly as image source for WPF. But if you really want try this small helper

public static BitmapSource loadBitmap(System.Drawing.Bitmap source)
        { 
            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(source.GetHbitmap(),IntPtr.Zero,Int32Rect.Empty,
                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
        }

 

It gets System.Drawing.Bitmap (from WindowsBased) and converts it into BitmapSource, which can be actually used as image source for your Image control in WPF.

This is not very efficient way. It's much better to use Resources management of WPF as described here, but in some cases (this one is not reason :) ) you have to use Forms ResourceManager. One of them, is if you need pixel level access to your bitmap.

Source code for this article

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/why-my-chm-file-does-not-work/]


This question is, probably, one of those question, making IT guys crazy. But, actually, this is not really simple question and there are more then one reason for problems with displaying Compiled HTML files. Let's get once forever through the reasons for this

1) You have not real CHM file 
This may be HLP (WinHelp) file for real, so first try to rename it into HLP (if you are not running Vista) and execute. For those happy Vista owners - try using converter (hh.exe). Vista not supports HLP files anymore.

2) CHM viewer component is not registered
Why this can happen? A lot of reason - don't even try to understand. Just run from your command prompt "regsvr32 hhctrl.ocx" if you got "OK" - try to open file again. No result. Look closer to file name

3) Your CHM file has "#" (hash) character in file or directory name
Why this happens? Hash (#) is registered character for in document hyperlinks. This character can not be used (aline with "?","&" and "+" characters. All those have special meaning. Remove all hashes and other special characters from file and directory name

4) You have security update (optional) for Windows XP or Vista that blocks CHM files
After all this file is compiled HTML, so it can consist of harmful scripts. So one of patches block this type of content. As well it might be blocked by system policy, your IT department put on your machine. To solve it right click on CHM file and select Properties. Click "Unblock" button under "Advances" one in "General" tab. Hit "Apply". Once you unblock the file successfully, the unblock button disappear.

5) CHM file is in restricted internet zone
The file might be on remote machine, NetApp, network drive, that exists in restricted zone or you trying to open it directly from unknown internet site. You can either copy it locally or change ItssRestrictions in registry. Run regedit, locate HKLM\SOFTWARE\Microsoft\HTMLHelp\1.x\ItssRestrictions. If the key does not exists - create it. Add new DWORD subkey named MaxAllowerZone and put there 1. Actually, you can use numbers from 0 to 4 (My Computer, Local Intranet, Trusted Sites, Internet Zone, Restricted Zone). In most cases, Local Internet Zone (1) is enough. You should feel comfortable with changing registry settings in order to use this method!

If after all all those methods you still can not open CHM file. Try to open it in Notepad. Sometimes, files, downloaded from internet actually error HTML page of the server. So check it.

One last thing. Be sure, that you paid for ebook, downloaded from one of P2P servers even after you read it as CHM. Writers works really hard to write books for you :)

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/this-is-not-bug-this-is-feature/]


Try to do following. Create ListBox and bind it's items to big collection. Now set height property of the ListBox to some value. After everything binded and collection filled iterate it's items and request it's indexes. How to do it? Simple. iterate items of ListBox and ask about YouListBox.ItemContainerGenerator.ContainerFromIndex(yourIndex). Check results. What we got? All visible items (± some before and after) has right index, but for all invisible items you'll get null response for this method. What's the problem? Is it bug?

The answer is no - this is feature. ListBox (as well as most of other base controls) do not actually draw invisible items. This is great performance advantage. But what to do if you need indexes of hidden items? Put your control inside ScrollViewer. This way your control will think, that it's fully drawn, but actual output will be partially with scroll bar. But if you doing this, know, that it's not really good idea to draw all items, even when they are invisible.

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/end-to-end-expression-studio-rtm/]


Finally, after the dry run of MIX '07 (...and I'm not there :( ), Expression Studio (Expression Blend, Expression Web, Expression Designer and Expression Media) is shipping with RTM tag. The new version fully supports Silverlight. But the most exciting is that Blend 2 May Preview, that you can download starting today works tightly with this new technology

More Posts « Previous page