Mar 13

Returning a string from unmanaged dll to .net

Remko WeijnenComments Off on Returning a string from unmanaged dll to .net

I write most of my code in unmanaged languages such as Delphi and C/C++. Sometimes I need to interface my code to .net code in which case I create a dll.

A recurring thing is that I need to return string to .net.

There are many ways to do this of course but in all cases we need to manage memory: who will allocate the memory for the string and who is responsible for freeing it?

Continue reading »

Dec 18

Dragging in vb.net, using a custom cursor

Marco HokkeComments Off on Dragging in vb.net, using a custom cursor

downloadWondering how you can display a custom cursor while dragging an item in your .Net application? Here’s how:

  1. Invoke the DoDragDrop method of the control you are dragging. This may sound obvious, but if you use the general DoDragDrop method then the GiveFeedback event of the control will not fire (see point 2).
  2. Use the GiveFeedback of the control you are dragging. In this event, set the Cursor.Current property to the cursor you created just before calling the DoDragDrop method (see point 1).
  3. Create a custom cursor based on the control, and save it in a private property or class variable. Any control can return a bitmap of itself and you can use this bitmap to create a cursor. You can also load a bitmap from file or use one from your resources.
  4. Reset the cursor property back to default (of whatever you want) right after the call to the DoDragDrop method in point 1.

Let’s say you want to drag a picturebox and want to use its contents as a cursor while dragging. Then there are two events you need: picturebox1.MouseDown, and picturebox1.GiveFeedback. In this code I use a private property to store the cursor so that I can set its value before calling DoDragDrop in the MouseDown event, and then use it in the GiveFeedback event.

Also, and it’s so obivous I almost did not mention this, you need a control that allows a drop and that has the DragOver and DragDrop events initiated. I assume you have done this and I will focus on the picturebox side of this scenario.

Continue reading »

Oct 02

DevExpress TileItems: Drag and Drop and Click

Marco HokkeComments Off on DevExpress TileItems: Drag and Drop and Click

Author: Marco Hokke


DevExpress
offers a nifty little control in its collection of editors: the TileControl It’s a control that offers a Metro-style interface using tiles, grouped together, and with a scrolling-mechanism that mimics the Metro interface.

In a normal desktop environment, however, this interface lacks a standard way of dragging and dropping individual tiles onto a different control. For example, for one of our products we want the user to be able to drag a tile onto a DevExpress ChartControl, and configure what data is shown on the axes of the ChartControl.

In standard .Net controls the drag and drop behavior is implemented in the MouseDown event using .Net’s DoDragDrop method. But TileItems lack this event; all you have to work with are the ItemPress and ItemClick events. This post will show you how to use these two events to implement both drag and drop functionality and keep the standard click function of the TileControl. Note that in order to do this, we must disable the TileControl’s ability to rearrange its tiles.

Continue reading »

Jul 30

Opening DevExpress ChartWizard on a specific page

Marco HokkeComments Off on Opening DevExpress ChartWizard on a specific page

For one of our products we are using the DevExpress ChartControl and we use the standard DevExpress ChartWizard to allow the user to customize the appearance of the chart. The ChartWizard is a wonderful object as it provides access to virtually all available properties of the ChartControl, and takes care of updating the ChartControl automatically.

imageIt has one drawback, though: it lacks a property through which you can set on which page the wizard should open. There are 13 pages in the standard wizard, and more if you add custom pages to it. And it will always open on the first page in the list which will be the Chart Type page (see screenshot on the right). Confusing if you just want to modify the chart’s title!!

There is a great way to determine which element of a chart the user has double-clicked: the ChartHitInfo object. By calling the CalcHitInfo method you can determine where the user has double-clicked on the ChartControl (by examining the InLegend, InChart, InDiagram, InAxis, and other properties like these). This will tell you which element the user would like to edit, and which page you would like to display in the ChartWizard.

But as I just mentioned, there is no (standard) way of opening the ChartWizard on any of its 13 pages except the first. Luckily, there is a workaround. All DevExpress wizards have methods to add or remove a page to or from a wizard, and to add or remove groups of pages. Using these methods, we can list the steps we need to take to show a specific page after opening the ChartWizard:

  • Remove the specific page from its group
  • Create a new group in the wizard
  • Add the specific page to the new group
  • Move the new group to the top of the group list

This will make sure that the top page in the list of pages is the requested page, and so that that specific page will be displayed when opening the ChartWizard!

My workaround consist of three methods: the double-click event on the ChartControl , a method to instantiate and display the ChartWizard, and a method to configure the ChartWizard so that it will display a particular page when opening.

Continue reading »

May 30

.Net data-adapter’s DeleteCommand exception

Marco HokkeComments Off on .Net data-adapter’s DeleteCommand exception

I hit an exception when adding auto-generated commands from a CommandBuilder object to a DataAdapter object. Specifically, when assigning the return value of the CommandBuilder.GetDeleteCommand() method to the DeleteCommand property of the DataAdapter object. The exception message reads: “Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information.”. The exception may also be thrown when adding a command to the UpdateCommand property of a DataAdapter object.

The exception message is not very helpful in finding the problem and fixing it. After some research I found that the reason this exception is thrown is because both the DeleteCommand and the UpdateCommand require a primary key in the table they are meant to modify. When the table does not have one, the DeleteCommand and UpdateCommand cannot accurately identify which row to delete or update, and they will throw the exception mentioned above.

The solution is simply to add a primary key to the table you want the DataAdapter object to connect to.

Mar 14

DataSet.ReadXML throws DirectoryNotFoundException

Ingmar VerheijComments Off on DataSet.ReadXML throws DirectoryNotFoundException

Author: Ingmar Verheij

A .NET application that reads a dataset from an XML using the DataSet.ReadXML method might throw the exception : System.IO.DirectoryNotFoundException: Could not find a part of the path ‘<path of XML file>’.

According to MSDN this exception is thrown when when “part of a file or directory cannot be found” .However, this exception is not only thrown when a file cannot be found on the disk. The exception is also thrown when the structure of the XML is invalid (for instance because you didn’t close a node)

Continue reading »

Jan 24

Using Sql Server Compact edition in VB.Net

Marco HokkeComments Off on Using Sql Server Compact edition in VB.Net

imagesThe available official help in using a Sql Server Compact edition in a .Net program is not very extensive. In fact, it does not mention some elemental aspects that you need in order to create a database. This post will show you how to create a new database or connect to an existing one.

I assume you have already downloaded and installed the latest version of the SQL Server Compact edition, and have added a reference to it in your VB.Net project. Also, that you have imported the right namespace (System.Data.SqlServerCe).

Continue reading »

Mar 04

Visualizing a form’s controls’ hierarchical structure

Marco HokkeComments Off on Visualizing a form’s controls’ hierarchical structure

I’ve had to add a control to a panel in a complicated form, in which many panels overlap and are docked. Working from the VS designer it is not always clear where the focus lies or what control is selected. It’s even worse trying to figure out to which panel control I should add my new control.

So I’ve written a small recursive function that returns a TreeNode object that contains all the controls in their hierarchical structure, starting from the form itself. Together with a form that contains only a TreeView control and takes a TreeNode in its constructor, I could easily find out to which panel I had to add a control and how to dock or anchor it. Here’s the function:

Function CreateTreeNode(ByVal p_objControl As Control) As TreeNode

    Dim l_objTreenode As New TreeNode()
    l_objTreenode.Text = p_objControl.Name

    If p_objControl.HasChildren Then
        For Each l_objSubControl As Control In p_objControl.Controls
            l_objTreenode.Nodes.Add(CreateTreeNode(l_objSubControl))
        Next
    End If

    Return l_objTreenode
End Function

If you then add this node to a TreeView control, you will see how the form’s controls are structured, and you can determine where to add what!

Mar 04

A replacement for String$

Marco HokkeComments Off on A replacement for String$

I’ve been looking for a replacement for the VB6 (and QuickBasic) function String$ for ages. Usually I just use PadLeft and PadRight if I can, but sometimes that really isn’t what I want: a string that consists of a specific number of occurences of the same character—almost always a space.

Creating your own method to do this is very straightforward, but today I’ve found a little VB.Net function that does just this! It’s the StrDup function, and it takes two parameters: the number of times you want a character to be repeated, and the character:

Dim l_objFiller As String = StrDup(10, " ")

will give you a string of 10 spaces.

Oct 21

True is not true (?)

Ingmar VerheijComments Off on True is not true (?)

The string 'True' is not a valid Boolean value.

This error is thrown when an XML file is read into a DataSet using “System.Boolean” as DataType.
Continue reading »