Image display with details in textbox on another form - vb.net

Why my code doesn't work, In form4 you can upload a pic then there's a textbox there where you can type the details of the pic then if you click submit button, Form5 will show and the picture that you upload in form4 will show in form5 picturebox and the typed details in textbox in form4 will also appear in form5 label1. Can you correct my code (I'm not using database)
When I already upload the pic, typed the details and click submit button it doesn't show on form5
Public Class Form4
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim file As New OpenFileDialog
file.Filter = "Image files | *.png; *.jpg; *.gif |" & _
"All files (*.*) | *.*"
file.FilterIndex = 1
If file.ShowDialog = Windows.Forms.DialogResult.OK Then
PictureBox1.ImageLocation = file.FileName
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Me.Hide()
Form3.Show()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.PictureBox1.Image = Form5.PictureBox1.Image
Me.TextBox1.Text = Form5.Label1.Text
MsgBox("Ad Posted!", MsgBoxStyle.Information)
Me.Hide()
Form5.Show()
TextBox1.Clear()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim lbutton As DialogResult
lbutton = MessageBox.Show("Do you want to logout now?", "Logging out", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If lbutton = DialogResult.Yes Then
Form1.Show()
Me.Hide()
End If
End Sub
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SetStyle(ControlStyles.SupportsTransparentBackColor, True)
End Sub
End Class

Related

Why openFileDialog doesn't responding when i dispose the form and back to it again?

I have tow openFileDialog tools in one form, when i browse openFileDialog1 and choose a picture, it's ok but when i dispose this form and back to it and click to browse another picture, it doesn't responding and the project stopped with no errors!!!
Private Sub Browse_Click(sender As Object, e As EventArgs) Handles Browse.Click
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Image Files (*.jpg, *.bmp, *.gif, *.png)|*.jpg; *.bmp; *.gif; *.png"
If OpenFileDialog1.FileName = "" Then
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
txtFileName.Text = OpenFileDialog1.FileName
txtFileName.SelectionStart = txtFileName.Text.Length 'to show the last portion of text
If Trim(txtFileName.Text) <> "" Then picSave.Image = Image.FromFile(txtFileName.Text)
End If
Else
Exit Sub
End If
End Sub
Private Sub Btnclose_Click(sender As Object, e As EventArgs) Handles Btnclose.Click
Me.Dispose()
End Sub
In Project Properties be sure you have set Shutdown Mode to "When last form closes." This is NOT the default.
In Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
OpenFileDialog1.Filter = "Image Files (*.jpg, *.bmp, *.gif, *.png)|*.jpg; *.bmp; *.gif; *.png"
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
TextBox1.Text = OpenFileDialog1.FileName
PictureBox1.Image = Image.FromFile(TextBox1.Text)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
Close()
End Sub
And then in Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.Show()
Close()
End Sub
When I return to Form1 the OpenFileDialog behaves normally.

Close Form1 If FileExists+ Open Form2

I try to close or hide the Form cnx if a file exist and open the Form Product.
But Something is wrong and i don't understand why this dont work.
Private Sub cnx_Load(sender As Object, e As EventArgs) Handles MyBase.Load
strFileName = "app.txt"
strBasePath = Application.StartupPath
If My.Computer.FileSystem.FileExists(strFileName) = True Then
Product.Show()
Me.Hide()
ElseIf My.Computer.FileSystem.FileExists(strFileName) = False Then
MessageBox.Show("File App.config is Missing! Create a new Database.",
"Something is Wrong!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
Thanks.
You could do something like this, although probably not optimal.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
hideForm(Me)
Form2.Show()
End Sub
Private Sub hideForm(form As Form)
form.Opacity = 0.0F
form.ShowInTaskbar = False
End Sub
Also remember to add under Form2 or your program will stay open after closing Form2.
Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Form1.Close()
End Sub

Button.Click event doesn't fire after TextBox.Leave event

I use the below code to verify if the text into a TextBox was changed and ask for saving changes:
Private TBoxTxtBefore As String = String.Empty
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
Try
TBoxTxtBefore = CType(sender, TextBox).Text
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Try
If CType(sender, TextBox).Text <> TBoxTxtBefore Then
Dim SN As MsgBoxResult = MsgBox("Save changes?", vbYesNo, "Save Changes")
If SN = vbYes Then Btn_SaveChanges.PerformClick()
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
But when I click a button (for example button1) while the cursor is inside TextBox1, only TextBox1.Leave event was raised.
How can I have Button?.click event raise after TextBox1.Leave event?
How can I have button1.click event raise after TextBox1.Leave event?
If TextBox1 has the focus and you click a button the leave event will fire before the click event. To test click in Texbox1, then click Button1.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Debug.WriteLine("B1C")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Debug.WriteLine("B2C")
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
Debug.WriteLine("TBL")
Button2.PerformClick()
End Sub
Try to clarify your question please.
edit: This recreates the problem I think you are having,
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Debug.WriteLine("B1C")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Debug.WriteLine("B2C")
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
MessageBox.Show("FOO")
Debug.WriteLine("TBL")
Button2.PerformClick()
End Sub
Did some searching and found that the Message box causes pending focus events to be lost.
Thanks to dbasnett's hints I was able to get the name of the control that fired the Leave event, so to call it (if it's a button) after the Message Box was showed:
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Try
Dim FiringCtrl As Control = Me.Activecontrol
If CType(sender, TextBox).Text <> TBoxTxtBefore _
AndAlso FiringCtrl.Name <> "Btn_SaveChanges" Then
Dim SN As MsgBoxResult = MsgBox("Save changes?", vbYesNo, "Save Changes")
If SN = vbYes Then Btn_SaveChanges.PerformClick()
If TypeOf FiringCtrl Is Button Then
DirectCast(FiringCtrl, Button).PerformClick()
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Anyway Plutonix showed me a better solution here
You can call multiple events one after the another. Your TextBox1_Leave event could trigger Button1_click event.
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
'Take the control to another sub. Pass the sender control and the event details as arguments
Call Button1_Click(sender,e)
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'Do your stuff here
End Sub
Hope this helps.

Buttonclick is not working VB.NET

Can you help me with this, my code doesn't have error but when I click the button to go back to the previous group box it doesn't work :( Idk why can you help me guys figure it out?
The app has login form when you login it directs you to the another form that you can choose categories then if I select a category example I chose gadgets it redirects me to the gadgets groupbox then if I try to go back it doesn't go back in the categories page
The button2.click, button6.click and button3.click are the back buttons
Here's my code
Public Class Form3
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
GroupBox2.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sbutton As DialogResult
sbutton = MessageBox.Show("Do you want to logout now?", "Logging out", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If sbutton = DialogResult.Yes Then
Form1.Show()
Me.Hide()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
GroupBox1.Show()
End Sub
Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
GroupBox3.Show()
End Sub
Private Sub LinkLabel2_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
GroupBox4.Show()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
MsgBox("Message was sent!", MsgBoxStyle.Information)
TextBox1.Clear()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
GroupBox2.Show()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
MsgBox("Message was sent!", MsgBoxStyle.Information)
TextBox2.Clear()
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
GroupBox2.Show()
GroupBox3.Hide()
End Sub
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
GroupBox2.Hide()
GroupBox3.Hide()
GroupBox4.Hide()
End Sub
End Class
Umm did you type this yourself or generate it from a UI? Shouldn't it be button_OnClick not button_click?

Button Click Event Within Another

I have two buttons Start button and Stop button .I run my program by clicking on start button. I want to stop the program during the start button . But the program will not responde until the start buttun finish its job. How i can do check the stop buttun during the start. i heard about threading but i do not know how i can do it.
Private Sub Button_Start (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
//my code
//check always if the user push stop if no continue if yes go to this sub
//my code
end sub
Private Sub Button_Stop (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
stopClick = True
Dim Response As Integer
Response = MessageBox.Show("Do you really want to exit?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Response = vbYes Then
Me.Close()
End If
End Sub
you can use threading put button1 code in a function and use the thread .
you can refer to this example
'Thread created to handle the Background process for start_function
Dim t As System.Threading.Thread
Private Sub start_function()
While True
'your code here for Eg:
Dim i As Integer
i = i + 1
End While
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
t = New System.Threading.Thread(AddressOf Me.start_function)
t.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
t.Abort()
End Sub
Drag a backgroundworker component onto your form.
Imports System.ComponentModel
Public Class Form1
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Me.Text = "Busy Doing Work"
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
Me.Text = "Asking to Cancel"
BackgroundWorker1.CancelAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
While Not BackgroundWorker1.CancellationPending
System.Threading.Thread.Sleep(1000)
End While
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Me.Text = "Cancelled"
End Sub
End Class