Working with Menus and Dialogue Boxes

MenuStrip menu bar ને item by item build કરે છે, ContextMenuStrip right-clicks ને answer કરે છે, અને dialogs conversation ને pause કરે છે: ShowDialog() DialogResult return કરે છે જે તમારે act કરતા પહેલા check કરવું જોઈએ.

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

till File menu ને grow કરે છે

ShopKeeper નું form buttons થી fill થઈ રહ્યું છે: New bill, Save, Print, Day report, Settings, Exit... counter cockpit જેવું look કરે છે, અને Mehta Uncle નો nephew mistake થી Print પર press કરે છે.

દરેક serious Windows program એ આને decades ago 2 furniture pieces સાથે solve કર્યું: top પર menu bar (File, Edit, Help) અને dialog boxes જે step in કરે છે જ્યારે task ને focused conversation ની જરૂર હોય: કયું file? કયું folder? are you sure?

બંને બીજા બધા જેવા જ Toolbox થી arrive થાય છે.

Theory

MenuStrip: bar અને તેના items

MenuStrip ને form પર drag કરો: તે top પર dock થાય છે અને type-here box ને show કરે છે. File type કરો, પછી તેની નીચે New bill, Save bill, Exit: દરેક entry ToolStripMenuItem બને છે તેના own Click event સાથે, exactly button ના જેમ handled.

બે polish properties જે examiners like કરે છે:

  • access keys: &File નું Text Alt+F ને open કરે છે (& F ને underline કરે છે)
  • ShortcutKeys: Save bill માટે Ctrl+S assign કરો, અને menu તેને show કરે છે

ContextMenuStrip એ same idea છે right-clicks માટે: તેને build કરો, પછી control ની ContextMenuStrip property ને તેના પર point કરો.

Theory

Dialogs: modal conversations

dialog box એ window છે જે ShowDialog() સાથે open થાય છે: તે modal છે, એટલે કે opening form freeze થાય છે જ્યાં સુધી dialog close ન થાય. Show() સાથે compare કરો, જે modeless window ને open કરે છે જે alongside lives કરે છે.

dialog નો answer DialogResult તરીકે back આવે છે: OK, Cancel, Yes, No...

.NET ready-made dialog components ship કરે છે: OpenFileDialog, SaveFileDialog (બંને FileName ને expose કરે છે), ColorDialog (Color), FontDialog (Font). તેઓ component tray માં sits કરે છે, faceless until summoned.

Practical

Save bill, complete 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: chosen path 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

વિદ્યાર્થી લખે છે: dlgSave.ShowDialog() : SaveToFile(dlgSave.FileName) કંઈ check કર્યા વગર. આ ક્યારે wrong થાય છે?

  1. જ્યારે પણ user Cancel દબાવે છે: code anyway save કરે છે, જે FileName hold કરતું હતું તેના પર
  2. ક્યારેય નહીં: ShowDialog() ફક્ત ત્યારે returns થાય છે જ્યારે user valid file ને pick કરે
  3. હંમેશા: ShowDialog() ને variable માં assign કરવું જોઈએ અથવા તે open નથી થતું
  4. ફક્ત જો Filter property set ન હોય
Show the answer

જ્યારે પણ user Cancel દબાવે છે: code anyway save કરે છે, જે FileName hold કરતું હતું તેના પર

ShowDialog() બંને buttons માટે DialogResult return કરે છે: Save OK આપે છે, પણ Cancel પણ return થાય છે, FileName હજુ hold કરતું હોય છે જે ત્યાં હતું (suggested name, અથવા last time ની choice). result ને ignoring કરવાનો અર્થ છે Cancel silently save કરે છે anyway: data written જે user એ explicitly decline કર્યું હતું. fix એ listing માં pattern છે: If ... = DialogResult.OK Then. Option B dialog ને credit આપે છે enforcement માટે જે તે do નથી કરતું. 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) શું તમે ખરેખર આ bill ને delete કરવા માંગો છો?, (2) floating calculator જે cashier till ની બાજુમાં keep કરે છે, (3) import કરવા માટે file ને choose કરો. દરેક માટે ShowDialog() અથવા Show(), અને શા માટે?

Show the answer

(1) ShowDialog(): destructive confirmation ને answer થવું જોઈએ before કંઈ else happen થાય; blocking એ point છે. (3) ShowDialog(): import file વગર proceed નથી કરી શકતું, એટલે flow ને wait કરવું જોઈએ. (2) Show(): calculator billing continue થાય તેની સાથે serve કરે છે; till ને તેના માટે freeze કરવું hostile હોતું. rule એક line માં: જો program answer વગર sensibly continue નથી કરી શકતું, modal; જો window alongside assist કરે છે, modeless. everything ને modal પર default કરવું એ beginner tell છે.

Watch out

Menu અને dialog manners

DialogResult ને check કરો, always: quiz bug real software માં depressingly often ship થાય છે.

Menu events per-item છે: mnuSaveBill.Click ને handle કરો, કોઈ general menu event નહીં; દરેક ToolStripMenuItem એ its own control છે.

modals ને stack ન કરો: modal જે modal ને open કરે છે જે MessageBox ને open કરે છે users ને 3 doors deep trap કરે છે; flow ને flatten કરો.

MsgBox vs MessageBox.Show: બંને legal; .NET form DialogResult return કરે છે અને બીજા dialogs સાથે consistently reads થાય છે, એટલે new code માં તેને prefer કરો.

Theory

Conventions free usability છે

Save ને File ની નીચે મૂકો Ctrl+S સાથે અને દરેક Windows user જે ક્યારેય Notepad ને use કર્યું છે તે already તમારા program ને જાણે છે: એ menu conventions ની quiet power છે (File first, Help last, Exit File ના bottom પર). BCA402-02 નો UI/UX subject એને user expectations ને match કરવાનો principle કહેશે. ShopKeeper નું face હવે complete છે: controls, containers, grid, tray workers, menus અને dialogs. Unit 3 માં એક lesson બાકી છે: શું થાય છે જ્યારે આ બધા ની પાછળ નું code wrong થાય છે, અને VB.NET ની 2 schools of error handling.

Summary

Key takeaways

  • MenuStrip menu bar ને ToolStripMenuItems થી build કરે છે, દરેક તેના own Click event સાથે.
  • &File Alt+F access આપે છે; ShortcutKeys Ctrl+S style shortcuts ને attach કરે છે; ContextMenuStrip right-clicks ને handle કરે છે.
  • Dialogs ShowDialog() સાથે open થાય છે: modal, blocking, DialogResult return કરે છે; Show() modeless companions ને open કરે છે.
  • હંમેશા test કરો If dlg.ShowDialog() = DialogResult.OK before result ને use કરવા: Cancel પણ return થાય છે.
  • Ready-made: OpenFileDialog/SaveFileDialog (FileName), ColorDialog, FontDialog; MessageBox.Show પણ DialogResult return કરે છે.
  • Modal જ્યારે flow ને wait કરવું જોઈએ; modeless જ્યારે window alongside assist કરે છે.
  • Memory hook: menus commands ને organise કરે છે, dialogs answers માટે pause કરે છે, અને Cancel always return થાય છે.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to Windows controls

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Working with Menus and Dialogue Boxes · .NET Programming · Gri-Learn