VB.NET - Hiding Form on Load - vb.net

So theres a few questions on this but they all give the same answer Me.Hide() which "works" if you count that when it loads, It will SHOW but then will hide seconds after which makes a weird Shadow-y-Laggy Effect.
Example:
(The small Window that shows and almost instantly hides is the window im trying to NEVER show)
Is there a way to actually hide the WHOLE form upon load? I know of the whole VisibilityCore method but with that I cant find a way to show it again at a later point.
So is there a way to hide it so it NEVER shows unless I tell it to Show?
Currently im using "Hide() combined with Form.Show Method".

If you set the form's Opacity property to 0 (through the Property Window) you shouldn't have this problem.
When you want to show the form (if it's going to be shown at all) just set the opacity back to 1.0.

Opacity is still a hack. The correct way is to not show it at all, versus making it invisible. To do this, don't make it the "Startup Object" at all.
On the Application tab of the Project Properties screen, there is a Startup Object setting. Create a Module with a Sub Main() and make that the entry point of your app by selecting it as the Startup Object instead of that little form that apparently doesn't do anything visual.
Maybe you have some initialization code in that starting form...move that to Sub Main.

you can minimize the form upon application launch.
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form fm = new Form1();
fm.WindowState = FormWindowState.Minimized;
Application.Run(fm);
}

Related

Reset focus to control with lowest tab index

I have a form that is re-used. That is, instead of creating a new instance of the form each time, the form is kept hidden, and is made visible when needed. (A design I inherited; I presume this was a performance optimization.)
The problem: The second time that the form is used, the focus is on the OK or Cancel button, from the first use of the form.
The user wants the focus to start the way it did the first time the form appears - on the control with lowest tab index.
If there were just one such form, I would hack it: add a line of code hardwired to the desired control.
But there are many such forms, and the visibility logic is in a common base class.
So it would make more sense to do this right, and tell the form to focus on its first (lowest tabindex) control.
Is there an easy way to do so?
(I could iterate through all the controls, but then I have to correctly handle nested controls. Since the GUI has to do this the first time it shows a form, I am hoping there is some method I can call that does it for me.)
(Coded in VB.net, but a C# answer would be fine.)
It is a one-liner, the logic to find the next control is exposed as a method, SelectNextControl(). You should start at the Form object, the one that can never get the focus, and ask it to find the next one in the tabbing order. Which is the child with the lowest TabIndex, whatever value it might have.
So something like this:
public void ShowAgain() {
this.Show();
this.SelectNextControl(this, true, true, true, true);
}
And do consider that a Form object that isn't visible is a rather major resource hog, using up lots of operating system resources for a small convenience. Surely you can also Close/Dispose it and recreate it when needed. YMMV.
You can try to set ActiveControl property before making form visible:
_frm.ActiveControl = null;
This should clear the active control for the form and remove focus from its controls.

AutomationPeer - how to show data of elements not on the control?

I want to write an AutomationPeer for my custom control in WPF.
Now, I want to show textBlocks \ TextBoxex that are not actually visible on my control.
I know how to override the method GetChildrenCore().
My problem is that when i run the playback (coded ui record) - it's trying to find a control not vissible on the window. Have you got any ideas?
You can set the SearchConfiguration VisibleOnly on the Coded UI control. Also, the UITestControl.FindMatchingControls method can let you know if the search properties are too ambiguous. Coded UI stops looking for a control after the first instance of a matching control is found.
inspect.exe can shed light on a form and how Coded UI may be seeing the control.
Try opening the Designer.cs and see the hierarchy and what Coded UI is trying to find.

Keep form on top of another form in modal fashion, but continue execution

I have a form in a vb.net windows form application called PolicyRefreshStatus.vb that has a ProgressBar control on it. From the main form called EditPolicy.vb I need to show PolicyRefreshStatus.vb over top of EditPolicy.vb - but the way things are wired I'm controlling the the ProgressBar and it's steps from logic inside EditPolicy.vb.
If I display the PolicyRefreshStatus.vb bar using the .show() method things work fine. The problem is if the user clicks back on the main form then PolicyRefreshStatus.vb losses focus. If I show PolicyRefreshStatus.vb as a modal form using .ShowDialog() then execution halts in EditPolicy.vb after the .ShowDialog() statement.
so for example in the code:
mPolicyRefreshStatus = New PolicyRefreshStatus
mPolicyRefreshStatus.pbMax = mPolicy.ClaimsUpdateMax
mPolicyRefreshStatus.ShowDialog()
mPolicy.UpdateFromFIS()
The line mPolicy.UpdateFromFIS() never executes because it's waiting for the PolicyRefreshStatus form to close.
How can I show PolicyRefreshStatus in a modal form but let execution continue in EditPolicy.vb?
You've got a couple of related options.
This first is to pass the unit of work to the progress bar in the form of a delegate or a class implementing an Interface. Something like this (not checked for correctness, just a rough example):
mPolicyRefreshStatus = New PolicyRefreshStatus
mPolicyRefreshStatus.pbMax = mPolicy.ClaimsUpdateMax
mPolicyRefreshStatus.UnitOfWork = AddressOf(mPolicy.UpdateFromFIS())
mPolicyRefreshStatus.ShowDialog()
Then within the progress form you can call back to the routine that actually does the work.
Another approach is to define events on your ProgressForm and then the owning/launching object can handle those events to do the work in. With this option you can create a fairly detailed set of events to be able to handle incremental work or cancels, but the concept is the same, youu are calling back from the progress form into launcher to perform the actual business logic.
You cannot show the form modally and let your routine continue. You must show the form Non-modally and then do your other stuff, closing the form when you've finished. Maybe a loop until the task has finished?
Using Show() with a parent form parameter will give you better usability. More like a tool window.

How can I DoubleBuffer a winforms Treeview?

I have a standard winforms treeview control that keeps flickering whenever I hover my mouse over any other control on the form. I would like to doubleBuffer the treeview to reduce the flickering but I have no idea how to do so.
Could someone please show me how to achieve my goal?
Many thanks
I had to implement a double buffered TreeView a way back when developing some parts of a financial software, because of the same scenario. The TreeView implementation in .NET is a pretty sketchy one, but here is how I resolved it:
Public Class DoubleBufferedTreeView
Inherits System.Windows.Forms.TreeView
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.UpdateStyles()
End Sub
End Class
The other reason I implemented this in this manner was because I had to do some custom drawing to show where the user was dragging-and-dropping the TreeNodes, so I did some custom drawing to display a bar in between nodes.
DoubleBuffering was not a fullproof solution as the TreeView flickered slightly, but that was the best I was able to get it at the time. I also did not want to suspend the TreeView as others have stated, because I still wanted the TreeView to perform its layout and normal operations, even when the user was possibly using different parts of the UI.
PS. the code is almost identical for C#.
All answers here are wrong.
The Treeview ignores
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
completely.
And using BeginUpdate() or SuspendLayout() does not change anything.
The correct answer was given by Hans Passant here: Treeview flickering?
In Addition to activating the double buffering like Int3 already said, I suggest you to suspend the layout logic temporarily while you do the processing that causes the flickering.
You need to first call SuspendLayout to stop building the full tree contents in th UI.
After you have finished your processing, you start the layout logic again eith ResumeLayout().
MSDN documentatiion for SuspendLayout with code sample is here.
private void buildTreeContent()
{
// Suspend the form layout and add two buttons.
this.SuspendLayout();
// Do your work here
// ...
// Make the Form do paint the layout again.
this.ResumeLayout();
}
This should help a great deal of flicker as building the tree element is resource consuming, we've done that a lot of times in our projects.
An alternative approach is to work with the Windows messages.
This is explained in more depth in another SO thread.
DoubleBuffer does not affect Treeview in .NET. If your purpose is to reduce flicker when the TreeView is drawn, then I suggest you have a look at BeginUpdate and EndUpdate.
I had a similar issue, and attempted to double buffer the form in hopes of it fixing the issue with my treeview. As it turns out, setting the DoubleBuffered property does not affect the TreeView control.
Hope this helps. Useful link
Hope you are ok with the C# code as it is trivial.
DoubleBuffer is actually a protected property of the Control. So you can only access it from the declaring class or that are derived.
Following snippet will help you understand how to set this property.
public sealed class MyNonFlickringTreeView:Treeview
{
public MyNonFlickringTreeView()
{
this.DoubleBuffered=true;
}
}

Add a spinning wheel while application is searching database

I have an application that searches in a database for some information.
Since the database is quite big, it sometimes takes a lot of time before the application returns the results to the interface.
I want to add some sort of spinning wheel to inform the user that the application is still searching the database and did not freeze. Once the results are returned, the wheel should disappear.
Any idea how to do this or is there a good tutorial explaining how to do this?
Have you considered changing the mouse pointer to the hourglass as this would be extremely simple to implement:
Me.Cursor = Cursors.WaitCursor
...Do your DB calls here...
Me.Cursor = Cursors.Default
However, I would agree that display a 'spinning wheel' is probably a little more user friendly and definately a lot more obvious. So, first get hold of an animated gif that suits your needs. Then create a form that has a picture box containing the image.
Once you have that you can show the form to the user and in the background do the DB work, once this has completed close the form.
Another alternative would be to use a rolling progress bar instead, so when it reaches 100% it cycles around again and keeps going until you close it.
EDIT:
One thing that I forgot to mention is that you will have to handle exception conditions. Lets say you set the cursor to wait, then an error occurs. The exception may bypass the code that resets everything. This leaves the user with a changed cursor and no means of changing it.
When I have done this kind of thing I have typically created a disposable WaitCursor class and then used something like this:
Using myWaitCursor As WaitCursor = New WaitCursor
...do something...
End Using
In the Dispose of the WaitCursor class you set the cursor back to default. The same would apply if you went down the route of using a form with an image or progress bar.
Find an animated gif of such a spinner, like this one. Put it in a PictureBox, set its Visible property to True when you start the job. Beware that you'll have to run the query in a worker thread to keep the animation alive and the user interface responsive. The BackgroundWorker class is good for that.
You could use the Environments Default wait cursor, which for Vista/7 is a circle with the outside spinning, or the XP tumbling hourglass.
You could launch your DB access on a BackgroundWorker and show an animated control such as a Marquee progress bar, or you could show a custom animation to show Busy status.