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.

Apr 27

Tabstops in a .Net CheckedListbox

Marco HokkeComments Off on Tabstops in a .Net CheckedListbox

imageFor one of my projects I needed to create a simple CheckedListbox containing various entries of a specific entity, of which the user may select one (and only one). I wanted the entries of this listbox to be more than just the instance name of an entity; they should also include a description and optionally other data depending on the type of entity. And I wanted these items to be lined up.

One way to do this is to use a gridview, but it was too much hassle and gridviews generally don’t look very good. Should you consider this choice: note that the checked listbox I use does not have columnheaders.

There is an out-of-the-box way to line up the various items in each line of the checked listbox. This post will tell you how to achieve this.

Continue reading »

Jan 25

Deploying Sql Server Compact Edition

Marco HokkeComments Off on Deploying Sql Server Compact Edition

imagesWhen you are deploying an application that uses the Sql Server Compact Edition, you need to make sure the target machine is able to run the Compact Edition. There are two ways of doing this: a silent way that leaves no trace after uninstalling your application, and the transparent way that installs a separate instance of the Compact Edition.

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 »

Jan 20

Prezi Error id: 2000, Error #2035

Marco HokkeComments Off on Prezi Error id: 2000, Error #2035

A colleague gave me a zipped Prezi presentation the other day, and when I opened it after unzipping the presentation gave me an error on startup:

image

The error details “id: 2000 the reason is: PreziError id: 2000 the reason is: ErrorEvent ioError Error #2035” do not give much of a clue as to what has happened.

In my case it turned out that two of the pictures used in the presentation had a different filename after zipping and unzipping. Before zipping, both pictures had spaces in their filenames. After zipping and unzipping, those spaces were replaced by ‘%20’ and Prezi could no longer find the pictures.

You can find the pictures used in a presentation in the data\repo folder of the zipped/unzipped presentation. I changed the filenames back to their original value, and the error disappeared.

Jan 05

Avoid the ‘System.ComponentModel.Design.ExceptionCollection’ when opening a form

Marco HokkeComments Off on Avoid the ‘System.ComponentModel.Design.ExceptionCollection’ when opening a form

I’ve had a problem with one of the forms in my solution. The main form, to be precise, which contained a lot of controls and a few user controls. When opening the form in the designer of Visual Studio 2010, it would display a messagebox saying

“Exception of type ‘System.ComponentModel.Design.ExceptionCollection’ was thrown.”

This prevented the designer from showing the form—and me from working on it!

I traced the problem to code contained in two of my user controls. To this yourself, open a second instance of Visual Studio 2010 and attach it to the process of the instance you have your solution in: Alt + Ctrl + P, and choose devenv.exe. The in the Debug menu, go to Exceptions and check both Thrown and User-unhandled boxes for the Common Language Runtime Exceptions. Then go back to the instance with your solution, and try and open the form. The instance that is attached will break on any exception and will give you information on what code exactly raises which exception.

I found that some code attached to my user controls executed during runtime and threw an Object not set to an instance of an object exception. I must admit I do not know why the code executed, but I did find a way to avoid it. Any user control has the property DesignMode, which is True if the control is shown in design mode. Simply put the offending code in between an If..End If block, like so:

If Not Me.DesignMode Then
   ...
End If

Do this for any code that raises the System.ComponentModel.Design.ExceptionCollection exception, and the exception should no longer be thrown when opening the form in the designer!

Dec 19

Statusmessages from threads in VB.Net

Marco HokkeComments Off on Statusmessages from threads in VB.Net

When developing an application that has a few time-consuming tasks, and a user interface, it may be important to you to keep a responsive interface while still performing that task. Simply calling a method that takes a long time will freeze the GUI and may leave the user thinking the application has crashed. Using threading is the solution to this problem.

And it introduces new difficulties. You can’t access controls on a form from a thread; so showing the user what is happening has to be done in a different way. Keeping the UI responsive is great, but you also want the UI to show some kind of progress of the task at hand.

I’m going to explain 2 ways of doing this. The first, and easy, way is by using the .Net component BackgroundWorker. The second way is to use delegates.

Continue reading »