vb.net - How to connect keyboard events to press down buttons in Visual Basic? -
i'm making hangman game project in school, , can't create keyboard function buttons have in our game. have 29 buttons letters "a-Å" , can press them using mousepad/mouse. i'm programming in visualbasic.
public class form1 private ord_liste() string private ord string private r new random private feil single
private sub btn_click(byval btn button) btn.enabled = false phone call check(btn.text) end sub private sub buttons_click(byval sender system.object, byval e system.eventargs) handles button1.click, button2.click, button3.click, button4.click, button5.click, button6.click, button7.click, button8.click, button9.click, button10.click, button11.click, button12.click, button13.click, button14.click, button15.click, button16.click, button17.click, button18.click, button19.click, button20.click, button21.click, button22.click, button23.click, button24.click, button25.click, button26.click, button27.click, button28.click, button29.click dim btn button = directcast(sender, button) phone call btn_click(btn) end sub
'this whats not working me...
private sub form1_keydown(byval sender system.object, byval e system.windows.forms.keyeventargs) handles mybase.keydown each btn button in panel1.controls.oftype(of button)() if e.keycode.tostring.tolower = btn.text.tolower phone call btn_click(btn) end if next end sub private sub check(byval letter string) if ord.contains(letter.tolower) 'sjekke om bokstaven passer ordet. dim indexes new list(of integer) integer = 0 ord.length - 1 if ord.substring(i, 1).tolower = letter.tolower indexes.add(i) end if next 'denne gjør @ når du gjetter rett bokstav så blir den skrevet ut tekstboksen. each int integer in indexes textbox1.text = textbox1.text.remove(int * 4, 4).insert(int * 4, letter.toupper & " ") next 'du har vunnet if textbox1.text.contains("_") = false messagebox.show("gratulerer, du har vunnet!", me.text, messageboxbuttons.ok, messageboxicon.information) phone call nyttspill() end if else 'du gjettet feil bokstav. feil += 1 select case feil case 1 head.visible = true case 2 body.visible = true case 3 left_arm.visible = true case 4 right_arm.visible = true case 5 left_leg.visible = true case 6 right_leg.visible = true messagebox.show("beklager, men du har tapt. ordet du skulle ha var: " & ord, me.text, messageboxbuttons.ok, messageboxicon.information) phone call nyttspill() end select end if end sub private sub nyttspill() 'starter spillet på nytt og gjemmer kroppen. head.visible = false body.visible = false left_arm.visible = false right_arm.visible = false left_leg.visible = false right_leg.visible = false 'setter antall feil til 0. feil = 0 'velger nytt random ord. dim integer = r.next(0, ord_liste.count) ord = ord_liste(i) 'viser hvor langt det er. textbox1.clear() int integer = 0 ord.length - 1 textbox1.text &= "__ " next 'knappene blir "utrykket" igjen. each btn button in panel1.controls.oftype(of button)() btn.enabled = true next end sub private sub form1_load(byval sender system.object, byval e system.eventargs) handles mybase.load dim del() string = {environment.newline} ord_liste = my.resources.ord_fil.split(del, stringsplitoptions.removeemptyentries) phone call nyttspill() end sub
end class
this code have far.
borrowing technique this question, in keydown handler you'll need to:
identify key/letter pressed (a case statement, perhaps)change appropriate button's flatstyle property,
cmdabutton.flatstyle = flatstyle.flat
call event handler appropriate button, or perform desired code,
cmdabutton.performclick()
then, create keyup handler similar keydown you,
identify key/letter pressed, andrevert appropriate button's flatstyle,
cmdabutton.flatstyle = flatstyle.standard
vb.net
No comments:
Post a Comment