|
You have to post such of content to an author of article (under the article). Use "Have a question or comment" widget.
|
|
|
|
|
This is supposed to be a reply to a solution in QA[^]. You have already posted it as a comment there. DO NOT post it in the forums. It makes absolutely no sense here.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
This keep generating an error...
Dim number As Integer
number = Convert.ToInt32(Console.ReadLine())
|
|
|
|
|
That's because whatever you're typing on the command line isn't convertible to an integer. An empty string is NOT convertible.
Forget using Convert and use Integer.TryParse instead.
|
|
|
|
|
Console.ReadLine Method (System) | Microsoft Docs returns string. So, you have to define string variable, read line from console and then convert string into integer. Please, use Int32.TryParse Method (System) | Microsoft Docs:
Dim ci As System.Globalization.CultureInfo = New System.Globalization.CultureInfo("PL-pl")
Dim format As System.Globalization.NumberStyles = System.Globalization.NumberStyles.AllowLeadingSign
Dim line As String = Console.ReadLine()
Dim number As Integer = 0
If Int32.TryParse(line, format, ci, number) Then
Console.WriteLine($"You have entered {number}")
Else
Console.WriteLine($"Can't convert '{line}' into number!")
End If
Good luck!
|
|
|
|
|
Hello.
I have static library and I need to add it to VB project.
How can I do it ?
I am working in VisualStudio 2013
Thanks in advance.
Andrii.
|
|
|
|
|
That depends on what you mean by "static library".
Is this supplied in a .DLL? Where did this library come from? Is this COM-based? A function library? Got any documentation on it?
|
|
|
|
|
Hello everyone ,
I am trying to retrieve XML information, I managed to recover the attributes against the childrens (<pre><ToolLine Text=" )they are not recovering below my code and the form of my XML file:
XML template :
<pre lang="xml">
<?xml version="1.0" encoding="utf-8"?>
<Root>
<ToolsList>
<Tool PartNumber="0018B">
<ToolLine Text=" FI : 3897" />
<ToolLine Text=" PINCE1 Reference : M 22520/2.01" />
<ToolLine Text=" Position : 2.02" />
<ToolLine Text=" Reglage : 7" />
<ToolLine Text=" PINCE2 Reference: M 22520/7.01" />
</Tool>
</ToolsList>
</Root>
Code C#:
<pre lang="vb">Imports System.IO
Imports System.Xml
Module ParsingUsingXmlDocument
Sub Main()
Try
Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
'Create the XML Document
m_xmld = New XmlDocument()
'Load the Xml file
m_xmld.Load("D:\Users\Desktop\Xml to excel chahine\U881A1013_105_A_PREP_1.xml")
m_nodelist = m_xmld.SelectNodes("/Root/ToolsList/PartNumber")
'Loop through the nodes
For Each m_node In m_nodelist
Dim partnumber = m_node.Attributes.GetNamedItem("PartNumber").Value
'Get the firstName Element Value
Dim text1 = m_node.ChildNodes.Item(0).InnerText
Dim text2 = m_node.ChildNodes.Item(1).InnerText
Dim text3 = m_node.ChildNodes.Item(2).InnerText
Dim text4 = m_node.ChildNodes.Item(3).InnerText
Console.Write("partnumber: " & partnumber _
& " text1: " & text1 & " text3: " _
& text3)
Console.Write(vbCrLf)
Next
Catch errorVariable As Exception
'Error trapping
Console.Write(errorVariable.ToString())
End Try
End Sub
End Module
if you have any ideas do not hesitate to help me, thanks in advance
after a question how can I implement this information on an excel file
|
|
|
|
|
Member 14506089 wrote:
m_xmld.SelectNodes("/Root/ToolsList/PartNumber") Based on the XML provided, the node under ToolsList is called Tool , not PartNumber .
Member 14506089 wrote:
m_node.ChildNodes.Item(0).InnerText InnerText returns the text between the opening and closing tags. You are trying to read the value of an attribute. You cannot read an attribute value using the InnerText property.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Sorry for so much code but I think you need to see all of it.
The below code works for a 32 bpp image but using a 24 bpp image. The resulting
24 bpp image is skewed; it looks like by 1 pixel. The skewing starts at the top an skews
to the right down the image. I've tried messing with indices and widths but can not
get a clue as to what's happening.
Am I missing something really simple?
Public Sub CropImage()
'Source image in a PictureBox
Dim _arRGBValues() As Byte
Dim _BMPData As Imaging.BitmapData
Dim _BMPPtr As IntPtr
Dim bmSrc As Bitmap = Me.Image.Clone()
Dim rect As New Rectangle(0, 0, bmSrc.Width, bmSrc.Height)
_BMPData = bmSrc.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmSrc.PixelFormat)
_BMPPtr = _BMPData.Scan0
If bmSrc.PixelFormat = Imaging.PixelFormat.Format24bppRgb Then
' 24bpp
Dim bytes As Integer = (bmSrc.Width * bmSrc.Height) * 3
ReDim _arRGBValues(bytes - 1)
System.Runtime.InteropServices.Marshal.Copy(_BMPPtr, _arRGBValues, 0, _arRGBValues.Length)
Else
' 32bpp
Dim bytes As Integer = (bmSrc.Width * bmSrc.Height) * 4
ReDim _arRGBValues(bytes - 1)
System.Runtime.InteropServices.Marshal.Copy(_BMPPtr, _arRGBValues, 0, _arRGBValues.Length)
End If
' Destination image
Dim arCropValues() As Byte
Dim cropData As Imaging.BitmapData
Dim cropPtr As IntPtr
' Size to src bm above - 1 pixel in width to allow for 1 pixel offset; height is the same.
Dim bmCrop As Bitmap = New Bitmap(bmSrc.Width - 1, bmSrc.Height, bmSrc.PixelFormat)
Dim cropRect As New Rectangle(0, 0, bmCrop.Width, bmCrop.Height)
cropData = bmCrop.LockBits(cropRect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmCrop.PixelFormat)
cropPtr = cropData.Scan0
If bmCrop.PixelFormat = Imaging.PixelFormat.Format24bppRgb Then
' 24bpp
Dim bytes As Integer = (bmCrop.Width * bmCrop.Height) * 3
ReDim arCropValues(bytes - 1)
System.Runtime.InteropServices.Marshal.Copy(cropPtr, arCropValues, 0, arCropValues.Length)
Else
' 32bpp
Dim bytes As Integer = (bmCrop.Width * bmCrop.Height) * 4
ReDim arCropValues(bytes - 1)
System.Runtime.InteropServices.Marshal.Copy(cropPtr, arCropValues, 0, arCropValues.Length)
End If
' Copy from source image
Dim colour As Color
' Start at column 1; 1 pixel column offset
Dim xOffset As Integer = 1
Dim yDst As Integer = 0
For ySrc As Integer = 0 To Me.Image.Height - 1
' Copy starting at bmCrop column 0
Dim xDst As Integer = 0
' Starting at bmSrc column xOffset
For xSrc As Integer = xOffset To Me.Image.Width - 1
' Get pixel
If bmSrc.PixelFormat = Imaging.PixelFormat.Format24bppRgb Then
' 24bpp
Dim index As Integer = ((ySrc * bmSrc.Width) + xSrc) * 3
Dim b As Integer = _arRGBValues(index)
Dim g As Integer = _arRGBValues(index + 1)
Dim r As Integer = _arRGBValues(index + 2)
colour = Color.FromArgb(r, g, b)
Else
' 32bpp
Dim index As Integer = ((ySrc * bmSrc.Width) + xSrc) * 4
Dim b As Integer = _arRGBValues(index)
Dim g As Integer = _arRGBValues(index + 1)
Dim r As Integer = _arRGBValues(index + 2)
Dim a As Integer = _arRGBValues(index + 3)
colour = Color.FromArgb(a, r, g, b)
End If
' Set Pixel
If bmSrc.PixelFormat = Imaging.PixelFormat.Format24bppRgb Then
' 24bpp
Dim index As Integer = ((yDst * bmCrop.Width) + xDst) * 3
arCropValues(index) = colour.B
arCropValues(index + 1) = colour.G
arCropValues(index + 2) = colour.R
Else
' 32bpp
Dim index As Integer = ((yDst * bmCrop.Width) + xDst) * 4
arCropValues(index) = colour.B
arCropValues(index + 1) = colour.G
arCropValues(index + 2) = colour.R
arCropValues(index + 3) = colour.A
End If
'Exit when hit width of the destination bitmap
xDst += 1
If xDst >= bmCrop.Width Then
Exit For
End If
Next
' Exit when hit bottom of the destination bitmap
yDst += 1
If yDst >= bmCrop.Height Then
Exit For
End If
Next
System.Runtime.InteropServices.Marshal.Copy(arCropValues, 0, cropPtr, arCropValues.Length)
' Unlock the bits.
bmCrop.UnlockBits(cropData)
Me.Image = bmCrop
End Sub
-- modified 9-Jun-19 5:35am.
|
|
|
|
|
After further testing I have found this.
It looks like the data return by LockBits() for the source image is not correct.
Below is a portion of data I dumped for Scan0 returned from LockBits().
The test image is 38 x 38 pixels with a single pixel vertical line in column 0.
It was generated in Paint and saved as .jpg.
The Scan0 data shown below shows that the output is being shifted right which is
what the resulting image shows.
Is this some quirk with LockBits() and 24bbp?
Also, I'm still on Windows 7.
Thanks.
Dim bmSrc As Bitmap = Me.Image.Clone() ' The PictureBox image
Dim rect As New Rectangle(0, 0, bmSrc.Width, bmSrc.Height)
Dim _BMPData As Imaging.BitmapData = bmSrc.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmSrc.PixelFormat)
' bmSrc.PixelFormat = Format24bppRgb {137224}
Dim _BMPPtr As IntPtr = _BMPData.Scan0
Dump:
This is the first 2 pixels of col 0 , scanline 0
index(0) - 0 - Col 0 Black
index(1) - 0
index(2) - 0
index(3) - 255 - Col 1 White
index(4) - 255
index(5) - 255
index(x) = 255 from here to next scanline.
This is the first 2 pixels at col 0, scanline 1;
pixel 38 * 3 = 114
index(114) - 0 - Col 0
index(115) - 0
index(116) - 0
index(117) - 0 - Col 1 should be 255
index(118) - 0 - ditto
index(119) - 255
index(x) = 255 from here to next scanline.
This is the first 3 pixels of col 0, scanline 2;
pixel 76 * 3 = 228
index(228) - 255 - Col 0 should be 0
index(229) - 255 - ditto
index(230) - 0
index(231) - 0 - Col 1 should be 255
index(232) - 0 - ditto
index(233) - 0 - ditto
index(234) - 0 - ditto
index(235) - 255
index(236) - 255
|
|
|
|
|
It's not the LockBits that's returning what you think is bad data. What's wrong is what you think the good data is.
The stride is not a perfect 38 pixels * 3 bytes per pixel for each line in your image. It's going to be rounded up to the next 4 byte boundary. The Bitmap object has a Stride property that will tell you how many bytes are in each scan line.
|
|
|
|
|
Wow, I knew that. I wrote some code some 'years' ago in c++ to manipulate bitmaps
with rotation and flipping. Forgot all about it.
Thanks for pointing that out. It's starting to come back to me.
|
|
|
|
|
Skewed.
Quote: ' Size to src bm above - 1 pixel in width to allow for 1 pixel offset; height is the same.
Dim bmCrop As Bitmap = New Bitmap(bmSrc.Width - 1, bmSrc.Height, bmSrc.PixelFormat)
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.'
― Confucian Analects
|
|
|
|
|
I have written two simple classes (clsMessage and clsHello). I create an instance of clsMessage and call the Message1 function passing the specific clsHello class that exposes the correct MessagBox to me. When call then Message2 function passing a generic object that expose the correct MessageBox.
these are the classes:
Public Class clsMessage
Public Function Message1(Of T As {clsHello, IHello})(ByVal value As String) As T
Return (New clsHello(value))
End Function
Public Function Message2(Of T)(ByVal value As String) As T
Return DirectCast((New clsHello(value)), Object)
End Function
End Class
Public Class clsHello
Implements IHello
Dim _value As String
Sub New(ByVal value As String)
_value = value
End Sub
Public Sub Hello() Implements IHello.Hello
Call MsgBox(String.Concat("Hello: ", _value))
End Sub
End Class
Public Interface IHello
Sub Hello()
End Interface
these are code that works:
Dim objMessage As New clsMessage
Dim objHelloSpecific As clsHello = objMessage.Message1(Of clsHello)("My name specific")
objHelloSpecific.Hello()
objHelloSpecific = Nothing
Dim objHelloGeneric As Object = objMessage.Message2(Of Object)("My name generic")
objHelloGeneric.Hello
objHelloGeneric = Nothing
objMessage = Nothing
But if using reflection not works and I don't know how to write the invoke method:
Dim typMessage As System.Type = Reflection.Assembly.GetExecutingAssembly.GetType("WindowsApplication1.clsMessage")
Dim typHello As System.Type = Reflection.Assembly.GetExecutingAssembly.GetType("WindowsApplication1.clsHello")
Dim refMessage As Object = typMessage.GetConstructor({}).Invoke({})
Dim funHelloSpcecific As Reflection.MethodInfo = typMessage.GetMethod("Message1")
Dim funHelloGeneric As Reflection.MethodInfo = typMessage.GetMethod("Message2")
Try
Dim refHelloSpecific As Object = funHelloSpcecific.Invoke(Nothing, {"My name specific"})
refHelloSpecific.Hello()
refHelloSpecific = Nothing
Catch ex As Exception
Call MsgBox(String.Concat("refHelloSpecific: ", ex.Message))
End Try
funHelloSpcecific = Nothing
Try
Dim refHelloGeneric As Object = funHelloGeneric.Invoke(Nothing, {"My name generic"})
refHelloGeneric.Hello
refHelloGeneric = Nothing
Catch ex As Exception
Call MsgBox(String.Concat("refHelloGeneric: ", ex.Message))
End Try
funHelloGeneric = Nothing
refMessage = Nothing
typHello = Nothing
typMessage = Nothing
The error is: Impossibile eseguire operazioni con associazione tardiva in tipi o metodi per i quali ContainsGenericParameters è true (Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true)
question: can someone help me to write the invoke method correctly? thank you.
Giorgio
|
|
|
|
|
You have a reference to the unbound generic method. You need to call MakeGenericMethod[^] to provide the type parameters.
You're also passing Nothing as the first parameter to Invoke . That will only work for Shared methods. You need to pass the instance of your clsMessage class to invoke a method on that instance.
Dim funHelloSpcecific As Reflection.MethodInfo = typMessage.GetMethod("Message1").MakeGenericMethod(typHello)
Dim refHelloSpecific As IHello = DirectCast(funHelloSpcecific.Invoke(refMessage, {"My name specific"}), IHello)
refHelloSpecific.Hello()
Dim funHelloGeneric As Reflection.MethodInfo = typMessage.GetMethod("Message2").MakeGenericMethod(typHello)
Dim refHelloGeneric As IHello = DirectCast(funHelloGeneric.Invoke(refMessage, {"My name generic"}), IHello)
refHelloGeneric.Hello()
Reflection (Visual Basic) | Microsoft Docs[^]
Generic Procedures in Visual Basic | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
wow, i'ts work!!! thanks so much
|
|
|
|
|
hi,
i want to change color txt in messagebox with API
i try so many i spent 10 hours to research of this issue
|
|
|
|
|
You can't change the style of the MessageBox ; it's controlled by the current Windows theme.
You'll have to create your own form to display your message. For example: Custom Message Box in VC#[^]
(The code is in C#, but it should be fairly easy for you to convert it to VB.NET if required.)
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hello !
I'm using vb.net 2017 and EF 6.
I have a bindingsource and also datagrid that is bound to this bindingsource.
I need a way to clear all the items from bindingsource to empty it , but without deleting from database.
I've tried
Bindingsource1.Datasource=Nothing.
But , after calling this , the datagrid still have all the bindingsource items.
|
|
|
|
|
You need to call the Refresh method to repaint the grid.
|
|
|
|
|
I've tried also
Bindingsource.Clear
Bindingsource.Clear Bindingsource.Clear ,
this is working , but does this remove also from database ( if I call savechanges for example ?)
|
|
|
|
|
You should not use savechanges unless you want the database updated.
|
|
|
|
|
ok , but Bindingsource.clear only remove items from bindingsource or also mark them for deletion from database ?
|
|
|
|
|
Sorry, I don't know the answer. You could study the documentation, or write a simple test program to try it out.
|
|
|
|