ProgressBarColor
is an Enum
, which cannot be implicitly evaluated as an int
. You will need to cast the value as an int
.
Try doing the following:
SendMessage(ProgressBar_Name.Handle, 0x410, (int)ProgressBar_Color, 0);
Question
I needed to convert the following code from VB.NET to C#:
Public Class Form1
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Enum ProgressBarColor
Green = &H1
Red = &H2
Yellow = &H3
End Enum
Private Shared Sub ChangeProgBarColor(ByVal ProgressBar_Name As Windows.Forms.ProgressBar, ByVal ProgressBar_Color As ProgressBarColor)
SendMessage(ProgressBar_Name.Handle, &H410, ProgressBar_Color, 0)
End Sub
End Class
VB.NET code source: Ultimate Programming Tutorials
I used developerFusion's online code converter to convert the code, and this is the C# code that it produced:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class Form1
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
public enum ProgressBarColor
{
Green = 0x1,
Red = 0x2,
Yellow = 0x3
}
private static void ChangeProgBarColor(System.Windows.Forms.ProgressBar ProgressBar_Name, ProgressBarColor ProgressBar_Color)
{
SendMessage(ProgressBar_Name.Handle, 0x410, ProgressBar_Color, 0);
}
}
EDIT: Removed "ExactSpelling = true" from the DllImport, as it causes an error.
However, that auto-converted code fails to compile due to the following line:
SendMessage(ProgressBar_Name.Handle, 0x410, ProgressBar_Color, 0);
The error that is displayed by the C# compiler is:
Error: The best overloaded method for 'Jyrka98_Modpack_CS.Form1.SendMessage(System.IntPtr, int, int, int)' has some invalid arguments.
It also shows this error:
Argument 3: cannot convert from 'Jyrka98_Modpack_CS.Form1.ProgressBarColor' to 'int'
I don't understand what it means by those error messages. How can I fix that line in C#?
Solution
ProgressBarColor
is an Enum
, which cannot be implicitly evaluated as an int
. You will need to cast the value as an int
.
Try doing the following:
SendMessage(ProgressBar_Name.Handle, 0x410, (int)ProgressBar_Color, 0);
OTHER TIPS
Since you don't show how ProgressBarColor
is declared, it's a bit of a guess, but since VB is able to automatically convert it to an Integer
, I'd assume that it's an enumeration. The VB.NET compiler automatically converts enumeration values to Integer
, but the C# compiler does not. In C#, you'll need to explicitly cast the value to an int
, like this:
SendMessage(ProgressBar_Name.Handle, 0x410, (int)ProgressBar_Color, 0);
For what it's worth, the proper declaration for the SendMessage
API call, according to PINVOKE.NET, is:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
I would recommend using those argument types rather than int
, like the code converter used.