Jump to content
Sign in to follow this  
urofloxer

Ajuda com botões

Recommended Posts

urofloxer

Boas, estou a desenvolver um "Quem quer ser Milionário" em vb.net, utilizando base de dados...

Imports System.Data.OleDb

Public Class Form2
Dim pergs(50) As Integer
Dim resps(3) As Integer
Dim perg_size As Integer
Dim resp_size As Integer

Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
	Form1.Show()
	Me.Hide()
End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	perguntas(1)

	Dim k As Integer = Label2.Text
	respostas(k)

End Sub

Sub perguntas(ByVal i As Integer)
	Dim j As Integer = 0
	Dim con_str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=qqs.accdb"
	Dim con As New OleDbConnection
	con.ConnectionString = con_str
	con.Open()

	Dim SQL As String = "Select * from perg where nivel=" & i
	Dim cmd As New OleDbCommand(SQL, con)

	Dim dr As OleDbDataReader

	dr = cmd.ExecuteReader

	While dr.Read
		pergs(j) = dr("cod")
		j = j + 1
	End While
	perg_size = j - 1
	con.Close()

	RandomizeArray(pergs, perg_size)

	con = New OleDbConnection
	con.ConnectionString = con_str
	con.Open()

	Label2.Text = pergs(0)
	SQL = "Select * from perg where cod=" & pergs(0)
	cmd = New OleDbCommand(SQL, con)

	dr = cmd.ExecuteReader

	dr.Read()
	Label1.Text = dr("pergunta")
	con.Close()


End Sub

Sub respostas(ByVal i As Integer)

	Dim j As Integer = 0
	Dim con_str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=qqs.accdb"
	Dim con As New OleDbConnection

	con.ConnectionString = con_str
	con.Open()

	Dim Sql As String = "Select * from resp where cod_perg=" & i & " and vf='v'"
	Dim cmd As New OleDbCommand(Sql, con)

	Dim dr As OleDbDataReader

	dr = cmd.ExecuteReader

	dr.Read()
	Dim rp_verd As Integer = dr("cod")
	con.Close()

	con = New OleDbConnection
	con.ConnectionString = con_str
	con.Open()

	Sql = "Select * from resp where cod_perg=" & i & " and vf='F'"
	cmd = New OleDbCommand(Sql, con)
	Dim f_resp(50) As Integer

	dr = cmd.ExecuteReader
	j = 0
	While dr.Read
		f_resp(j) = dr("cod")
		j = j + 1
	End While
	resp_size = j - 1
	con.Close()
	RandomizeArray(f_resp, resp_size)
	'shuffle f_resp()

	resps(0) = rp_verd
	resps(1) = f_resp(0)
	resps(2) = f_resp(1)
	resps(3) = f_resp(2)
	RandomizeArray(resps, 3)
	'shuffle resps()


	Sql = "Select resp from resp where cod=" & resps(0)
	cmd = New OleDbCommand(Sql, con)
	con.Open()
	dr = cmd.ExecuteReader
	dr.Read()
	Label3.Text = dr("resp")
	con.Close()

	Sql = "Select resp from resp where cod=" & resps(1)
	cmd = New OleDbCommand(Sql, con)
	con.Open()
	dr = cmd.ExecuteReader
	dr.Read()
	Label4.Text = dr("resp")
	con.Close()

	Sql = "Select resp from resp where cod=" & resps(2)
	cmd = New OleDbCommand(Sql, con)
	con.Open()
	dr = cmd.ExecuteReader
	dr.Read()
	Label5.Text = dr("resp")
	con.Close()

	Sql = "Select resp from resp where cod=" & resps(3)
	cmd = New OleDbCommand(Sql, con)
	con.Open()
	dr = cmd.ExecuteReader
	dr.Read()
	Label6.Text = dr("resp")
	con.Close()

End Sub

Private Sub RandomizeArray(ByVal items() As Integer, ByVal size As Integer)
	Dim max_index As Integer = size
	Dim rnd As New Random
	For i As Integer = 0 To max_index - 1
		' Pick an item for position i.
		Dim j As Integer = rnd.Next(i, max_index + 1)

		' Swap them.
		Dim temp As Integer = items(i)
		items(i) = items(j)
		items(j) = temp
	Next i
End Sub

Private Sub botaocerto()

End Sub

Dim bBtnClicked As Boolean = False
Private Sub cmdOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
	If bBtnClicked = True Then
		MessageBox.Show("Certo!")
	Else
		MessageBox.Show("_______")
	End If
	bBtnClicked = True
End Sub
End Class

... a minha dúvida é a seguinte, como é que eu faço para que o programa saiba que eu clico no botão da resposta certa?

Edited by Rui Carlos
Formatação do código.

Share this post


Link to post
Share on other sites
thoga31

São 4 botões. A cada botão associas um número - 1, 2, 3 e 4. Na BD incluis que a resposta certa é a 1, 2, 3 ou 4. Assim, quando tens uma determinada questão a decorrer, terás numa variável o valor da resposta certa. Quando clicas num botão, verificas se o seu código é igual a esse valor.

É a forma mais directa e fácil que me ocorre.


Knowledge is free!

Share this post


Link to post
Share on other sites
acao

boas

já usei idêntico em vb6 ao poste do amigo @thoga31 mas utilizando o índice.

deixo aqui esse exemplo.

verificar qual o botao clicado

Private Sub Toolbar1_Buttonclick(ByVal Button As MSComctlLib.Button)
Dim vOk As Integer
'Verifica qual foi o botão clicado:
Select Case Button.Index
	Case 1
		'Botão Gravar:
		ConsCamposEditados
		If vInclusao = False Then
			If txtNumeroMsg.Text <> Empty And txtAssuntoMsg.Text <> Empty And vConsCamposEditados = True Then
				vOk = MsgBox("Ainda não gravou os dados editados da tela, deseja GRAVAR ?", _
				vbYesNo + vbQuestion + vbApplicationModal, "Saída")
				If vOk = vbYes Then
					GravarDados
				ElseIf vOk = vbNo Then
					LimparTela
					MsgBox "O registo não foi editado"
				End If
			ElseIf lblDataMsg.Caption <> vDataHojeMsgConsCamposEditados Then
				MsgBox "O registo não pode ser editado, a DataMsg é diferente de hoje"
			ElseIf txtAssuntoMsg.Text = Empty Then
				MsgBox "O registo não foi editado, o campo Assunto Msg está vazio"
			End If
		Else
			GravarDados
		End If
	Case 2
		'Botão Limpar:
		LimparTela
	Case 3
		'Botão Excluir:
	Case 4
		'Botão Retornar:
	Unload Me
End Select
End Sub

Edited by acao

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.