Theory
Till में एक File Menu बढ़ता है
ShopKeeper का form buttons से भरता जा रहा है: New bill, Save, Print, Day report, Settings, Exit... Counter एक cockpit जैसा दिखता है, और Mehta Uncle का nephew बार-बार Print ग़लती से press करता रहता है।
हर serious Windows program ने यह decades पहले 2 furniture pieces से solve किया था: ऊपर की menu bar (File, Edit, Help) और वे dialog boxes जो तब आगे आते हैं जब किसी task को एक focused conversation चाहिए: कौन सी file? कौन सा folder? क्या आप sure हैं?
दोनों उसी Toolbox से आते हैं जहाँ से बाकी सब कुछ।
Theory
MenuStrip: Bar और इसके Items
एक MenuStrip को form पर drag कीजिए: यह ऊपर dock हो जाता है और एक type-here box दिखाता है। File type कीजिए, फिर इसके नीचे New bill, Save bill, Exit: हर entry एक ToolStripMenuItem बन जाती है अपने खुद के Click event के साथ, बिल्कुल एक button की तरह handled।
दो polish properties जो examiners पसंद करते हैं:
- access keys:
&Fileका Text Alt+F को इसे खोलने देता है (& F को underline करता है) - ShortcutKeys: Save bill को Ctrl+S assign कीजिए, और menu इसे दिखाता है
एक ContextMenuStrip right-clicks के लिए वही idea है: इसे build कीजिए, फिर एक control की ContextMenuStrip property को इस पर point कीजिए।
Theory
Dialogs: Modal Conversations
एक dialog box ShowDialog() से खुली एक window है: यह modal है, यानी opening form dialog बंद होने तक freeze रहता है। Show() से compare कीजिए, जो एक modeless window खोलता है जो साथ में रहती है।
Dialog का answer एक DialogResult की तरह वापस आता है: OK, Cancel, Yes, No...
.NET ready-made dialog components ship करता है: OpenFileDialog, SaveFileDialog (दोनों FileName expose करते हैं), ColorDialog (Color), FontDialog (Font)। वे component tray में बैठते हैं, बुलाए जाने तक faceless।
Practical
Save Bill, पूरी Conversation
Private Sub mnuSaveBill_Click(sender As Object, e As EventArgs) _
Handles mnuSaveBill.Click
dlgSave.Filter = "Text files|*.txt"
dlgSave.FileName = "bill-" & Now.ToString("dd-MM-yyyy")
If dlgSave.ShowDialog() = DialogResult.OK Then
' user pressed Save: the chosen path is in dlgSave.FileName
lblStatus.Text = "Saved: " & dlgSave.FileName
Else
lblStatus.Text = "Save cancelled"
End If
End Sub
Private Sub mnuExit_Click(sender As Object, e As EventArgs) _
Handles mnuExit.Click
Dim answer As DialogResult = MessageBox.Show( _
"Close ShopKeeper?", "Confirm", MessageBoxButtons.YesNo)
If answer = DialogResult.Yes Then Me.Close()
End Sub
Quiz
एक student लिखता है: dlgSave.ShowDialog() : SaveToFile(dlgSave.FileName) कुछ भी check किए बिना। यह कब ग़लत जाता है?
- जब भी user Cancel press करे: code फिर भी save करता है, FileName में जो भी हो उसी में
- कभी नहीं: ShowDialog() सिर्फ़ तभी return होता है जब user एक valid file चुने
- हमेशा: ShowDialog() को एक variable में assign करना ज़रूरी है वरना यह खुलेगा ही नहीं
- सिर्फ़ तब अगर Filter property set न हो
Show the answer
जब भी user Cancel press करे: code फिर भी save करता है, FileName में जो भी हो उसी में
ShowDialog() दोनों buttons के लिए एक DialogResult return करता है: Save OK देता है, पर Cancel भी return होता है, FileName में जो भी था वह रखे हुए (suggested name, या पिछली बार की choice)। Result ignore करने का मतलब है Cancel फिर भी चुपचाप save कर देता है: वह data लिखा गया जिसे user ने explicitly declined किया। Fix listing वाला pattern है: If ... = DialogResult.OK Then। Option B dialog को ऐसा enforcement credit देता है जो यह नहीं करता। Option C false है (return value directly compare हो सकती है), और Filter (option D) सिर्फ़ file-type list narrow करता है; यह कुछ भी validate नहीं करता।
Think first
Modal या Modeless: 3 Windows पर Rule कीजिए
तीन ShopKeeper windows: (1) क्या आप sure हैं इस bill को delete करना चाहते हैं?, (2) एक floating calculator जो cashier till के बगल रखता है, (3) import करने के लिए file चुनिए। हर एक के लिए ShowDialog() या Show(), और क्यों?
Show the answer
(1) ShowDialog(): एक destructive confirmation को कुछ और होने से पहले answer किया जाना ज़रूरी है; blocking ही point है। (3) ShowDialog(): import एक file के बिना आगे नहीं बढ़ सकता, तो flow को wait करना ज़रूरी है। (2) Show(): calculator billing continue रहते हुए WHILE serve करता है; इसके लिए till को freeze करना hostile होगा। एक line में rule: अगर program answer के बिना sensibly continue नहीं कर सकता, modal; अगर window साथ में assist करती है, modeless। सब कुछ default से modal बनाना beginner tell है।
Watch out
Menu और Dialog के Manners
DialogResult हमेशा Check कीजिए: quiz वाला bug real software में depressingly often ship होता है।
Menu Events Per-Item हैं: mnuSaveBill.Click handle कीजिए, कोई general menu event नहीं; हर ToolStripMenuItem अपना खुद का control है।
Modals Stack मत कीजिए: एक modal जो एक modal खोले जो एक MessageBox खोले users को 3 doors deep trap करता है; flow को flatten कीजिए।
MsgBox बनाम MessageBox.Show: दोनों legal हैं; .NET form DialogResult return करता है और बाकी dialogs के बगल consistently पढ़ता है, तो नए code में इसे prefer कीजिए।
Theory
Conventions Free Usability हैं
Save को File के नीचे Ctrl+S के साथ रखिए और हर वह Windows user जिसने कभी Notepad इस्तेमाल किया हो पहले से आपका program जानता है: यही menu conventions की शांत power है (File पहले, Help आख़िर में, Exit File के bottom पर)। BCA402-02 का UI/UX subject इसे matching user expectations का principle कहेगा। ShopKeeper का face अब complete है: controls, containers, grid, tray workers, menus और dialogs। Unit 3 में एक lesson बाकी है: जब इस सबके पीछे का code ग़लत जाए तो क्या होता है, और VB.NET के error handling के 2 schools।
Summary
Key takeaways
- MenuStrip ToolStripMenuItems से menu bar बनाता है, हर एक अपने खुद के Click event के साथ।
- &File Alt+F access देता है; ShortcutKeys Ctrl+S style shortcuts attach करता है; ContextMenuStrip right-clicks handle करता है।
- Dialogs ShowDialog() से खुलते हैं: modal, blocking, एक DialogResult return करते हुए; Show() modeless companions खोलता है।
- Result इस्तेमाल करने से पहले हमेशा If dlg.ShowDialog() = DialogResult.OK test कीजिए: Cancel भी return होता है।
- Ready-made: OpenFileDialog/SaveFileDialog (FileName), ColorDialog, FontDialog; MessageBox.Show भी DialogResult return करता है।
- Modal जब flow को wait करना ज़रूरी हो; modeless जब window साथ में assist करे।
- Memory hook: menus commands organise करते हैं, dialogs answers के लिए pause करते हैं, और Cancel हमेशा return होता है।