Theory
workers જે તમે જોઈ શકતા નથી
Toolbox થી Timer ને ShopKeeper ના form પર drag કરો અને કંઈક odd થાય છે: તે form પર sit કરવા refuse કરે છે. તે designer ની નીચે tray માં drop થાય છે.
bug નથી. તમે just તમારા first component ને meet કર્યું છે: Toolbox citizen જેની કોઈ face નથી. controls screen space ને occupy કરે છે; components તેમનું work invisibly કરે છે: time keep કરવું, images નો stock કરવો, errors ને flag કરવા, F1 ને answer કરવું. till ને તેમાંથી 4 ની જરૂર છે, અને tray એ જગ્યા છે જ્યાં તેઓ live કરે છે.
Theory
Component vs control, ઔપચારિક રીતે
control visible છે: તેની position છે, size છે, user ના view માં place છે (Button, TextBox, DataGrid).
component functionality provide કરે છે WITHOUT તેની own screen presence; design time પર તે form ની નીચે component tray માં sits કરે છે.
exam નું favourite phrasing: all controls are components, but not all components are controls: visible ones પોતાને draw કરવા માટે extra machinery inherit કરે છે. જો તેની કોઈ face નથી, તેને tray માં look કરો, અને Properties window દ્વારા configure કરો કોઈ પણ other thing જેવું.
At a glance
tray ના 4 workers
| Component | Job | યાદ રાખવા માટે એક call |
|---|---|---|
| ImageList | same-sized images નો stock જે others borrow કરે છે | ImageList1.Images.Add(...); index દ્વારા used |
| ErrorProvider | bad input ની બાજુમાં blinking icon + tooltip | ErrorProvider1.SetError(txtPrice, "Price must be a number") |
| HelpProvider | દરેક control માટે F1 help text | HelpProvider1.SetHelpString(txtQty, "Enter quantity") |
| Timer | દરેક Interval milliseconds પર Tick fire કરે છે | Timer1.Interval = 1000 : Timer1.Start() |
Practical
living clock અને polite error
Public Class BillingForm
Private Sub BillingForm_Load(sender As Object, e As EventArgs) _
Handles MyBase.Load
tmrClock.Interval = 1000 ' milliseconds: 1 second
tmrClock.Start()
End Sub
Private Sub tmrClock_Tick(sender As Object, e As EventArgs) _
Handles tmrClock.Tick
lblClock.Text = Now.ToString("HH:mm:ss") ' beats once a second
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) _
Handles btnAdd.Click
If IsNumeric(txtPrice.Text) Then
errProv.SetError(txtPrice, "") ' clear: input is fine
Else
errProv.SetError(txtPrice, "Price must be a number")
End If
End Sub
End Class
Quiz
Timer1.Interval = 5000 અને Timer1.Start() run થઈ ગયા છે. next શું થાય છે?
- Tick event દરેક 5 seconds પર fire થાય છે જ્યાં સુધી Stop() call ન થાય
- Tick event એકવાર fire થાય છે, 5 seconds પછી, પછી timer stop થાય છે
- program 5 seconds માટે pause થાય છે, Sleep call જેવું
- Tick દરેક second 5000 વાર fire થાય છે
Show the answer
Tick event દરેક 5 seconds પર fire થાય છે જ્યાં સુધી Stop() call ન થાય
Interval એ MILLISECONDS માં gap છે Ticks વચ્ચે, અને started WinForms Timer તે rhythm પર ticking keep કરે છે જ્યાં સુધી તમે Stop() call ન કરો: 5000 એટલે દરેક 5 seconds, repeatedly. Option B one-shot timer ને describe કરે છે, જે તમે first Tick ની અંદર Stop() call કરીને yourself build કરશો. Option C એ crucial misreading છે: Timer ક્યારેય block નથી કરતું; program Ticks વચ્ચે fully responsive રહે છે, જે exactly એટલે છે કે તે pause loop ને beat કરે છે. Option D milliseconds ને upside down read કરે છે.
Think first
શા માટે error પર just MsgBox નહીં?
બંને 1 line છે: MsgBox("Price must be a number") વિરુદ્ધ errProv.SetError(txtPrice, "..."). mid-rush માં cashier ને imagine કરો 6 customers queue થયેલા. tap કરતા પહેલા ErrorProvider માટે argue કરો.
Show the answer
MsgBox everything ને halt કરે છે: modal box કોઈ પણ other work continue થાય તે પહેલા click ને demand કરે છે, દરેક mistake પર once, queue watching સાથે. ErrorProvider announce કરવાને બદલે annotate કરે છે: blinking icon exact guilty field ની બાજુમાં appear થાય છે, message તેના tooltip માં wait કરે છે, cashier તેમના hands free હોય ત્યારે fix કરે છે, અને SetError(ctrl, "") તેને clear કરે છે. Same information, opposite manners. Design rule worth keeping beyond this exam: problems ને ત્યાં point કરો જ્યાં તેઓ live કરે છે; તેમને announce કરવા માટે world ને stop ન કરો.
Watch out
Tray traps
Interval એ milliseconds છે: 1 લખવું અને 1 second ને expect કરવું millisecond pace પર Tick storm આપે છે; 1 second = 1000.
Stopped Timer silent છે: Start() (અથવા Enabled = True) ને forget કરવું એ most common "my clock is frozen" bug છે.
ErrorProvider errors પોતાને clear નથી કરતા: user input ને fix કર્યા પછી, SetError(ctrl, "") call કરો અથવા icon innocent data પર forever blink કરે છે.
ImageList images એક size ને share કરવું જોઈએ: તે everything ને તેના ImageSize પર scale કરે છે; mixed originals distorted come out થાય છે.
Theory
polished till, for free
notice કરો કે professionalism કેટલી cheaply arrive થઈ: live clock (Timer + 1 line), inline validation જે ક્યારેય interrupt નથી કરતું (ErrorProvider), new employee માટે F1 answers (HelpProvider), અને everywhere consistent icons (ImageList). કોઈને visible control અથવા 10 lines of code ની જરૂર નહોતી. અને Timer ને BCA403 ના reminder thread ની બાજુમાં તમારા mind માં file કરો: બંને "schedule પર કંઈક કરો" ને solve કરે છે; WinForms Timer એ UI-friendly, single-threaded cousin છે. next lesson: menus અને dialog boxes, plus VB.NET ના 2 styles of error handling.
Summary
Key takeaways
- Components screen presence વગર work કરે છે અને component tray માં live કરે છે; controls તેમના visible cousins છે.
- ImageList same-sized images નો stock કરે છે જે TreeView, ListView અને buttons index દ્વારા reference કરે છે.
- ErrorProvider.SetError(ctrl, msg) bad input ની બાજુમાં blink કરે છે; SetError(ctrl, "") clear કરે છે: validation without modal interruptions.
- HelpProvider SetHelpString દ્વારા per-control F1 help ને attach કરે છે.
- Timer Start() પછી દરેક Interval MILLISECONDS પર Tick fire કરે છે, repeatedly, program ને block કર્યા વગર.
- 1 second = Interval 1000; never-started Timer ક્યારેય tick નથી કરતો.
- Memory hook: no face, tray માં, હજુ payroll પર.