Why does my image not show properly after clicking? VB.NET - vb.net

I'm having this code:
Private Sub FormatMouseOver_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles FormatMouseOver.MouseClick
FormatClicked.Enabled = True
FormatClicked.Visible = True
BigLettersNoClicked.Enabled = True
BigLettersNoClicked.Visible = True
FormatMouseOver.Visible = False
FormatMouseOver.Enabled = False
End Sub
all the variables are images stored in pictureBoxes. The first one mentioned (FormatClicked) does show and enables itself properly, however the secound one (BigLettersNoClicked) doesn't do anything. The two pictureBoxes overlap, the BigLettersNoClicked is exactly one layer above FormatClicked. I tried putting it on any other place in the project and it still doesn't work.
Just to clarify: FormatMouseOver disabling works fine, and there are no errors during debuging
Subsequent question: since i'm new to the vb.net, can anyone explain to me, what is the difference between MouseClick and Click events?
Thanks

As always, MSDN provides a good summary about that topic:
Maybe this helps you: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.click%28v=vs.110%29.aspx

Related

Perform Load upon after click button

I am trying to use the panel and buttons to load 2 different forms along with entity framework and ado model stuff. I am trying to load the 2nd form this picture, using the codes running on the first form since they have the same table. but what happen is whenever I close the 2nd form and try to switch on the second user from this form(driverlist), I am still getting the results that I created before on the first user, I kinda think that the 2nd form is not being loaded although I put some code:
Private Sub DriverLicense_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim user_id As Integer = Driverlist.tbxUser_id.Text
Driverlist.db = New [Emme_Subic_Transport_Corporation_Payroll].EmmeSubicEntities
Driverlist.db.UserDetails.Where(Function(c) c.isDeleted <> 1 And c.Id = user_id).Load()
UserDetailBindingSource.DataSource = Driverlist.db.UserDetails.Local
If Driverlist.tbxUser_id.Text.Count < 1 Then
MessageBox.Show("Please Select an Employee ID First", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
Drivertest.Panelswitch(Driverlist)
Else
Me.Show()
End If
End Sub
Private Sub DriverLicense_Activated(sender As Object, e As EventArgs) Handles Me.Activated
Dim user_id As Integer = Driverlist.tbxUser_id.Text
Driverlist.db = New [Emme_Subic_Transport_Corporation_Payroll].EmmeSubicEntities
Driverlist.db.UserDetails.Where(Function(c) c.isDeleted <> 1 And c.Id = user_id).Load()
UserDetailBindingSource.DataSource = Driverlist.db.UserDetails.Local
End Sub
I binded the 2nd form through other resources of data binding from the properties but still on the same resources as the first form. Im pretty new to this so please stop putting negatives on my questions. If you think I am bad at this, I know and I am sorry. by the way this is the code from the buttons and panel if that helps. Thanks everyone.
Sub Panelswitch(Panel As Form)
PanelControl.Controls.Clear()
Panel.TopLevel = False
PanelControl.Controls.Add(Panel)
Panel.Show()
End Sub
Private Sub Btntest_Click(sender As Object, e As EventArgs) Handles Btntest.Click
Panelswitch(Driverlist)
End Sub
Private Sub Btntest2_Click(sender As Object, e As EventArgs) Handles btntest2.Click
Panelswitch(DriverLicense)
End Sub
There are number of things I would avoid, just looking at your pictures, but that goes beyond your question.
Secondly, agreed with the comment, you should revise your question further (I see you tried, but still, it's neither complete or clear).
If I understand correctly, you have container PanelControl, which you fill either with a form DriverList or DriverLicence. I just want to make sure - are you sure you have your instances corectly set? Because I would expect that the switch would act only for displaying correct form, but should behave differently for the [persistent] list DriverList and DriverLicence, which should be probably a new instance every time you want to add a licence. But this code is not shown, just guessing the problem might be along those lines. There are quite few design possibilities, I guess.
Simplified, the point is:
Private Sub Btntest2_Click(sender As Object, e As EventArgs) Handles btntest2.Click
Dim DriverLicenceInstance as new DriverLicence
Panelswitch(DriverLicenseInstance)
End Sub
I also think, that you might have your binding set incorrectly for the 2nd form, the DriverLicence. I'm not proficient in .NET binding, but it seems to me that you declare the bindings as two separate things (comes from the copying the code?). But what you probably need to do is to use the same binding source for both parts. See here: A-Detailed-Data-Binding-Tutorial.

VB.net PrintForm Not Working in New Thread

I am developing a e-filing app and I need to print an adhesive label with some info to attach to the physical folder.
I already designed the label as a Form put the logo and everything that I need there. Then on the Form.Shown event I put the command to print:
Me.PrintLabelForm.Print() (This is VisualStudio PowerPack Control)
And here is where I bump into a problem. The print out is totally empty (I already changed margins setup the printer, etc). The issue is that the form is not actually fully loaded, I switch the method to the print preview and the controls are there but they are empty.
I tried several approaches but I have been not able to do this automatically. One solution that I found was to have a button to do the Me.PrintLabelForm.Print() then it works because the form is already fully loaded and displayed but this is not an option. I need the form to open automatically, print and close.
An option that I think it should work will be to have a new thread with a timer then printing so I did this:
Private Sub LabelPrint_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub LabelPrint_Shown(sender As Object, e As EventArgs) Handles Me.Shown
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Left = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Right = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Top = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Bottom = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Landscape = True
Dim PrintThread As New System.Threading.Thread(AddressOf PrintSub)
PrintThread.Start()
End Sub
Private Sub PrintSub()
Threading.Thread.Sleep(1000)
Me.PrintLabelForm.Print()
Me.Close()
End Sub
The idea was to have the PrintSub to give the app enough time to finish to render the whole thing then print but I am getting this error:
**An unhandled exception of type 'System.Exception' occurred in Microsoft.VisualBasic.PowerPacks.dll
Additional information: The window being printed must be visible and contain focus.**
So I wonder how to make this thread have the window form in focus in order to be able to print.
That is all. Thanks for all the help.
Always work with the form only from main thread.
You found it right – form printing will not run from new thread.
When you do any actions on forms, you must perform all the work from Dispatcher thread. It is the thread on which all event methods run. If you fail doing so, you can encounter many side effects. (Not only problem with printing. I've been there and this advice from senior Windows programmer helped me to get things back to normal.) So do not use form printing from any other thread.
If you want a workaround for this, print form to the image (in main thread) and then you can print the image using new thread.
This has nothing to do with .NET, this is related to internals of Windows Forms technology. Welcome to Windows programming.
I manage to solve it putting this line in the Form.Shown
PrintLabelForm.Print(Me, PrintForm.PrintOption.ClientAreaOnly)
I don't know why or how but it works.
Thanks to all of you guys for your help. Let's hope I don't find myself trying to do stuff when the form is fully displayed.
This is my full code let's hope it works for someone else:
Imports Microsoft.VisualBasic.PowerPacks.Printing
Public Class PrintAdhesiveLabel
Private Sub LabelPrint_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub LabelPrint_Shown(sender As Object, e As EventArgs) Handles Me.Shown
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Left = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Right = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Top = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Margins.Bottom = 0.1
PrintLabelForm.PrinterSettings.DefaultPageSettings.Landscape = True
PrintLabelForm.Print(Me, PrintForm.PrintOption.ClientAreaOnly)
Me.Close()
End Sub
End Class
Perhaps this is relevant:
Only the form that currently has focus can be printed by using this
method. If you have set the Form property to another form before
calling this method, the image of the form may not be rendered as
expected. To avoid this, call the Focus method of the form before you
call Print.
So call Me.PrintLabelForm.Focus() before calling Me.PrintLabelForm.Print():
Private Sub PrintSub()
Threading.Thread.Sleep(1000)
Me.PrintLabelForm.Focus()
Me.PrintLabelForm.Print()
Me.Close()
End Sub

Choosing between two sets of tootips

Working with the vbnet form editor, I have instancied 2 times the tooltip class: one for English Langage, one for French langage. (you can also imagine: short version /extended version)
How to choose dynamically between the to instancies at runtime?
I found this way to handle the topic:
Private Sub Tip_En_CheckedChanged(sender As Object, e As EventArgs) Handles Tip_En.CheckedChanged
If Tip_En.Enabled Then
Me.ToolTip_Fr.Active = False
Me.ToolTip_en.Active = True
End If
ToolTip_en.IsBalloon = True
End Sub
Private Sub Tip_Fr_CheckedChanged(sender As Object, e As EventArgs) Handles Tip_Fr.CheckedChanged
If Tip_Fr.Enabled Then
Me.ToolTip_en.Active = False
Me.ToolTip_Fr.Active = True
End If
End Sub
Could surely be shortened and factorized...
You can use 1 instance, only changing the text displayed at runtime.
You can use an extra class like ToolTipTranslationTexts that holds all the text translations after setting a language this class knows what language you have set.
Put a method like PopulateToolTipText into it, from which you change all tooltip texts to the right language.
This is one approach I could think off, but there are many more.
That way you can also add more languages only by adding text to this class.

Checkbox events when Form is opened and closed fire when form is reloaded

First... I am open to skinning this cat a different way if I am going at it wrong to begin with. Using VB 2010 .net 4.0 and I am very much a beginner.
I am making a product billing application that has a main form and a subform with additional options. Whenever that subform is reopened after being opened once, the checkbox events that were selected are blank by default. If I recheck them (so someone can uncheck) then any that are rechecked all refire and increase the variable again.
I ultimately need to be able to open that second form after closing it, display any checkboxes that were selected before as selected again and not increase the variable in the process.
Main form Checkbox code to set booleans and increase or decrease subtotal variable of most used products.
Private Sub chkbox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbox1.CheckedChanged
If chkbox1.Checked = True Then
bChkbox1 = True
Subtotal += 15
Else
bChkbox1 = False
Subtotal -= 15
End If
End Sub
Main form button to launch subform with all products listed.
Private Sub btnAllProducts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAllProducts.Click
Form3.Show()
End Sub
Subform checkbox code works perfectly the first time it is opened but not when relaunched.
Private Sub chkbox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbox2.CheckedChanged
If chkbox2.Checked = True Then 'also tried without the nested if with same results
If Me.IsHandleCreated = True Then 'me.visible behaves the same way
MsgBox("form visible true")'launches after clicking button but before form is actually on screen
Form1.bcheckbox2 = True
Form1.Subtotal += 105
End If
Else
Form1.bcheckbox2 = False
Form1.Subtotal -= 105
End If
End Sub
Booleans are used to check boxes that were checked on the main page or when it was open before.
If Form1.bcheckbox2 = True Then
chkbox2.Checked = True
End If
As I said, I can completely rework the code if it makes sense to do so or just fix something if I have made some sort of mistake.
For example, I was thinking of changing to wipe the subtotal on each form load and rebuild it based off the toggled booleans but it seems like there should be a much more elegant way with less overhead and I am just doing something incorrectly.
It is not common to have to tell checks and radios to ignore events while loading the form. You just need an Ignore or Loaded flag:
Public Class Form1
Private ignore As Boolean = True
...
Private Sub Form1_Load(...
' do normal stuff
ignore = False ' should be the ONLY place it is set
End Sub
Private Sub CheckBox2_CheckedChanged(...
If ignore Then Exit Sub
End Sub
The Form Designer code will fire events as it creates the form and controls, which CAN be handy for initializing stuff but often it causes trouble. Some controls will even get the same event twice. There isnt really a "reload" action for forms. If you hide them, Show() won't fire the Load event again.
You can avoid the flag and manually add the handlers for the troublesome controls when the form loads, but that can be tedious if there are lots of them. Flags can be abused and misused, but if it is set in that one spot only, its fine.
If someone is looking for alternative or have similar problem here's my workaround to detect event change so checkbox wouldn't get triggered on re-load:
If ((Me.CheckBox2.Value <> Sheets(1).Range("t6").Value) And (Me.CheckBox2 = True)) = True Then
' do your stuff
Me.CheckBox2.Value = False
Else
Me.CheckBox2.Value = True
End If
Where Sheets(1).Range("t6").Value is where checkbox2 value is being stored.
I have this assigned to a msgbox input so when vbno event is being triggered else is executed.
Cheers.

Prevent control disabling in vb.net

is there a way to make a control always enabled, so that if you set Enabled = False it has no effect?
I tried to add a handler to the EnabledChange event in this way:
AddHandler mybutton.EnabledChange, Sub()
mybutton.EnabledChange = True
End Sub
but it causes a stackoverflow exception in a lot of situations, for example when you try to disable the control that contains the button. So is there another way to do this?
Try change like this .. it's worked for me
Private Sub myButton_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles myButton.EnabledChanged
If Not myButton.Enabled Then myButton.Enabled = True
End Sub
No
Not within any realm of good coding practice
if you change it to enabled = false, it changes the property setting with that event.
What is the code issue, there will be a solution?
In the properties of the control, enabled =true. You don't need to write code for it. It's in the form design.
In response to your comment:
You can use the patch that matzone has selected.. personally, I am in favor of removing that particular control from the loop, and I've had issues with nested and endless loops in vb.net...I just prefer to have less unnecessary changes, within my program, just helps keep out the bugs. It means you've got to go through and ensure the code re-enabling the control is always called, whenever it is changed in the loop.. asking for errors if you ask me.. It may be more work in the short run, but would be better to select the control out in the first place. I've faced similar quandaries.
See link to MSDN
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.enabled.aspx
This worked for me
Protected Overridable Sub cmdCancel_EnabledChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.EnabledChanged
If DirectCast(eventSender, TPDotnet.GUIControls.WinButtonEx).Enabled = False Then
cmdCancel.Enabled = True
End If
End Sub
The condition is handled correctly.