将指定的 32 位有符号整数的值转换为等效的 32 位无符号整数。
此方法不符合 CLS。
Overloads Public Shared Function ToUInt32(Integer) As UInt32
但是在执行代码时总是报OverflowException 异常。
我的应用场合是这样的,在SQL SERVER中定义一个Integer(32)的字段,保存一个颜色值,然后在程序中读入该值,但总是报OverflowException 异常,应该如何处理?
Dim someColor As Color
myrow("颜色") = someColor.ToArgb()
Color = Color.FromArgb(myrow("颜色"))
5 个解决方案
#1
myrow("颜色") = System.Drawing.ColorTranslator.ToOle(someColor)
Color = System.Drawing.ColorTranslator.FromOle(myrow("颜色"))
Color = System.Drawing.ColorTranslator.FromOle(myrow("颜色"))
#2
someColor= System.Drawing.ColorTranslator.FromOle(myrow("颜色"))
#3
Public Sub ToArgbToStringExample(ByVal e As PaintEventArgs)
Dim g As Graphics = e.Graphics ' Color structure used for temporary storage.
Dim someColor As Color = Color.FromArgb(0) ' Array to store KnownColor values that match the criteria.
Dim colorMatches(167) As KnownColor
Dim count As Integer = 0 ' number of matches found
' Iterate through KnownColor enums to find all corresponding colors
' that have a non-zero green component and zero-valued red
' component and that are not system colors.
Dim enumValue As KnownColor
For enumValue = 0 To KnownColor.YellowGreen
someColor = Color.FromKnownColor(enumValue)
If someColor.G <> 0 And someColor.R = 0 And _
Not someColor.IsSystemColor Then
colorMatches(count) = enumValue
count += 1
End If
Next enumValue
Dim myBrush1 As New SolidBrush(someColor)
Dim myFont As New Font("Arial", 9)
Dim x As Integer = 40
Dim y As Integer = 40 ' Iterate through the matches found and display each color that
' corresponds with the enum value in the array. Also display the
' name of the KnownColor and the ARGB components.
Dim i As Integer
For i = 0 To count - 1
' Display the color.
someColor = Color.FromKnownColor(colorMatches(i))
myBrush1.Color = someColor
g.FillRectangle(myBrush1, x, y, 50, 30)
' Display KnownColor name and four component values. To display
' component values: Use the ToArgb method to get the 32-bit
' ARGB value of someColor (created from a KnownColor). Create
' a Color structure from the 32-bit ARGB value and set someColor
' equal to this new Color structure. Then use the ToString method
' to convert it to a string.
g.DrawString(someColor.ToString(), myFont, Brushes.Black, _
x + 55, y)
someColor = Color.FromArgb(someColor.ToArgb())
g.DrawString(someColor.ToString(), myFont, Brushes.Black, _
x + 55, y + 15)
y += 40
Next i
End Sub
Dim g As Graphics = e.Graphics ' Color structure used for temporary storage.
Dim someColor As Color = Color.FromArgb(0) ' Array to store KnownColor values that match the criteria.
Dim colorMatches(167) As KnownColor
Dim count As Integer = 0 ' number of matches found
' Iterate through KnownColor enums to find all corresponding colors
' that have a non-zero green component and zero-valued red
' component and that are not system colors.
Dim enumValue As KnownColor
For enumValue = 0 To KnownColor.YellowGreen
someColor = Color.FromKnownColor(enumValue)
If someColor.G <> 0 And someColor.R = 0 And _
Not someColor.IsSystemColor Then
colorMatches(count) = enumValue
count += 1
End If
Next enumValue
Dim myBrush1 As New SolidBrush(someColor)
Dim myFont As New Font("Arial", 9)
Dim x As Integer = 40
Dim y As Integer = 40 ' Iterate through the matches found and display each color that
' corresponds with the enum value in the array. Also display the
' name of the KnownColor and the ARGB components.
Dim i As Integer
For i = 0 To count - 1
' Display the color.
someColor = Color.FromKnownColor(colorMatches(i))
myBrush1.Color = someColor
g.FillRectangle(myBrush1, x, y, 50, 30)
' Display KnownColor name and four component values. To display
' component values: Use the ToArgb method to get the 32-bit
' ARGB value of someColor (created from a KnownColor). Create
' a Color structure from the 32-bit ARGB value and set someColor
' equal to this new Color structure. Then use the ToString method
' to convert it to a string.
g.DrawString(someColor.ToString(), myFont, Brushes.Black, _
x + 55, y)
someColor = Color.FromArgb(someColor.ToArgb())
g.DrawString(someColor.ToString(), myFont, Brushes.Black, _
x + 55, y + 15)
y += 40
Next i
End Sub
#4
看来问题解决啦?
#5
谢谢,wang_cel(哦哦),先在这给你记分了!
但是,看来我的问题没有提清楚
我在程序中使用了一个ACTIVEX控件,其中有个属性是OLE_COLOR类型的,在NET下显示是需要一个UInt32类型的值,而NET下Color和ColorTranslator转换出来的都是Integer类型的值,所以我在代码中就无法对OLE_COLOR类型的属性赋值,当一个为负值的Integer转化为UInt32类型时,始终报OverflowException 异常。
解答的人一定给加分!
但是,看来我的问题没有提清楚
我在程序中使用了一个ACTIVEX控件,其中有个属性是OLE_COLOR类型的,在NET下显示是需要一个UInt32类型的值,而NET下Color和ColorTranslator转换出来的都是Integer类型的值,所以我在代码中就无法对OLE_COLOR类型的属性赋值,当一个为负值的Integer转化为UInt32类型时,始终报OverflowException 异常。
解答的人一定给加分!
#1
myrow("颜色") = System.Drawing.ColorTranslator.ToOle(someColor)
Color = System.Drawing.ColorTranslator.FromOle(myrow("颜色"))
Color = System.Drawing.ColorTranslator.FromOle(myrow("颜色"))
#2
someColor= System.Drawing.ColorTranslator.FromOle(myrow("颜色"))
#3
Public Sub ToArgbToStringExample(ByVal e As PaintEventArgs)
Dim g As Graphics = e.Graphics ' Color structure used for temporary storage.
Dim someColor As Color = Color.FromArgb(0) ' Array to store KnownColor values that match the criteria.
Dim colorMatches(167) As KnownColor
Dim count As Integer = 0 ' number of matches found
' Iterate through KnownColor enums to find all corresponding colors
' that have a non-zero green component and zero-valued red
' component and that are not system colors.
Dim enumValue As KnownColor
For enumValue = 0 To KnownColor.YellowGreen
someColor = Color.FromKnownColor(enumValue)
If someColor.G <> 0 And someColor.R = 0 And _
Not someColor.IsSystemColor Then
colorMatches(count) = enumValue
count += 1
End If
Next enumValue
Dim myBrush1 As New SolidBrush(someColor)
Dim myFont As New Font("Arial", 9)
Dim x As Integer = 40
Dim y As Integer = 40 ' Iterate through the matches found and display each color that
' corresponds with the enum value in the array. Also display the
' name of the KnownColor and the ARGB components.
Dim i As Integer
For i = 0 To count - 1
' Display the color.
someColor = Color.FromKnownColor(colorMatches(i))
myBrush1.Color = someColor
g.FillRectangle(myBrush1, x, y, 50, 30)
' Display KnownColor name and four component values. To display
' component values: Use the ToArgb method to get the 32-bit
' ARGB value of someColor (created from a KnownColor). Create
' a Color structure from the 32-bit ARGB value and set someColor
' equal to this new Color structure. Then use the ToString method
' to convert it to a string.
g.DrawString(someColor.ToString(), myFont, Brushes.Black, _
x + 55, y)
someColor = Color.FromArgb(someColor.ToArgb())
g.DrawString(someColor.ToString(), myFont, Brushes.Black, _
x + 55, y + 15)
y += 40
Next i
End Sub
Dim g As Graphics = e.Graphics ' Color structure used for temporary storage.
Dim someColor As Color = Color.FromArgb(0) ' Array to store KnownColor values that match the criteria.
Dim colorMatches(167) As KnownColor
Dim count As Integer = 0 ' number of matches found
' Iterate through KnownColor enums to find all corresponding colors
' that have a non-zero green component and zero-valued red
' component and that are not system colors.
Dim enumValue As KnownColor
For enumValue = 0 To KnownColor.YellowGreen
someColor = Color.FromKnownColor(enumValue)
If someColor.G <> 0 And someColor.R = 0 And _
Not someColor.IsSystemColor Then
colorMatches(count) = enumValue
count += 1
End If
Next enumValue
Dim myBrush1 As New SolidBrush(someColor)
Dim myFont As New Font("Arial", 9)
Dim x As Integer = 40
Dim y As Integer = 40 ' Iterate through the matches found and display each color that
' corresponds with the enum value in the array. Also display the
' name of the KnownColor and the ARGB components.
Dim i As Integer
For i = 0 To count - 1
' Display the color.
someColor = Color.FromKnownColor(colorMatches(i))
myBrush1.Color = someColor
g.FillRectangle(myBrush1, x, y, 50, 30)
' Display KnownColor name and four component values. To display
' component values: Use the ToArgb method to get the 32-bit
' ARGB value of someColor (created from a KnownColor). Create
' a Color structure from the 32-bit ARGB value and set someColor
' equal to this new Color structure. Then use the ToString method
' to convert it to a string.
g.DrawString(someColor.ToString(), myFont, Brushes.Black, _
x + 55, y)
someColor = Color.FromArgb(someColor.ToArgb())
g.DrawString(someColor.ToString(), myFont, Brushes.Black, _
x + 55, y + 15)
y += 40
Next i
End Sub
#4
看来问题解决啦?
#5
谢谢,wang_cel(哦哦),先在这给你记分了!
但是,看来我的问题没有提清楚
我在程序中使用了一个ACTIVEX控件,其中有个属性是OLE_COLOR类型的,在NET下显示是需要一个UInt32类型的值,而NET下Color和ColorTranslator转换出来的都是Integer类型的值,所以我在代码中就无法对OLE_COLOR类型的属性赋值,当一个为负值的Integer转化为UInt32类型时,始终报OverflowException 异常。
解答的人一定给加分!
但是,看来我的问题没有提清楚
我在程序中使用了一个ACTIVEX控件,其中有个属性是OLE_COLOR类型的,在NET下显示是需要一个UInt32类型的值,而NET下Color和ColorTranslator转换出来的都是Integer类型的值,所以我在代码中就无法对OLE_COLOR类型的属性赋值,当一个为负值的Integer转化为UInt32类型时,始终报OverflowException 异常。
解答的人一定给加分!