Run VB.net app on second monitor - vb.net

I'm a newbie at coding but I have done simple application for the plan I work for!!
I recently encounter a "little" issue with one of my application!
I build a Windows Form application, with multiple form, that run on a dual screen CNC. The CNC program have to run on the primary screen at a higher resolution. My application run on the second screen who is a touch screen at 1024;768.
The problem itself is if I run the code with the debug data everything run as I want, open the application on second screen and all the next form open on this screen. If I install it with the released data, all form open on the primary monitor, even if I drag them on second screen. After I closed them, they return on the primary.
Is there a way I can put a code line at the beginning of each form who make all form open on the secondary screen.
When the setup will work correctly I'll lock the screen setup just to be sure nobody will mess with the settings.
Please be gentle with me I don't have any formation on how to code. I learn from myself with reading on the web!!
Thanks all!!

Something like this should position the window at offset 100, 100 on the first non-primary screen found. You can adjust the location and/or size to suit your needs.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim secondaryMonitor = Screen.AllScreens.FirstOrDefault(Function(x) Not x.Primary)
If secondaryMonitor IsNot Nothing Then
Dim newLocation = secondaryMonitor.Bounds.Location
newLocation.Offset(100, 100) ' adjust as needed
Me.Location = newLocation
' Also see Me.Size and Me.Bounds
End If
End Sub

This worked for me:
Private Sub _MainForm_Move(sender As Object, e As EventArgs) Handles MyBase.Move
' preserve me.Location.x and me.Location.y here.
End Sub
Then, at program boot, restore: me.Location.x and me.Location.y

Related

How to make a picture box appear randomly on the screen

I am coding a zombie game in vb.net and need to make the zombies(which I have put in picture boxes and an array, there are 13) appear randomly, maybe two each time 1 zombie is killed. How can I make this in code ? I am new to coding and cannot figure it out even after numerous searching.
I think I understand what you're trying to do.
You'll want to construct a picture box in code then define where you want it to "spawn" on your form.
You can start with something like this, if you're going to have more than one zombie at a time you'll want to either make a list of them or name them uniquely so you can reference them later on (moving them/despawning/etc)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim zombie As New PictureBox
With zombie
zombie.Width = 100 'or the size you need
zombie.Height = 100 ' same thing
zombie.Top = 20 'or where you need it could be a random
zombie.Left = 20 'same as top
zombie.ImageLocation = "C:\mydocuments\zombie.png" 'change this to the location of your zombie image. if you're storing it in a resource you can call it here
End With
Me.Controls.Add(zombie)
End Sub
This should get you started at least
edit: I missed the part where you said you have an array for your zombies, but you can do an array of your picture boxes as well

How to cause a running VB program to start from scratch again

How can I restart a running VB program from within?
I have written a VB program that solves SUDOKU puzzles.
When it completes the solution, it asks if the user wants to run again with another puzzle.
I can not:
-determine the first line of code that the program executes. The main() is empty. All code is on the tab for Form1. I have tried breakpoints, but still can't determine the starting point.
-figure out how to cause the program to jump back to the starting point in the code.
Can you help me?
I've tried placing breakpoints in the code at places that might be close to the starting point, but so far that hasn't worked. When the program starts, Form1 is displayed, and it is waiting to see if a check box gets activated, or if text is entered anywhere in many text boxes.
You should do what Alejandro said in his comment: "reset the data state to the starting point".
But if you must, you can restart a WinForms application with Shell(Application.ExecutablePath)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If MessageBox.Show("Start from scratch?", "Confirm Restart", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then restart()
End Sub
Private Sub restart()
Shell(Application.ExecutablePath, AppWinStyle.NormalFocus, False)
End
End Sub

How to get more than one parent control in form

Newbie here :)
In order to achieve a transparency-type effect for pictureboxcharacter1 on the background, I have set pictureboxbackground1 as its parent.
this works fine.
For the second picturebox (on the same form) I tried the same thing and set pictureboxbackground2 as its parent so it would look transparent over pictureboxbackground2. However when I debug the application pictureboxcharacter2 disappears and only pictureboxbackground2 is visible.
the code I have is:
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.pictureboxcharacter1.Parent = Me.pictureboxbackground1
Me.pictureboxcharacter2.Parent = Me.pictureboxbackground2
End Sub
Really strange: if I put pictureboxcharacter2 on pictureboxbackground1 in the designer tab, while debugging it shows up on picturebackground2 and transparent (like how I wanted it to be)??
Does anyone know what's going on at all?
Please I'm NEED any help I could get
Is pictureboxcharacter1 and pictureboxcharacter2 one over other ? If so try making the one be other's parent.
Have a play with bring to front option and see if that works.

What would be a better way to make a calendar than using labels?

I've created my own winforms month-view calendar in VB.net.
To do it I've used a table layout panel, with 42 separate cells. In each of the cells is a label called lblDay1, lblDay2, etc.
When I load the page, the labels are all written to with the correct numbers for that month.
Dim daysInMonthCnt As Integer =31 'Assume 31 days for now
Dim firstDay As Integer = Weekday("1/" & Now.month & "/" & Now.year) 'Get weekday for 1st of month
For dayCount As Integer = firstDay To daysInMonthCnt
Dim lbl As Label
lbl = CType(pnlMonthBody.Controls("lblDay" & dayCount), Label)
lbl.Text = dayCount 'Write to label
Next dayCount
Unfortunately, that turns out to be incredibly slow to load. Can anyone please suggest a faster method.
Just writing values to a so small number of labels is a really fast process. The problems you are experiencing have to do most likely with the VB.NET "problems" while refreshing the contents of GUI controls; the best way to fix this is looking into multithreading, as suggested by FraserOfSmeg.
As far as I think that this is a pretty simplistic GUI with a low number of controls and a not too demanding algorithm (big amount of/long loops is the prime cause of GUI-refreshing problems), you might get an acceptable performance even without relying on multithreading. In your situation, I would do the following:
A container (the TableLayoutPanel you are using or something
simpler, like a Panel) including all the labels at the start. In
case of not getting too messy (what does not seem to be the case,
with just 42 labels) I would include them in the "design view"
(rather than at run time).
A function populating all the labels depending upon the given month.
A "transition effect" for
the container called every time the user selects a different month.
You can accomplish this quite easily with a Timer relocating the
container (e.g., when the button is clicked the container's position
is set outside the form and then comes back gradually (20 points per
10ms -> made-up numbers) until being back to its original position).
Synchronising the two points above: the values of the labels
will start changing when the transition starts; in this way the user
will not notice anything (just a nice-appealing transition
month to month).
This GUI should deliver the kind of performance you are after. If not, you should improve its performance by relying on additional means (e.g., the proposed multi-threading).
SAMPLE CODE TO ILLUSTRATE POINT 3
Add a panel (Panel1), a button (Button1) and a timer (Timer1) to a new form and the code below.
Public Class Form1
Dim curX, origX, timerInterval, XIncrease As Integer
Dim moving As Boolean
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If (curX >= origX) Then
If (moving) Then
curX = Panel1.Location.X
moving = False
Timer1.Stop()
Else
curX = 0 'getting it out of the screen
moving = True
End If
Else
curX = curX + XIncrease
End If
Panel1.Location = New Point(curX, Panel1.Location.Y)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
XIncrease = 100
timerInterval = 100
Panel1.BackColor = Color.Maroon
origX = Panel1.Location.X
curX = origX
With Timer1
.Enabled = False
.Interval = timerInterval
End With
End Sub
End Class
This is very simplistic, but shows the idea clearly: when you click the button the panel moves in X; by affecting the timerInterval and XIncrease values you can get a nice-looking transition (there are lots of options, bear in mind that if you set curX to minus the width of the panel, rather than to zero, it goes completely outside the form).
If you're purely interested in speeding up your code I'd suggest running the loading code on multiple threads simultaneously. This may be overkill depending on your application needs but it's a good way to code. As a side note so the program looks a little more slick for the end user I'd suggest always running time consuming processes such as this on a separate thread(s).
For info on multithreading have a look at this page:Mutlithreading tutorial

Dragging and Dropping in VB.NET

I am preparing a chess program in VB.NET. So what I want to create is a drag and drop event. In drag and drop events, the original image is kept intact and the copy is placed wherever you want to place it.
But what I want to do is, I want to remove the original as soon as the image is being picked. Any idea how can I do that?
My user interface consists of 64 picture boxes arranged in rows of 8. And they all have images of their respective pieces on them.
Please help me.
#Hans is correct; it would be much easier to do this as one PictureBox. If, however, you are stuck on the method you are currently using, change the code in your MouseMove function on the source PictureBox to look like this. It basically copies the image to a variable, and then sets the source image to Nothing. Of course, you will have to handle if the move is not made (setting the source image back to the value of nImage) as well as dealing with disposing of the variable once the move is made.
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If m_MouseIsDown Then
' Initiate dragging and allow either copy or move.
Dim iImage As Image
iImage = PictureBox1.Image
PictureBox1.Image = Nothing
PictureBox1.DoDragDrop(iImage, DragDropEffects.Copy Or _
DragDropEffects.Move)
End If
m_MouseIsDown = False
End Sub