What is the equivalent of My.Resources in Visual-C++? - vb.net

I need to iterate through all the resources in a project and basically output their names. I have this done in VB. But I can't figure out what the equivalent of My.Resources.ResourceManager is in VC++.
Here's the VB code.
Dim objResourceManager As Resources.ResourceManager = My.Resources.ResourceManager
Dim objResourceSet As Resources.ResourceSet = objResourceManager.GetResourceSet(CultureInfo.CurrentCulture, True, True)
Dim iterator As IDictionaryEnumerator = objResourceSet.GetEnumerator()
Private Sub go()
Dim s As String = iterator.Key
Debug.WriteLine(s)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If iterator.MoveNext Then
go()
Else
iterator.Reset()
If iterator.MoveNext Then
go()
Else
Throw New Exception("No elements to display")
End If
End If
End Sub
And this is how far I am in VC++.
private:
Resources::ResourceManager^ rmgnr;
Resources::ResourceSet^ rSet;
public:
Form1(void)
{
rmgnr = gcnew System::Resources::ResourceManager(L"Resources ProjectCPP",Reflection::Assembly::GetExecutingAssembly());
//This is the problem as I can't find the equivalent in c++
rSet = rmgnr->GetResourceSet(CultureInfo::CurrentCulture,true,true);
Please help me figure this out.

I think you just want:
rmgnr = gcnew System::Resources::ResourceManager(GetType());

You can use something like the following for unmanaged C++:
HRSRC hResInfo = FindResource(hInstance, MAKEINTRESOURCE(resourceId), type);
HGLOBAL hRes = LoadResource(hInstance, hResInfo);
LPVOID memRes = LockResource(hRes);
DWORD sizeRes = SizeofResource(hInstance, hResInfo);
You will need to change the type and resourceId to match your resource. Not sure if its an image or icon or what kind of resource, but you would use something like:
FindResource(hInstance, MAKEINTRESOURCE(bitmapId), _T("PNG"));
For Managed C++, try something like the following:
Bitmap *MyBitmap;
String *Msg;
Reflection::Assembly *MyAssembly;
IO::Stream *ResourceStream;
MyAssembly = System::Reflection::Assembly::GetExecutingAssembly();
ResourceStream = MyAssembly->GetManifestResourceStream(ImageName);
if (ResourceStream != NULL)
{
MyBitmap = new Bitmap(ResourceStream);
Msg = String::Format("GetIcon: {0}, OK", ImageName);
}
else
Msg = String::Format("GetIcon: {0}, Failed", ImageName);
// MyBitmap countains your resource
You will need to replace ImageName with the name of your resource you are trying to grab. Again, I'm assuming its an image resource you are trying to grab.

Related

VB.NET: Instantiate a nested property by reflection

I want to set the values of the properties via reflection. In this thread they propose a solution. But the problem with the solution is that it is not instantiating the properties. But I want to check and instantiate the properties if necessary. My DTO is:
Public Class root
Public Property Printing() As rootPrinting
End Class
Public Class rootPrinting
Public Property Printer() As String
Public Property PrinterBatch() As String
End Class
Now for setting the properties I have defined the following function:
Public Sub SetProperty(ByVal target As Object, ByVal compoundProperty As String, ByVal value As Object)
Dim properties As String() = compoundProperty.Split("."c)
For i As Integer = 0 To properties.Length - 1 - 1
Dim propertyToGet As PropertyInfo = target.[GetType]().GetProperty(properties(i))
target = propertyToGet.GetValue(target, Nothing)
if IsNothing(target) then
if propertyToGet.PropertyType.IsClass then
target = Activator.CreateInstance(propertyToGet.PropertyType)
End If
End If
Next
Dim propertyToSet As PropertyInfo = target.[GetType]().GetProperty(properties.Last())
propertyToSet.SetValue(target, value, Nothing)
End Sub
Then I call it like this:
Dim configObject as New root
SetProperty(configObject , "Printing.Printer","skjfkd")
If before calling SetProperty(configObject,...) I instantiate configObject.Printing then it will work fine:
Dim configObject as New root
configObject.Printing = new rootPrinting()
SetProperty(configObject , "Printing.Printer","skjfkd")
Otherwise after calling SetProperty(...), configObject.Printing will be Nothing.
It seems that when calling Activator.CreateInstance(propertyToGet.PropertyType) the reference to the original object is lost. While the object in the function is really initialized, the main object remains Nothing. How can I instantiate the class property correctly?
This question/answer was very helpful to me (thanks Code Pope!), I needed the same code in C#:
public void SetProperty(object target, string compoundProperty, object value)
{
var properties = compoundProperty.Split('.');
for (int i=0; i < (properties.Length - 1); i++)
{
var propertyToGet = target.GetType().GetProperty(properties[i]);
var property_value = propertyToGet.GetValue(target, null);
if (property_value == null)
{
if (propertyToGet.PropertyType.IsClass)
{
property_value = Activator.CreateInstance(propertyToGet.PropertyType);
propertyToGet.SetValue(target, property_value);
}
}
target = property_value;
}
var propertyToSet = target.GetType().GetProperty(properties.Last());
propertyToSet.SetValue(target, value);
}
Ok. The problem was solved. To solve the problem the code has to be modified as following:
Public Sub SetProperty(ByVal target As Object, ByVal compoundProperty As String, ByVal value As Object)
Dim properties As String() = compoundProperty.Split("."c)
For i As Integer = 0 To properties.Length - 1 - 1
Dim propertyToGet As PropertyInfo = target.GetType().GetProperty(properties(i))
Dim property_value = propertyToGet.GetValue(target, Nothing)
If IsNothing(property_value) Then
If propertyToGet.PropertyType.IsClass Then
property_value = Activator.CreateInstance(propertyToGet.PropertyType)
propertyToGet.SetValue(target, property_value)
End If
End If
target = property_value
Next
Dim propertyToSet As PropertyInfo = target.GetType().GetProperty(properties.Last())
propertyToSet.SetValue(target, value)
End Sub

Referencing an Unbound DataGridView Without Specifically Naming It?

I am using 3 unbound DataGridView controls to display certain information. To load the information into those DGVs, I am pulling the information from an encrypted file, decrypting it, parsing the information, then trying to fill the DGVs with that information. The loading from the file is called by the menu item click. Here is what I have so far:
Private Sub miCLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles miCLoad.Click
Dim FilePath As String = "C:\FList\CList.clt"
Dim LoadFile As New SaveandLoad.SaveAndLoad
Dim FileRead As New Simple3Des("MyPassword")
Dim FileString As String = FileRead.ReadFile(FilePath)
With LoadFile
.WhichList = dgCourses
.FilePath = FilePath
.DecryptedString = FileRead.DecryptData(FileString)
.dgList = dgCourses
End With
Call LoadFile.LoadFile()
End Sub
Public Class SaveandLoad
Public Property WhichList As New DataGridView
Public Property FilePath As String
Public Property DecryptedString As String
Public Property EncryptedString As String
Public Property dgList As Control
Public Sub LoadFile()
Dim dgRow As DataGridViewRow
Dim dgCell As DataGridViewTextBoxCell
Dim Lines() As String = DecryptedString.Split(vbLf)
Dim LinesList As List(Of String) = Lines.ToList
LinesList.RemoveAt(Lines.Length - 1)
For Each Line As String In LinesList
Dim Fields() As String = Line.Split(",")
dgRow = New DataGridViewRow
For x = 0 To (WhichList.Columns.Count - 1) Step 1
dgCell = New DataGridViewTextBoxCell
dgCell.Value = Fields(x).ToString
dgRow.Cells.Add(dgCell)
Next
WhichList.Rows.Add(dgRow)
Next
Select Case WhichList.Name
Case "dgCourses"
frmFacultyList.dgCourses = WhichList
frmFacultyList.dgCourses.Refresh()
WhichList.Dispose()
Case "dgFList"
frmFacultyList.dgFList = WhichList
frmFacultyList.dgFList.Refresh()
WhichList.Dispose()
Case "dgSList"
frmFacultyList.dgSList = WhichList
frmFacultyList.dgSList.Refresh()
WhichList.Dispose()
End Select
MsgBox("List Successfully Loaded", vbOKOnly, "Load")
End Sub
I want to be able to reference (or fill) a DGV without using 'select case' or 'if-then' statements. This will be too inefficient once I start adding the many other DGVs, that will be added in the future. Therefore, the title is the main question. I am using VS Express 2010.
I don't know VB too much, however, I'll post my solution in C# (may be helpfull in some way....)
DataGridView myDGV;
foreach (var item in this.Controls)
{
if (item.GetType() == typeof(DataGridView))
{
if (((DataGridView)item).Name == WhichList.Name)
{
//Cannot assing to 'item' here, because it is a 'foreach iteration variable'
//However you can save the variable for later use.
myDGV = (DataGridView)item;
}
}
}
myDGV = WhichList;
// different approach
DataGridView myDGV = (DataGridView)this.Controls.Find(WhichList.Name, false).First();
myDGV = WhichList;
Here is what worked for me in VB.NET:
Dim FormControls As New frmFacultyList.ControlCollection(frmFacultyList)
For Each DGV As DataGridView In FormControls
If WhichList.Name = DGV.Name Then
DGV = WhichList
DGV.Refresh()
End If
Next
Make an instance of the control collection then search specifically for DGVs using For Each. Simple and efficient.

Strange error reported in this code, please explain?

I put together this bit of code from a few other samples, and I am getting an error I cant understand. On this line in the code below, on the word Observer,
Dim Results As ManagementObjectCollection = Worker.Get(Observer)
I get the error
"Value of type 'System.Management.ManagementOperationObserver' cannot be converted to 'Integer'"
Can somebody explain what this means?
There are two signatures for ManagementObjectSearcher.Get(), one has no parameters and the other has one parameter, a ManagementOperationObserver for async operation. That is what I am providing, yet the error indicates conversion involving an integer?
Public Shared Sub WMIDriveDetectionASYNC(ByVal args As String())
Dim Observer As New ManagementOperationObserver()
Dim completionHandler As New MyHandler()
AddHandler Observer.Completed, AddressOf completionHandler.Done
Dim Machine = "192.168.0.15"
Dim Scope = New ManagementScope("\\" & Machine & "\root\cimv2")
Dim QueryString = "select Name, Size, FreeSpace from Win32_LogicalDisk where DriveType=3"
Dim Query = New ObjectQuery(QueryString)
Dim Worker = New ManagementObjectSearcher(Scope, Query)
Dim Results As ManagementObjectCollection = Worker.Get(Observer) 'use parameter to make async
For Each item As ManagementObject In Results
Console.WriteLine("{0} {2} {1}", item("Name"), item("FreeSpace"), item("Size"))
Dim FullSpace As Long = (CLng(item("Size")) - CLng(item("FreeSpace"))) \ 1000000
Console.WriteLine(FullSpace)
Next
End Sub
Public Class MyHandler
Private _isComplete As Boolean = False
Public Sub Done(sender As Object, e As CompletedEventArgs)
_isComplete = True
End Sub 'Done
Public ReadOnly Property IsComplete() As Boolean
Get
Return _isComplete
End Get
End Property
End Class
Thanks for any advice!
I think that uses a reference type to get the result and put it in the object you sent as a parameter. So I think it just needs to look like:
Worker.Get(Observer)
instead of trying to set something = to that since it isn't a function that returns a value.
Then use the events you hook up to the object to handle whatever you need to do with the items you find.

VB.net: Object not set to an instance of object

I am getting a Object not set to an instance of object error. I have put a list view where all messages should be shown. I am using lumisoft sample code which I ported to vb.net
Private Sub FillMessagesList()
Me.Cursor = Cursors.WaitCursor
Try
Dim m_pPop3 As POP3_Client = Nothing
For Each message As POP3_ClientMessage In m_pPop3.Messages
Dim mime As Mail_Message = Mail_Message.ParseFromByte(message.HeaderToByte())
Dim item As New ListViewItem()
If mime.From IsNot Nothing Then
item.Text = mime.From.ToString()
Else
item.Text = "<none>"
End If
If String.IsNullOrEmpty(mime.Subject) Then
item.SubItems.Add("<none>")
Else
item.SubItems.Add(mime.Subject)
End If
item.SubItems.Add(mime.[Date].ToString())
item.SubItems.Add(CDec(message.Size / CDec(1000)).ToString("f2") & " kb")
item.Tag = message
ListView1.Items.Add(item)
Next
Catch x As Exception
MessageBox.Show(Me, "Errorssssss: " + x.Message)
End Try
Me.Cursor = Cursors.[Default]
End Sub
The problem is here:
Dim m_pPop3 As POP3_Client = Nothing
For Each message As POP3_ClientMessage In m_pPop3.Messages
You set m_pPop3 to Nothing and then try to access one of its members.
You say that you ported the code - perhaps you need to look back at the original code and port it correctly:
private POP3_Client m_pPop3 = null;
/// <summary>
/// Default constructor.
/// </summary>
public wfrm_Main()
{
InitUI();
this.Visible = true;
wfrm_Connect frm = new wfrm_Connect(
new EventHandler<WriteLogEventArgs>(Pop3_WriteLog));
if(frm.ShowDialog(this) == DialogResult.OK){
m_pPop3 = frm.POP3;
// etc...
}
private void FillMessagesList()
{
this.Cursor = Cursors.WaitCursor;
try{
foreach(POP3_ClientMessage message in m_pPop3.Messages){
// etc...
}
}
Notice that m_pPop3.Messages is a private member here, not a local variable as you have implemented it.
To correct your code I would suggest changing it to be more similar to the original. Change the local variable to a private member and set it in the constructor, just as the original C# code does.
The culprit is possibly from the code in the 2 lines:
Dim m_pPop3 As POP3_Client = Nothing
For Each message As POP3_ClientMessage In m_pPop3.Messages
You're trying to loop through the messages in "m_pPop3" but you've explicitly set it to nothing on the line directly above.
I'm guessing it's here since you're setting m_pPop3 to Nothing. If you'd stepped through the code it would show you that.
Dim m_pPop3 As POP3_Client = Nothing
For Each message As POP3_ClientMessage In m_pPop3.Messages

How do I validate a string of XML against an XML Schema file

I'm developing a VB Web Application in .NET3.5 using Visual Studio 2008.
I'm having difficulty in validating some XML as a string before I add it to a HTML form to post to a 3rd party. I have an XML schema file from the 3rd party to validate against and at this point I'd like the application to perform the validation before each post.
After searching I've found references to a XmlValidatingReader but this is obsolete and I'm having difficulty finding another way to do it.
Also all the good examples are in C# - for now I'm stuck with VB. This is what I have so far which I'm looking for help with!
Public Function ValidateXML(ByVal strXML As String) As Boolean
' er how do I get the schema file into here?
Dim schema As XmlReader
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add("", schema)
settings.ValidationType = ValidationType.Schema
' When I use LoadXML to get the string I can't use the settings object above to get the schema in??
Dim document As XmlDocument = New XmlDocument()
document.LoadXml(strXML)
document.Validate(AddressOf ValidationEventHandler)
End Function
Private Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
' Gonna return false here but haven't got to it yet! Prob set a variable for use above
End Sub
Thanks
Here's an example:
XmlSchemaValidator in VB.NET
UPDATE - Try this:
Public Function ValidateXML(ByVal strXML As String) As Boolean
Dim xsdPath As String = "path to your xsd"
Dim schema As XmlReader = XmlReader.Create(xsdPath)
Dim document As XmlDocument = New XmlDocument()
document.LoadXml(strXML)
document.Schemas.Add("", schema)
document.Validate(AddressOf ValidationEventHandler)
End Function
This is what I ended up going with
Public validationErrors As String = ""
Public Function ValidPortalRequest(ByVal XMLPortalRequest As String) As Boolean
Try
Dim objSchemasColl As New System.Xml.Schema.XmlSchemaSet
objSchemasColl.Add("xxx", "xxx")
objSchemasColl.Add("xxx", "xxxd")
Dim xmlDocument As New XmlDocument
xmlDocument.LoadXml(XMLPortalRequest)
xmlDocument.Schemas.Add(objSchemasColl)
xmlDocument.Validate(AddressOf ValidationEventHandler)
If validationErrors = "" Then
Return True
Else
Return False
End If
Catch ex As Exception
Throw
End Try
End Function
Private Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
validationErrors += e.Message & "<br />"
End Sub
Same as Jose's except I've added 2 XSD as a SchemaSet rather than reading them in with an XMLReader.