DCSIMG
August 2008 - Posts - IronShay

August 2008 - Posts

הזמנה להרצאה

ב 8.9 אני הולך להעביר הרצאה ב Office User Group וכולכם מוזמנים!

בהרצאה אני הולך ללמד על למה אופיס כבר מוכן שתפתחו (גם מלשון להיפתח וגם מלשון לפתח!) אליו, מדוע אתם עדיין לא עושים את זה ומאיזה סיבות ממש כדאי לכם לעשות את זה.

ההרצאה פתוחה לכל המתעניינים ובפרט לאלה מבינכם שהרהרו פעם על האפשרות להתחיל לפתח לאופיס, אבל באותו לילה קמו מסיוטים נוראיים על אטבים שמציעים עזרה.

יהיה כיף!

קישור חשוב: דף ההרשמה להרצאה

נתראה בקרוב,
שי. 

Posted by shayf | with no comments
תגים:

Tip: SuspendLayout and ResumeLayout in Office Applications

When you develop WinForm applications, and you want to update various UI elements, it'd be better to call Form.SuspendLayout before the updates and Form.ResumeLayout after the updates.

When developing in Office environment, you don't have these methods... But it doesn't mean that you can't achieve the exact behavior!
When you want to suspend the layout, execute the following line of code:

Application.ScreenUpdating = false;

When you want to resume the layout, execute:

Application.ScreenUpdating = true;

That's it!

WARNING: if you set the ScreenUpdating to false and never set it to true again, your office application editing surface will freeze and the user will have to restart the application... I've come up with a solution for that. Add the following class to your application:

public class ScreenSuspender : IDisposable
{
    public static IDisposable SuspendLayout()
    {
        return new ScreenSuspender();
    }

    private ScreenSuspender()
    {
        Globals.ThisDocument.Application.ScreenUpdating = false;
    }

    public void Dispose()
    {
        Globals.ThisDocument.Application.ScreenUpdating = true;
    }        
}

Pay attention that this code is for a VSTO Word document. Other office applications might require small adjustments for it to work.
Now when you want to suspend and resume the document layout, all you have to do is:

using (ScreenSuspender.SuspendLayout())
{ 
    // do some layout changes
}

This will make sure your Office application doesn't freeze, even if the code inside the using block fails to execute.

All the best,
Shay.

Posted by shayf | with no comments
תגים:,

Workaround: WebBrowser Control - Links Do Not Work

Yesterday I had to fix a bug that had been discovered at our form that used the WebBrowser control. Here is the description of the problem and solution. I hope it will save you the time I've spent on this.

The problem

When you use the WebBrowser.DocumentText in order to set the HTML of the document, some links don't work when you click on them.
For example, the following link to the user's desktop doesn't work: <a href="file:///C:/Documents%20and%20Settings/user/Desktop/jon.png">File</a>. This happens because of permissions problem I guess - a web browser tries to execute a file on the user's desktop...

The solution

After trying various solutions, only this one proved to really work.
Before you set the DocumentText property, register to the DocumentCompleted event:

webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.DocumentText = textBox1.Text;

Now, on the event handler we will register to the links Click event (you can't do that right after you set the DocumentText property because the Document property might not be complete by then):

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    foreach (HtmlElement link in webBrowser1.Document.Links)
    {
        link.Click += new HtmlElementEventHandler(link_Click);
    }
}

What we have left is to take care of link clicks. What I do here is quite simple - I retrieve the href attribute and execute it using the Process.Start method. This will jump above the security barriers and open the file with the default program associated with it. Right after that I prevent the event to continue bubble through the layers:

void link_Click(object sender, HtmlElementEventArgs e)
{
    // Get the file path
    string file = (sender as HtmlElement).GetAttribute("href");
    
    // Execute it
    Process.Start(file);

    // Prevent the event bubbling
    e.BubbleEvent = false;
    e.ReturnValue = false;
}        

That's it!
Pay attention that if you have some other links in the HTML document, you should filter them somewhere and let the WebBrowser control to take care of them.

Hope it helps,
Shay.