RSS

Tag Archives: .net

C# First or Default IP Address


IPHostEntry localHostEntry = Dns.GetHostEntry("");

_LocalIpAddress = localHostEntry.AddressList.FirstOrDefault(current => current.AddressFamily == AddressFamily.InterNetwork).ToString();

 
Leave a comment

Posted by on August 22, 2011 in .net, c#, Uncategorized

 

Tags: , , , , , ,

C# Cross Thread Exception on Subform

I was having an issue where I had a textbox control on a sub form and calling subform.show() after an update would cause a cross thread exception (even using invoke). The problem was I created a subform (with the textbox control) and added text to the textbox before the sub form was displayed. The problem is that the handle for the control was not created yet and invokerequired was false because of this. Thus the thread that updated my control actually caused the control’s handle to be created. When I called the subform.show(), it would try to access the control on the GUI thread and not the thread that had created it. See below:

Example:

Subform _Sub
 
Public MainForm()
{
    _Sub = new Subform();
    // Sub form’s handle is not created yet.
}
 
 
// If this method is called  on a different thread before the sub form (show) is
// called on the GUI thread, a cross thread exception can occur.
// -This method will append to a text box on the sub form.
private void appendToSubForm(string text)
{
        //  Invoke will not be required since the handle has not yet been created.
        if (_Sub.InvokeRequired)
        {
            _ Sub.Invoke((Action)appendToDebugLog, text);
        }
        else
        {
            if (_Sub == null || _ Sub.IsDisposed)
            {
                return;
            }
            // This method which will append to textbox will cause the sub form’s handle to be created
            // on the thread this method is called on.
            _Sub.AppendToMe(text);
        }
}
 
// SOLUTION:
// Call this.CreateHandle() on the Sub form in order to force creation of the handle when the constructor is called.
 
// Required to create the handle on the thread that the constructor is called on.
// In our case, the GUI thread.  This prevents cross threading if the handle
// is created on a non GUI thread.
this.CreateHandle();

 
1 Comment

Posted by on August 22, 2011 in .net, c#

 

Tags: , , , , , , ,

C# Scroll To End Text Box

After appending text to a text box, you may want to scroll to the end of that box. Here’s some code that will accomplish this (remember to include, System.Runtime.InteropServices in your using header):

///
/// Function import to automatically scroll down rich text boxes.
///
/// handle to destination window
/// message
/// first message parameter
/// second message parameter
///
[DllImport("user32.dll")]
public static extern int SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
IntPtr wParam, // first message parameter
IntPtr lParam // second message parameter
);

///
/// Automatically scroll down rich text boxes.
///
/// The windows handle of a text box.
public void ScrollToEnd(IntPtr Handle)
{
int WM_VSCROLL = 0x115;
int SB_BOTTOM = 7;
SendMessage(Handle, WM_VSCROLL, (IntPtr)SB_BOTTOM, IntPtr.Zero);
}

// Using the method.
ScrollToEnd(richTextBox1.Handle)

 
Leave a comment

Posted by on August 10, 2011 in .net, c#

 

Tags: , , , , ,

C# Windows Vista/7 Program Might Not Have Installed Correctly

If you include the word “Installer” in your Visual Studio Project, you may run into issue when executing your program on a Windows Vista or 7 machine.

Visual Studio will tag your application as an installation application.  If you do not have the correct settings in your application manifest file, it will display something along the lines of:

This program might not have installed correctly

There may also be an option to reinstall using recommended settings or to you can select the installation did work as expected.

Remedy:

  1. Add an application manifest to your file by right clicking in the Project and selecting Add New Item.
  2. Open the app manifest and include the following:

    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">

    <application>
    <!--The ID below indicates application support for Windows Vista -->

    <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>

    <!--The ID below indicates application support for Windows 7 -->

    <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>

    </application>

    </compatibility>

  3. Save your app manifest file and ensure it is the selected one in your Project’s Properties Application tab.
 
Leave a comment

Posted by on August 9, 2011 in .net, c#

 

Tags: , , , , , ,