Category Archives: Beruf

Alles was meinen Beruf und dessen Inhalte betrifft.

Bubble Mouse Move events from child controls up the hierarchy in Windows Forms

Windows Forms (or “WinForms” for short) does not know the concept of event bubbling (also called “event propagation” sometimes). To solve this in terms of command routing, I’ve written some small classes earlier.

To bubble up events from child controls to parent controls (or the form itself), the idea is to hook into the child control creation and hook up for those specific events and manually forward them.

Based on this idea and with the help of a forum answer on MSDN, I’ve written a small class that you can attach to a control and get all child control MouseMove events. The class looks like:

public sealed class MouseEventBubbler
{
    private readonly Control _attachTo;

    public MouseEventBubbler(Control attachTo)
    {
        _attachTo = attachTo;

        _attachTo.MouseMove += _attachTo_MouseMove;

        _attachTo.ControlAdded += _attachTo_ControlAdded;
        _attachTo.ControlRemoved += _attachTo_ControlRemoved;

        foreach (Control control in _attachTo.Controls)
        {
            AttachToControl(control);
        }
    }

    public void _attachTo_MouseMove(object sender, MouseEventArgs e)
    {
        OnMouseMove(e);
    }

    public event MouseEventHandler MouseMove;

    private void _attachTo_ControlAdded(object sender, ControlEventArgs e)
    {
        AttachToControl(e.Control);
    }

    private void _attachTo_ControlRemoved(object sender, ControlEventArgs e)
    {
        DetachFromControl(e.Control);
    }

    private void AttachToControl(Control c)
    {
        c.MouseMove += Child_MouseMove;
        c.ControlAdded += Child_ControlAdded;
        c.ControlRemoved += Child_ControlRemoved;
        AttachToChildren(c);
    }

    private void AttachToChildren(Control parent)
    {
        foreach (Control child in parent.Controls)
        {
            AttachToControl(child);
        }
    }

    private void DetachFromControl(Control c)
    {
        DetachFromChildren(c);
        c.MouseMove -= Child_MouseMove;
        c.ControlAdded -= Child_ControlAdded;
        c.ControlRemoved -= Child_ControlRemoved;
    }

    private void DetachFromChildren(Control parent)
    {
        foreach (Control child in parent.Controls)
        {
            DetachFromControl(child);
        }
    }

    private void Child_ControlAdded(object sender, ControlEventArgs e)
    {
        AttachToControl(e.Control);
    }

    private void Child_ControlRemoved(object sender, ControlEventArgs e)
    {
        DetachFromControl(e.Control);
    }

    private void Child_MouseMove(object sender, MouseEventArgs e)
    {
        var pt = e.Location;
        var child = (Control)sender;
        do
        {
            pt.Offset(child.Left, child.Top);
            child = child.Parent;
        }
        while (child != _attachTo);

        var newArgs = new MouseEventArgs(e.Button, e.Clicks, pt.X, pt.Y, e.Delta);
        OnMouseMove(newArgs);
    }

    private void OnMouseMove(MouseEventArgs newArgs)
    {
        var h = MouseMove;
        if (h != null)
        {
            h(this, newArgs);
        }
    }
}

I’ve also saved it as a PasteBin.

The class can be adjusted to match other events than the MouseMove event, if required.

Dateigrößenangaben in C# menschenlesbar formatieren

Um aus Byte-Angaben (z.B. von Dateigrößen) menschenlesbare Werte (z.B. “12.5GB”) zu machen, sowie den anderen Weg, habe ich mir ein bisschen Code geschrieben bzw. von Stack Overflow zusammen gesucht:

public static class SizeTranslationHelper
{
    /// <summary>
    /// Converts bytes to human-readable, e.g. "12.5GB".
    /// </summary>
    public static string MakeLazy(ulong byteCount)
    {
        string[] suf = { @"B", @"KB", @"MB", @"GB", @"TB", @"PB", @"EB" }; //Longs run out around EB
        if (byteCount == 0)
            return "0" + suf[0];
        var bytes = (ulong)Math.Abs((decimal)byteCount);
        var place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024ul)));
        var num = Math.Round(bytes / Math.Pow(1024ul, place), 1);
        return (Math.Sign((decimal)byteCount) * num).ToString(CultureInfo.InvariantCulture) + suf[place];
    }

    /// <summary>
    /// Converts human-readable, e.g. "12.5GB", to bytes.
    /// </summary>
    public static ulong TranslateLazySize(string sizeLazy)
    {
        sizeLazy = sizeLazy.ToLowerInvariant();

        ulong result = 0;
        if (tryParse(ref result, sizeLazy, @"b", 1ul))
        {
            return result;
        }
        else if (tryParse(ref result, sizeLazy, @"kb", 1024ul))
        {
            return result;
        }
        else if (tryParse(ref result, sizeLazy, @"mb", 1024ul * 1024ul))
        {
            return result;
        }
        else if (tryParse(ref result, sizeLazy, @"gb", 1024ul * 1024ul * 1024ul))
        {
            return result;
        }
        else if (tryParse(ref result, sizeLazy, @"tb", 1024ul * 1024ul * 1024ul * 1024ul))
        {
            return result;
        }
        else if (tryParse(ref result, sizeLazy, @"pb", 1024ul * 1024ul * 1024ul * 1024ul * 1024ul))
        {
            return result;
        }
        else if (tryParse(ref result, sizeLazy, @"eb", 1024ul * 1024ul * 1024ul * 1024ul * 1024ul * 1024ul))
        {
            return result;
        }
        else
        {
            decimal r;
            if (decimal.TryParse(sizeLazy, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out r))
            {
                return (ulong)r;
            }
            else
            {
                throw new Exception(string.Format(@"Cannot parse '{0}' to number.", sizeLazy));
            }
        }
    }

    private static bool tryParse(ref ulong result, string sizeLazy, string suffix, ulong factor)
    {
        decimal r;
        if (
            sizeLazy.EndsWith(suffix) &&
            decimal.TryParse(sizeLazy.Substring(0, sizeLazy.Length - suffix.Length), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out r))
        {
            result = (ulong)(r * factor);
            return true;
        }
        else
        {
            return false;
        }
    }
}

Hier noch der Pastebin-Eintrag dazu.

Fehlermeldung “System.MethodAccessException” bei .NET-4-Programmen beheben

Habe vorhin ein .NET-2-Programm (Windows Forms) nach .NET 4.5 geändert (also in der Projektkonfiguration in Visual Studio .NET 2012 das Ziel-Framework geändert.

Bei diesem Aufruf hat es dann gekracht:

AppDomain.CurrentDomain.UnhandledException += currentDomain_UnhandledException;

Dort kam dann eine Fehlermeldung:

System.MethodAccessException was unhandled HResult=-2146233072 Message=Fehler beim Versuch der SecurityTransparent-Methode “ZetaHelpdesk.Main.Code.AppHost.Host.Main()”, auf die sicherheitskritische Methode “System.AppDomain.add_UnhandledException(System.UnhandledExceptionEventHandler)” zuzugreifen.

Die Assembly “zeta-helpdesk, Version=2.1.0.1, Culture=neutral, PublicKeyToken=1dbe5f735b90e083″ ist mit “AllowPartiallyTrustedCallersAttribute” markiert und verwendet das Sicherheitstransparenzmodell der Stufe 2. Bei Festlegung der Transparenz auf Stufe 2 werden alle Methoden in AllowPartiallyTrustedCallers-Assemblys standardmäßig sicherheitstransparent, was die Ursache der Ausnahme sein kann.

Source=zeta-helpdesk

StackTrace: bei ZetaHelpdesk.Main.Code.AppHost.Host.Main() in c:\P\Zeta Helpdesk\Source\Core\Main\Code\AppHost\Host.cs:Zeile 100. bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() bei System.Threading.ThreadHelper.ThreadStart_Context(Object state) bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) bei System.Threading.ThreadHelper.ThreadStart() InnerException:

Wer die Fehlermeldung aufmerksam liest, findet auch schon die Ursache. Da steht nämlich, dass ein Assembly-Attribut hier unpassend ist. Und tatsächlich habe ich folgendes in der Datei “AssemblyInfo.cs” stehen:

[assembly: AllowPartiallyTrustedCallers]

Ich hatte seinerzeit aus diesem Grund das Attribut eingefügt.

Als ich das Attribut dann entfernt habe, lief alles wie gewünscht. Neu kompilieren natürlich auch noch ;-)

Unter Windows 8, Bilder mit der Desktop-Bildanzeige öffnen

Unter Windows 8 startet beim Anklicken von Bildern im Explorer im Desktop-Modus leider die Vollbild-Kachel-Anwendung “Foto”.

Das stört mich sehr. Zum Glück gibt es einen Weg, wieder die unter Windows 7 bekannte Bildanzeige mit all ihren Vorteilen anzuzeigen:

  1. From the desktop, load the Control Panel. Right-click the screen’s bottom-left corner and choose Control Panel from the pop-up menu.
  2. When the Control Panel opens, click the Programs category. Click Default Programs and then click Set Your Default Programs. The Set Default Programs window appears.
  3. From the left pane, click Windows Photo Viewer. Then select Set This Program As Default and click OK. That tells Windows Photo Viewer to open all of your photos, bypassing the Start screen’s Photos app.

Danach geht’s wieder sauber.

Kostenlos Faxe als E-Mail-Nachrichten empfangen

Gerade ausprobiert, hat funktioniert:

Unter call-manager.de könnt Ihr Euch kostenlos eine Telefonnummer besorgen (meine ist die 0911-30844-09073), eine E-Mail-Adresse hinterlegen und dann Faxe als E-Mail erhalten.

Laut Webutation ist die Call-Manager-Website seriös.

Um das alles kostenlos zu testen, habe ich mir von dieser Website kostenlos ein Testfax gesendet. Ein weiterer Testdienst (Webform to Fax) ist dieser hier.

Zwei Festplatten-Partitionen unter Windows Server 2008 mit Bordmitteln zu einer verbinden

Es gibt diverse käuflich zu erwerbende Tools, mit denen Ihr viele schöne Schnickschnack-Sachen mit Festplatten-Partitionen machen könnt.

Wenn Ihr ganz einfach zwei Partitionen zu einer zusammen fassen wollt, dann könnt Ihr das auch mit Bordmitteln von Windows Server 2008 oder neuer machen.

Achtung, die Daten auf der zweiten Partition gehen dabei verloren, also vorher Sicherheitskopie erstellen!

Vorgehen

Um die zweite Partition zu löschen, könnt Ihr so wie hier beschrieben vorgehen:

  1. Disk-Management-MMC-Snap-in starten.
  2. Zweite Partition rechtsklicken und “Delete Volume” auswählen.
  3. Zweite Partition nochmals rechtsklicken und “Delete Partition” auswählen.
  4. Erste/primäre Partition rechtsklicken und “Expand Partition” auswählen.

Das war’s. So einfach geht das.

Fehlermeldung “CS0103: Der Name ‘ViewBag’ ist im aktuellen Kontext nicht vorhanden” bei ASP.NET MVC 4 lösen

Heute habe ich bei einem neu deployten Projekt mit ASP.NET 4.5 und MVC 4 folgende Fehlermeldung erhalten:

CS0103: Der Name ‘ViewBag’ ist im aktuellen Kontext nicht vorhanden CS0103: The name ‘ViewBag’ does not exist in the current context

Lokal lief alles, auf dem Produktivserver kam die Fehlermeldung. Ich habe lange gesucht, bis ich die funktionierende Lösung gefunden habe.

Lösung

Die Lösung war im Endeffekt, dass es zwei Web.Config-Dateien gibt:

Diese im Views-Ordner hat bei mir gefehlt. Sobald ich diese hinzugefügt und entsprechend gefüllt hatte, hat alles funktioniert.

Alle Sichten in einer Microsoft-SQL-Server-Datenbank aktualisieren

Um alle Views in einem Rutsch zu aktualisieren, hilft folgender Code:

DECLARE @view AS VARCHAR(255);

DECLARE views_cursor CURSOR FOR 
    SELECT TABLE_SCHEMA + '.' +TABLE_NAME FROM INFORMATION_SCHEMA.TABLES 
    WHERE TABLE_TYPE = 'VIEW' 
    AND OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), 'IsMsShipped') = 0 
    ORDER BY TABLE_SCHEMA,TABLE_NAME 

OPEN views_cursor 

FETCH NEXT FROM views_cursor 
INTO @view 

WHILE (@@FETCH_STATUS <> -1) 
BEGIN
    BEGIN TRY
        EXEC sp_refreshview @view;
        PRINT @view;
    END TRY
    BEGIN CATCH
        PRINT 'Error during refreshing view "' + @view + '".';
    END CATCH;

    FETCH NEXT FROM views_cursor 
    INTO @view 
END 

CLOSE views_cursor; 
DEALLOCATE views_cursor;

Funktioniert wunderbar.

In der Praxis z.B. hilfreich, wenn in den zugrunde liegenden Tabellen Spalten hinzugekommen sind oder entfernt wurden.