VB.Net Cannot grab the values on the first form - vb.net

I would just like to ask for your help regarding my project as you can see it has a caller instance the other form also has it and it is working (without mathematical operation) but on this form which is form3 which I need a +1 on frm4.label it does not work. (I named it Public Sub New1) Error 1 Too many arguments to 'Public Sub New()(Note: i have another callerinstance on the previous form which is form2 which is named Public SubNew()) and when it run instead of getting the right results the label always shows the text of 0
Please help me thanks in advance happy coding to all :)
Public Class Form3
Private frm3 As Form3
Private frm1 As Form1
Private frm4 As Form4
Public Sub New1(ByVal Name As String, ByVal Bread As String, ByVal Cheese As String, ByVal Condiments As String, ByVal Meat As String, ByVal Cost As Decimal, ByVal Quantity As Decimal, ByVal callerInstance As Form1)
' Call required if you add your constructor manually
InitializeComponent()
' save the instance of the Me variable passed to this constructor
frm1 = callerInstance
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frm1 = New Form1(Me)
frm4 = New Form4()
If frm4 Is Nothing Then
frm4 = New Form4
AddHandler frm4.FormClosed, AddressOf Me.Form4HasBeenClosed
Dim i As Integer = 0
i = frm4.Label3.Text
If TextBox1.Text = frm1.TextBox2.Text Then
i = +1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
ElseIf TextBox2.Text = frm1.TextBox4.Text Then
i = +1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
ElseIf TextBox3.Text = frm1.TextBox6.Text Then
i = +1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
ElseIf TextBox4.Text = frm1.TextBox8.Text Then
i = +1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
ElseIf TextBox5.Text = frm1.TextBox10.Text Then
i = +1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
ElseIf TextBox6.Text = frm1.TextBox12.Text Then
i = +1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
ElseIf TextBox7.Text = frm1.TextBox14.Text Then
i = +1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
ElseIf TextBox8.Text = frm1.TextBox16.Text Then
i = +1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
ElseIf TextBox9.Text = frm1.TextBox18.Text Then
i = +1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
ElseIf TextBox10.Text = frm1.TextBox20.Text Then
i = +1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
frm4.Label3.Text = i.ToString()
frm4.Show()
Else
i += 0
frm4.Show()
End If
End If
If frm4 IsNot Nothing Then
frm4.Show(Me) 'Show Second Form
Me.Hide()
End If
End Sub
Sub Form4HasBeenClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
frm4 = Nothing
End Sub
End Class

Based on the comments it sounds like the error is on this line
frm1 = new Form1(Me)
If that is the case it means that there is no Sub New method on Form1 which accepts a parameter of type Form3. To fix this you need to add one
Public Class Form1
...
Public Sub New(ByVal frm3 As Form3)
End Sub
End Class

Related

Move to next record based on specific value in textbox

I am in need of some help. I want to be able to go through my records using the next and previous buttons on my form using a dynamic
values.
The code I currently have does moves to next record but from the starting value, however I want to be able to start from a specific value for example, if I type 100
into textbox and click next it should display records for 101, 102 and so forth with each click.
How do I accomplish this. Many thanks
This is the current code i have at the moment.
Dim intcurrentindex As Integer = 0
If intcurrentindex < ds.Tables(0).Rows.Count - 1 Then
intcurrentindex = intcurrentindex - 1
TextBox1.Text = ds.Tables(0).Rows(intcurrentindex).Item("NAME").ToString
TextBox2.Text = ds.Tables(0).Rows(intcurrentindex).Item("CODE").ToString
TextBox3.Text = ds2.Tables(0).Rows(intcurrentindex).Item("STOCKNAME").ToString
TextBox4.Text = ds.Tables(0).Rows(intcurrentindex).Item("WEIGHT").ToString
TextBox5.Text = ds.Tables(0).Rows(intcurrentindex).Item("LOCATION").ToString
End If
maybe something like this:
Public Class Form1
Private intcurrentindex As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TxtCurrentIndex.Text = 0
End Sub
Private Sub BtnNext_Click(sender As Object, e As EventArgs) Handles BtnNext.Click
If TxtCurrentIndex.Text = "" Then
intcurrentindex = 0
Else
'set intcurrentindex to textbox value
intcurrentindex = TxtCurrentIndex.text
End If
If intcurrentindex < ds.Tables(0).Rows.Count - 1 Then
TextBox1.Text = ds.Tables(0).Rows(intcurrentindex).Item("NAME").ToString
TextBox2.Text = ds.Tables(0).Rows(intcurrentindex).Item("CODE").ToString
TextBox3.Text = ds2.Tables(0).Rows(intcurrentindex).Item("STOCKNAME").ToString
TextBox4.Text = ds.Tables(0).Rows(intcurrentindex).Item("WEIGHT").ToString
TextBox5.Text = ds.Tables(0).Rows(intcurrentindex).Item("LOCATION").ToString
intcurrentindex = intcurrentindex + 1
End If
End Sub
Private Sub BtnPrevius_Click(sender As Object, e As EventArgs) Handles BtnPrevius.Click
If TxtCurrentIndex.Text = "" Then
intcurrentindex = 0
Else
'set intcurrentindex to textbox value
intcurrentindex = TxtCurrentIndex.text
End If
If intcurrentindex > 0 Then
TextBox1.Text = ds.Tables(0).Rows(intcurrentindex).Item("NAME").ToString
TextBox2.Text = ds.Tables(0).Rows(intcurrentindex).Item("CODE").ToString
TextBox3.Text = ds2.Tables(0).Rows(intcurrentindex).Item("STOCKNAME").ToString
TextBox4.Text = ds.Tables(0).Rows(intcurrentindex).Item("WEIGHT").ToString
TextBox5.Text = ds.Tables(0).Rows(intcurrentindex).Item("LOCATION").ToString
intcurrentindex = intcurrentindex - 1
End If
End Sub
End Class
you need a variable to store current index
Thank you guys for your help on this. I used binding source in the end to get it to work.

Reading lines from a text file in VB

I'm creating a Quiz Application in VB and the quiz app reads the questions from a text file which is already created and it has got some questions in it.
1
Q.Who won the WorldCup last time?
I Don't know
May be he knows
Who knows
Who cares?
2
Question 2
Answer A
Answer B
Answer C
Answer D
3
Question 3
Ans 1
Ans 2
Ans 3
Ans 4
The first line is the question number,the second line is the question,lines 3 - 6 represents the answer choices.Now I have created a Form for quiz and when the next button is pressed it should display the next question i.e,After the first question it should go to Question 2 and print accordingly.But unfortunately i'm unable to calculate the logic to go to next question.
Public Class Quiz
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
End Function
Dim SCORE As Integer = 0
Dim val As Integer = 30
Dim QUES As Integer = 0
Dim Line As Integer = 1
Dim allLines As List(Of String) = New List(Of String)
Dim TextFilePath As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Quiz.txt")
Private Sub Quiz_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = 30
Timer1.Enabled = True
Next_Prev_Ques(Line + QUES)
End Sub
Public Function Next_Prev_Ques(quesno As Integer) As Integer
Line = quesno
Using file As New System.IO.StreamReader(TextFilePath)
Do While Not file.EndOfStream
allLines.Add(file.ReadLine())
Loop
End Using
QUES = ReadLine(Line, allLines)
Label1.Text = ReadLine(Line + 1, allLines)
RadioButton1.Text = ReadLine(Line + 2, allLines)
RadioButton2.Text = ReadLine(Line + 3, allLines)
RadioButton3.Text = ReadLine(Line + 4, allLines)
RadioButton4.Text = ReadLine(Line + 5, allLines)
Return Line
End Function
Public Function ReadLine(lineNumber As Integer, lines As List(Of String)) As String
Return lines(lineNumber - 1)
End Function
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Value += 1
val -= 1
Label2.Text = val & " Sec"
If ProgressBar1.Value = ProgressBar1.Maximum Then
Timer1.Enabled = False
End If
If ProgressBar1.Value > 25 Then
SendMessage(ProgressBar1.Handle, 1040, 2, 0)
End If
End Sub
Private Sub Quiz_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Form1.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox(Line + QUES + 5)
Next_Prev_Ques(Line + QUES + 4)
Me.Refresh()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Next_Prev_Ques(Line + QUES + 5)
End Sub
The function Next_Prev_Ques Should run accordingly but its not.Can anyone post the correct code?
Below is some code that uses serialization to get the same results. You can create a class called Questions and properties on it like questionnumber, question and answer, store the data into a xml file and retrieve them with string methods. Check the code below:
The code for the class
Public Class clsQuestions
Private _Number As String
Private _Question As String
Private _Answer As String
Public Property Number() As String
Get
Number = _Number
End Get
Set(ByVal Value As String)
_Number = Value
End Set
End Property
Public Property Question() As String
Get
Question = _Question
End Get
Set(ByVal Value As String)
_Question = Value
End Set
End Property
Public Property Answer() As String
Get
Answer = _Answer
End Get
Set(ByVal Value As String)
_Answer = Value
End Set
End Property
End Class
The code for the form
Imports System.IO
Imports System.Xml.Serialization
Public Class Form1
Dim numQuestions() As String
Dim questions() As String
Dim answersGroup() As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = ""
Label2.Text = ""
Label3.Text = ""
Main()
End Sub
Private Sub Main()
Dim p As New clsQuestions
p.Number = "1,2,3,4,5,6,7,8,9,10"
p.Question = "Question 1,Question 2,Question 3," &
"Question 4,Question 5,Question 6,Question 7," &
"Question 8,Question 9,Question 10"
p.Answer = "Answer 1.1,Answer 1.2,Answer 1.3,Answer 1.4;" &
"Answer 2.1,Answer 2.2,Answer 2.3,Answer 2.4;" &
"Answer 3.1,Answer 3.2,Answer 3.3,Answer 3.4;" &
"Answer 4.1,Answer 4.2,Answer 4.3,Answer 4.4;" &
"Answer 5.1,Answer 5.2,Answer 5.3,Answer 5.4;" &
"Answer 6.1,Answer 6.2,Answer 6.3,Answer 6.4;" &
"Answer 7.1,Answer 7.2,Answer 7.3,Answer 7.4;" &
"Answer 8.1,Answer 8.2,Answer 8.3,Answer 8.4;" &
"Answer 9.1,Answer 9.2,Answer 9.3,Answer 9.4;" &
"Answer 10.1,Answer 10.2,Answer 10.3,Answer 10.4"
'Serialize object to a text file.
Dim objStreamWriter As New StreamWriter("C:\Users\Username\Documents\Questions.xml")
Dim x As New XmlSerializer(p.GetType)
x.Serialize(objStreamWriter, p)
objStreamWriter.Close()
'Deserialize text file to a new object.
Dim objStreamReader As New StreamReader("C:\Users\Username\Documents\Questions.xml")
Dim p2 As New clsQuestions
p2 = x.Deserialize(objStreamReader)
objStreamReader.Close()
numQuestions = p2.Number.Split(",")
questions = p2.Question.Split(",")
answersGroup = p2.Answer.Split(";")
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
Static x As Integer
If x <= questions.Length - 1 Then
Label1.Text = ""
Label2.Text = ""
Label3.Text = ""
arrayIndex(x)
x += 1
End If
End Sub
Private Sub arrayIndex(ByVal num As Integer)
Label1.Text = numQuestions(num)
Label2.Text = questions(num)
Dim answers() As String
answers = answersGroup(num).Split(",")
For Each item As String In answers
Label3.Text &= item & Environment.NewLine
Next
End Sub
End Class

Wrong Result to the last form on label always 0

Hi good day guys i would just like to seek some help regarding my problem here in VB.NET. I have this program that if the text in the textbox is equal to the text in the textbox of the previous form it will add 1 for each correct answer but instead it always shows zero (0) (last form) and not showing the results that I want. Here is my code guys I hope you can help me with this problem thanks in advance.
Imports System.Convert
Imports System.IO
Public Class Form3
Private frm1 As Form1
Private frm4 As Form4
Public Sub New1(ByVal callerInstance As Form1)
InitializeComponent()
' save the instance of the Me variable passed to this constructor
frm1 = callerInstance
End Sub
Public Sub New2(ByVal callerInstance As Form4)
InitializeComponent()
' save the instance of the Me variable passed to this constructor
frm4 = callerInstance
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm1 As Form1 = Form1
Dim frm4 As Form4 = Form4
frm1 = New Form1
frm4 = New Form4
'program execution proper
Dim lbl3 As Integer = CInt(frm4.Label3.Text)
lbl3 = CInt(frm4.Label3.Text)
Dim Label22 As New Label
If frm1.TextBox2.Text = TextBox1.Text Then
lbl3 = CInt(lbl3) + 0 'if(integer.tryparse, lbl3) then lbl3 += 1
Dim Label24 As New Label
If Not frm1.TextBox4.Text = TextBox2.Text Then
lbl3 = CInt(lbl3) + 0
Dim Label26 As New Label
If Not frm1.TextBox6.Text = TextBox3.Text Then
lbl3 = CInt(lbl3) + 0
Dim Label28 As New Label
If Not frm1.TextBox8.Text = TextBox4.Text Then
lbl3 = CInt(lbl3) + 0
Dim Label30 As New Label
If Not frm1.TextBox10.Text = TextBox5.Text Then
lbl3 = CInt(lbl3) + 0
Dim Label32 As New Label
If Not frm1.TextBox12.Text = TextBox6.Text Then
lbl3 = CInt(lbl3) + 0
Dim Label34 As New Label
If Not frm1.TextBox14.Text = TextBox7.Text Then
lbl3 = CInt(lbl3) + 0
Dim Label36 As New Label
If Not frm1.TextBox16.Text = TextBox8.Text Then
lbl3 = CInt(lbl3) + 0
Dim Label38 As New Label
If Not frm1.TextBox18.Text = TextBox9.Text Then
lbl3 = CInt(lbl3) + 0
Dim Label40 As New Label
If Not frm1.TextBox20.Text = TextBox10.Text Then
lbl3 = CInt(lbl3) + 0 'frm4.Label3.Text = (frm4.Label3.Text) + 1
frm4.Show()
Me.Hide()
Else
lbl3 = CInt(lbl3) + 1
frm4.Show()
Me.Hide()
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
If frm4 IsNot Nothing Then
frm4.Visible = False
frm4.Show(Me) 'Show Second Form
Me.Hide()
End If
End Sub
End Class
The only time you add anything to Lbl3 is:
If Not frm1.TextBox20.Text = TextBox10.Text Then
lbl3 = CInt(lbl3) + 0 'frm4.Label3.Text = (frm4.Label3.Text) + 1
frm4.Show()
Me.Hide()
Else
lbl3 = CInt(lbl3) + 1
frm4.Show()
Me.Hide()
End If
With the nesting you have it is only if all the other If statements = True that you even get to this point.
In addition to this on ALL the other if statement your outcome is either lbl3 = CInt(lbl3) + 0 (if this statement and all previous ones were true) or if any of them are false ALL subsequent ones are irrelevant.
If i understand your intention correctly then what you want is instead of:
If frm1.TextBox2.Text = TextBox1.Text Then
lbl3 = CInt(lbl3) + 0 'if(integer.tryparse, lbl3) then lbl3 += 1
Dim Label24 As New Label
If Not frm1.TextBox4.Text = TextBox2.Text Then
lbl3 = CInt(lbl3) + 0
Try:
If frm1.TextBox2.Text = TextBox1.Text Then lbl3 += 1
If Not frm1.TextBox4.Text = TextBox2.Text Then lbl3 += 1
And so on....
Then to display it you need to update the following code:
If Not frm1.TextBox20.Text = TextBox10.Text Then
lbl3 = CInt(lbl3) + 0 'frm4.Label3.Text = (frm4.Label3.Text) + 1
frm4.Label3.Text = CStr(lbl3)
frm4.Show()
Me.Hide()
Else
lbl3 = CInt(lbl3) + 1
frm4.Label3.Text = CStr(lbl3)
frm4.Show()
Me.Hide()
End If
(this is very similar to one of the answers on one of the other questions hence my comment above)
Add setting code.
frm4.Label3.Text = lbl3.ToString()

My If Else Statement VB.Net's weird behavior

I have this program that when I typed the same text on the form and to the other form will add one (+1) to the label of my last form..There were no errors but when I wrote the correct answers on the textboxes it just gave me a number 1 instead of 10. Please help me guys. Thanks in advance :)
here is my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frm4 = New Form4()
frm1 = New Form1()
Dim Textbox1 As New TextBox
Dim Label3 As New Label
If Textbox1.Text = frm1.TextBox2.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox2.Text = frm1.TextBox4.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox3.Text = frm1.TextBox6.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox4.Text = frm1.TextBox8.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox5.Text = frm1.TextBox10.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox6.Text = frm1.TextBox12.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox7.Text = frm1.TextBox14.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox8.Text = frm1.TextBox16.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox9.Text = frm1.TextBox18.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox10.Text = frm1.TextBox20.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
End If
End If
End If
End If
End If
End If
End If
End If
End If
frm4.Show()
Else
frm4.Label3.Text = frm4.Label3.Text
frm4.Show()
End If
End Sub
Form 2 codes:
Public Class Form2
Private frm1 As Form1 ' remove the new here because it creates a new instance of Form1'
Private frm3 As Form3
Public lbl As New Label ' not needed?'
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(lbl)
End Sub
Public Sub New(ByVal callerInstance As Form1)
' Call required if you add your constructor manually
InitializeComponent()
' save the instance of the Me variable passed to this constructor
frm1 = callerInstance
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Dim Label21 As Label = New Label ' this label is not needed'
' Dim Textbox1 As TextBox = New TextBox ' this textbox is not needed'
' Create a new instance of Form3, do not use the automatic Form3 instance
' automatically created by VB.NET
frm3 = New Form3()
' now you are referring to the caller instance of Form1 '
' where there is the textbox filled with your text '
frm3.Label21.Text = frm1.TextBox1.Text
frm3.Label22.Text = frm1.TextBox3.Text
frm3.Label23.Text = frm1.TextBox5.Text
frm3.Label24.Text = frm1.TextBox7.Text
frm3.Label25.Text = frm1.TextBox9.Text
frm3.Label26.Text = frm1.TextBox11.Text
frm3.Label27.Text = frm1.TextBox13.Text
frm3.Label28.Text = frm1.TextBox15.Text
frm3.Label29.Text = frm1.TextBox17.Text
frm3.Label30.Text = frm1.TextBox19.Text
frm3.Show()
End Sub
End Class
Form1 Codes:
Public Class Form1
Private frm2 As Form2
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If frm2 Is Nothing Then
frm2 = New Form2(Me)
AddHandler frm2.FormClosed, AddressOf Me.Form2HasBeenClosed
Dim Label21 As Label = New Label
frm2.Label21.Text = TextBox1.Text
frm2.Label21.ForeColor = Color.Black
Dim Label22 As Label = New Label
frm2.Label22.Text = TextBox2.Text
frm2.Label22.ForeColor = Color.Black
Dim Label23 As Label = New Label
frm2.Label23.Text = TextBox3.Text
frm2.Label23.ForeColor = Color.Black
Dim Label24 As Label = New Label
frm2.Label24.Text = TextBox4.Text
frm2.Label24.ForeColor = Color.Black
Dim Label25 As Label = New Label
frm2.Label25.Text = TextBox5.Text
frm2.Label25.ForeColor = Color.Black
Dim Label26 As Label = New Label
frm2.Label26.Text = TextBox6.Text
frm2.Label26.ForeColor = Color.Black
Dim Label27 As Label = New Label
frm2.Label27.Text = TextBox7.Text
frm2.Label27.ForeColor = Color.Black
Dim Label28 As Label = New Label
frm2.Label28.Text = TextBox8.Text
frm2.Label28.ForeColor = Color.Black
Dim Label29 As Label = New Label
frm2.Label29.Text = TextBox9.Text
frm2.Label29.ForeColor = Color.Black
Dim Label30 As Label = New Label
frm2.Label30.Text = TextBox10.Text
frm2.Label30.ForeColor = Color.Black
Dim Label31 As Label = New Label
frm2.Label31.Text = TextBox11.Text
frm2.Label31.ForeColor = Color.Black
Dim Label32 As Label = New Label
frm2.Label32.Text = TextBox12.Text
frm2.Label32.ForeColor = Color.Black
Dim Label33 As Label = New Label
frm2.Label33.Text = TextBox13.Text
frm2.Label33.ForeColor = Color.Black
Dim Label34 As Label = New Label
frm2.Label34.Text = TextBox14.Text
frm2.Label34.ForeColor = Color.Black
Dim Label35 As Label = New Label
frm2.Label35.Text = TextBox15.Text
frm2.Label35.ForeColor = Color.Black
Dim Label36 As Label = New Label
frm2.Label36.Text = TextBox16.Text
frm2.Label36.ForeColor = Color.Black
Dim Label37 As Label = New Label
frm2.Label37.Text = TextBox17.Text
frm2.Label37.ForeColor = Color.Black
Dim Label38 As Label = New Label
frm2.Label38.Text = TextBox18.Text
frm2.Label38.ForeColor = Color.Black
Dim Label39 As Label = New Label
frm2.Label39.Text = TextBox19.Text
frm2.Label39.ForeColor = Color.Black
Dim Label40 As Label = New Label
frm2.Label40.Text = TextBox20.Text
frm2.Label40.ForeColor = Color.Black
End If
If frm2 IsNot Nothing Then
frm2.Show(Me) 'Show Second Form
Me.Hide()
End If
End Sub
Sub Form2HasBeenClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
frm2 = Nothing
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Form 3 Code:(instance calling to form1)
Public Class Form3
Private frm3 As Form3
Private frm1 As Form1
Private frm4 As Form4
Public Sub New1(ByVal callerInstance As Form1)
' Call required if you add your constructor manually
InitializeComponent()
' save the instance of the Me variable passed to this constructor
frm1 = callerInstance
End Sub
End Class
If you want show in label number of right answers then
You need compare all user answers with right answers
And count times when answers are same:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frm4 = New Form4()
frm1 = New Form1()
Dim Textbox1 As New TextBox
Dim Label3 As New Label
Dim sameQnt As Int32 = 0
If Textbox1.Text = frm1.TextBox2.Text Then sameQnt += 1
If Textbox2.Text = frm1.TextBox4.Text Then sameQnt += 1
If Textbox3.Text = frm1.TextBox6.Text Then sameQnt += 1
If Textbox4.Text = frm1.TextBox8.Text Then sameQnt += 1
If Textbox5.Text = frm1.TextBox10.Text Then sameQnt += 1
If Textbox6.Text = frm1.TextBox12.Text Then sameQnt += 1
If Textbox7.Text = frm1.TextBox14.Text Then sameQnt += 1
If Textbox8.Text = frm1.TextBox16.Text Then sameQnt += 1
If Textbox9.Text = frm1.TextBox18.Text Then sameQnt += 1
If Textbox10.Text = frm1.TextBox20.Text Then sameQnt += 1
'Print reslut
frm4.Label3.Text = sameQnt.ToString()
frm4.Show()
End Sub
Here is my new answer
Public Class MainForm '<----Your main form class name
Dim frm4 = New Form4()'<----declare it here
Dim frm1 = New Form1()'<----declare it here
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Textbox1 As New TextBox
Dim Label3 As New Label
Dim i As Integer = 0
If Textbox1.Text = frm1.TextBox2.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox2.Text = frm1.TextBox4.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox3.Text = frm1.TextBox6.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox4.Text = frm1.TextBox8.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox5.Text = frm1.TextBox10.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox6.Text = frm1.TextBox12.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox7.Text = frm1.TextBox14.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox8.Text = frm1.TextBox16.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox9.Text = frm1.TextBox18.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox10.Text = frm1.TextBox20.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
frm4.Label3.Text = i.ToString()
frm4.Show()
End Sub
End Class

Show a MessageBox centered in form

There is a way to center a MessageBox without subclassing or hooking?
I'm looking for VB.NET code.
The solution for VB.NET:
This code is taken and translated from an asnwer of #Hans Passant: Winforms-How can I make MessageBox appear centered on MainForm?
Centered_MessageBox.vb
Imports System.Text
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Class Centered_MessageBox
Implements IDisposable
Private mTries As Integer = 0
Private mOwner As Form
Public Sub New(owner As Form)
mOwner = owner
owner.BeginInvoke(New MethodInvoker(AddressOf findDialog))
End Sub
Private Sub findDialog()
' Enumerate windows to find the message box
If mTries < 0 Then
Return
End If
Dim callback As New EnumThreadWndProc(AddressOf checkWindow)
If EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero) Then
If System.Threading.Interlocked.Increment(mTries) < 10 Then
mOwner.BeginInvoke(New MethodInvoker(AddressOf findDialog))
End If
End If
End Sub
Private Function checkWindow(hWnd As IntPtr, lp As IntPtr) As Boolean
' Checks if <hWnd> is a dialog
Dim sb As New StringBuilder(260)
GetClassName(hWnd, sb, sb.Capacity)
If sb.ToString() <> "#32770" Then
Return True
End If
' Got it
Dim frmRect As New Rectangle(mOwner.Location, mOwner.Size)
Dim dlgRect As RECT
GetWindowRect(hWnd, dlgRect)
MoveWindow(hWnd, frmRect.Left + (frmRect.Width - dlgRect.Right + dlgRect.Left) \ 2, frmRect.Top + (frmRect.Height - dlgRect.Bottom + dlgRect.Top) \ 2, dlgRect.Right - dlgRect.Left, dlgRect.Bottom - dlgRect.Top, True)
Return False
End Function
Public Sub Dispose() Implements IDisposable.Dispose
mTries = -1
End Sub
' P/Invoke declarations
Private Delegate Function EnumThreadWndProc(hWnd As IntPtr, lp As IntPtr) As Boolean
<DllImport("user32.dll")> _
Private Shared Function EnumThreadWindows(tid As Integer, callback As EnumThreadWndProc, lp As IntPtr) As Boolean
End Function
<DllImport("kernel32.dll")> _
Private Shared Function GetCurrentThreadId() As Integer
End Function
<DllImport("user32.dll")> _
Private Shared Function GetClassName(hWnd As IntPtr, buffer As StringBuilder, buflen As Integer) As Integer
End Function
<DllImport("user32.dll")> _
Private Shared Function GetWindowRect(hWnd As IntPtr, ByRef rc As RECT) As Boolean
End Function
<DllImport("user32.dll")> _
Private Shared Function MoveWindow(hWnd As IntPtr, x As Integer, y As Integer, w As Integer, h As Integer, repaint As Boolean) As Boolean
End Function
Private Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
End Class
Usage:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using New Centered_MessageBox(Me)
MessageBox.Show("Test Text", "Test Title", MessageBoxButtons.OK)
End Using
End Sub
Sadly there is no way to centre a MessageBox to a parent. It centres on the screen by default, and cannot be changed.
Create Your Own - Easy Create a form (get rid of controlbox in properties set false)
Place Textbox (called TextBox_Prompt) and set it to multiline in properties
Add 3 Buttons (wide/height enough to hold "CANCEL" comfortably) below the text box
add below code to your form (I used the | character to denote a newline):
Public Class frmMsgBox
Private mName As String = "Message Box" ' default name for form
Private mLocation As Point = New Point(400, 400) ' default location in case user does set
Private mStyle As MsgBoxStyle
Private mPrompt As String
Private mResult As MsgBoxResult
Private b1Result As MsgBoxResult
Private b2Result As MsgBoxResult
Private b3Result As MsgBoxResult
Public WriteOnly Property Style As MsgBoxStyle
Set(value As MsgBoxStyle)
mStyle = value
End Set
End Property
Public WriteOnly Property Prompt As String
Set(value As String)
mPrompt = value
End Set
End Property
Public ReadOnly Property Result As MsgBoxResult
Get
Return mResult
End Get
End Property
Public WriteOnly Property pLocation As Point
Set(value As Point)
mLocation = value
End Set
End Property
Public WriteOnly Property sName As String
Set(value As String)
mName = value
End Set
End Property
Private Sub frmMsgBox_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim strPrompt() As String = mPrompt.Split("|") ' use | for splitting lines
Dim sWidth As Integer = 0
Dim sHeight As String = ""
Me.Text = mName
For Each sLine As String In strPrompt ' get maximum width and height necessary for Prompt TextBox
sWidth = Math.Max(sWidth, TextRenderer.MeasureText(sLine, TextBox_Prompt.Font).Width)
sHeight += "#" + vbCrLf ' TextRenderer.MeasureText("#", TextBox_Prompt.Font).Height
TextBox_Prompt.Text += sLine + vbCrLf
Next
TextBox_Prompt.Width = Math.Min(800, sWidth + 5) ' set max width arbitrarily at 800
TextBox_Prompt.Height = Math.Min(600, TextRenderer.MeasureText(sHeight, TextBox_Prompt.Font).Height) ' set max height to 600 pixels
Me.Width = Math.Max(Me.Width, TextBox_Prompt.Width + Me.Width - Me.ClientRectangle.Width + 20)
TextBox_Prompt.Left = Math.Max(10, (Me.ClientRectangle.Width - TextBox_Prompt.Width) \ 2)
Button1.Top = TextBox_Prompt.Top + TextBox_Prompt.Height + 20
Button2.Top = Button1.Top : Button3.Top = Button1.Top
Me.Height = Me.Height - Me.ClientRectangle.Height + 2 * TextBox_Prompt.Top + TextBox_Prompt.Height + Button1.Height + 20
Dim Space2 As Integer = (Me.ClientRectangle.Width - 2 * Button1.Width) / 3
Dim Space3 As Integer = (Me.ClientRectangle.Width - 3 * Button1.Width) / 4
Select Case mStyle
Case MsgBoxStyle.AbortRetryIgnore
Button1.Text = "Abort" : Button2.Text = "Retry" : Button3.Text = "Ignore"
Button1.Left = Space3
Button2.Left = 2 * Space3 + Button1.Width
Button3.Left = 3 * Space3 + 2 * Button1.Width
b1Result = MsgBoxResult.Abort : b2Result = MsgBoxResult.Retry : b3Result = MsgBoxResult.Ignore
Case MsgBoxStyle.YesNoCancel
Button1.Text = "Yes" : Button2.Text = "No" : Button3.Text = "Cancel"
Button1.Left = Space3
Button2.Left = 2 * Space3 + Button1.Width
Button3.Left = 3 * Space3 + 2 * Button1.Width
b1Result = MsgBoxResult.Yes : b2Result = MsgBoxResult.No : b3Result = MsgBoxResult.Cancel
Case MsgBoxStyle.YesNo
Button1.Text = "Yes" : Button2.Text = "No" : Button3.Visible = False
Button1.Left = Space2
Button2.Left = 2 * Space2 + Button1.Width
b1Result = MsgBoxResult.Yes : b2Result = MsgBoxResult.No
Case MsgBoxStyle.OkCancel
Button1.Text = "Ok" : Button2.Text = "Cancel" : Button3.Visible = False
Button1.Left = Space2
Button2.Left = 2 * Space2 + Button1.Width
b1Result = MsgBoxResult.Ok : b2Result = MsgBoxResult.Cancel
Case MsgBoxStyle.RetryCancel
Button1.Text = "Retry" : Button2.Text = "Cancel" : Button3.Visible = False
Button1.Left = Space2
Button2.Left = 2 * Space2 + Button1.Width
b1Result = MsgBoxResult.Retry : b2Result = MsgBoxResult.Cancel
Case MsgBoxStyle.OkOnly
Button1.Visible = False : Button2.Text = "Ok" : Button3.Visible = False
Button1.Left -= Space2 : Button2.Width += 2 * Space2
b2Result = MsgBoxResult.Ok
End Select
Me.Location = New Point(mLocation.X - Me.Width \ 2, mLocation.Y - Me.Height \ 2)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
mResult = b1Result
Me.Close()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
mResult = b2Result
Me.Close()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
mResult = b3Result
Me.Close()
End Sub
End Class
to use your program you can do the following
Dim ans as MsgBoxResult
Using f As New frmMsgBox With {
.sName = "form tile goes here ",
.Style = MsgBoxStyle.YesNoCancel, ' or whatever style
.Prompt = "Your prompt|2nd line||4th line",
.pLocation = New Point(Me.Left + Me.Width \ 2, Me.Top + Me.Height \ 2)
} ' this location will center MsgBox on form
f.ShowDialog()
ans = f.Result
End Using
If ans = MsgBoxResult.Yes Then
'do whatever
ElseIf ans = MsgBoxResult.No then
'do not whatever
Else ' was cancel
' do cancel
End If
I use this form all the time
You can also add a picture property/box to your form as well as other stuff.
Georg
This is my own message box use C# winform, In addition to the realization of the parent form in the center, can customize the button text and icon. You can convert it to VB code yourself.