Theory
Workers जिन्हें आप देख नहीं सकते
Toolbox से एक Timer को ShopKeeper के form पर drag कीजिए और कुछ अजीब होता है: यह form पर बैठने से मना कर देता है। इसके बजाय यह designer के नीचे एक tray में drop हो जाता है।
कोई bug नहीं। आप अभी अपने पहले component से मिले हैं: बिना चेहरे वाला एक Toolbox citizen। Controls screen space occupy करते हैं; components अपना काम invisibly करते हैं: time रखना, images stock करना, errors flag करना, F1 answer देना। Till को इनमें से 4 चाहिए, और tray वह जगह है जहाँ वे रहते हैं।
Theory
Component बनाम Control, Formally
एक control visible होता है: इसकी एक position है, एक size है, user के view में एक जगह है (Button, TextBox, DataGrid)।
एक component अपनी खुद की screen presence के BINA functionality provide करता है; design time पर यह form के नीचे component tray में बैठता है।
Exam का favourite phrasing: सारे controls components हैं, पर सारे components controls नहीं हैं: visible वाले खुद को draw करने के लिए extra machinery inherit करते हैं। अगर इसका कोई face नहीं है, इसे tray में ढूँढिए, और इसे किसी और चीज़ की तरह Properties window से configure कीजिए।
At a glance
Tray के 4 Workers
| Component | Job | याद रखने के लिए एक Call |
|---|---|---|
| ImageList | same-sized images stock करता है जो दूसरे borrow करते हैं | ImageList1.Images.Add(...); index से इस्तेमाल होता है |
| 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 हो चुके हैं। आगे क्या होता है?
- Tick event हर 5 seconds में fire होता है जब तक Stop() call न हो
- Tick event एक बार fire होता है, 5 seconds बाद, फिर timer रुक जाता है
- Program 5 seconds के लिए pause होता है, एक Sleep call की तरह
- Tick प्रति second 5000 बार fire होता है
Show the answer
Tick event हर 5 seconds में fire होता है जब तक Stop() call न हो
Interval, Ticks के बीच MILLISECONDS में gap है, और एक started WinForms Timer उस rhythm पर tick करता रहता है जब तक आप Stop() call न करें: 5000 का मतलब है हर 5 seconds में, बार-बार। Option B एक one-shot timer describe करता है, जिसे आप पहले Tick के अंदर Stop() call करके खुद बनाते। Option C crucial misreading है: एक Timer कभी block नहीं करता; Ticks के बीच program पूरी तरह responsive रहता है, यही exactly वजह है यह एक pause loop से बेहतर है। Option D milliseconds को उल्टा पढ़ता है।
Think first
सिर्फ़ MsgBox से Error क्यों नहीं?
दोनों 1 line हैं: MsgBox("Price must be a number") बनाम errProv.SetError(txtPrice, "...")। 6 customers की queue के साथ mid-rush cashier को picture कीजिए। Tap करने से पहले ErrorProvider के लिए argue कीजिए।
Show the answer
MsgBox सब कुछ halt करता है: एक modal box किसी भी दूसरे काम के continue होने से पहले एक click माँगता है, हर mistake पर एक बार, queue watch करते हुए। ErrorProvider interrupt करने की जगह annotate करता है: एक blinking icon exact guilty field के बगल में appear होता है, message इसके tooltip में wait करता है, cashier हाथ खाली होने पर इसे fix करता है, और SetError(ctrl, "") इसे clear करता है। वही information, opposite manners। इस exam से आगे भी रखने लायक design rule: problems को वहीं point out कीजिए जहाँ वे रहते हैं; उन्हें announce करने के लिए दुनिया मत रोकिए।
Watch out
Tray Traps
Interval Milliseconds है: 1 लिखना और 1 second expect करना millisecond pace पर एक Tick storm देता है; 1 second = 1000।
एक Stopped Timer Silent है: Start() (या Enabled = True) भूल जाना सबसे common "my clock is frozen" bug है।
ErrorProvider Errors खुद Clear नहीं होते: user के input fix करने के बाद, SetError(ctrl, "") call कीजिए वरना icon innocent data पर हमेशा के लिए blink करता रहता है।
ImageList Images को एक Size Share करना ज़रूरी है: यह सब कुछ अपने ImageSize तक scale करता है; mixed originals distorted निकलते हैं।
Theory
एक Polished Till, Free में
नोटिस कीजिए professionalism कितने सस्ते में आई: एक live clock (Timer + 1 line), inline validation जो कभी interrupt नहीं करता (ErrorProvider), नए employee के लिए F1 answers (HelpProvider), और हर जगह consistent icons (ImageList)। किसी को एक visible control या 10 lines code नहीं चाहिए था। और Timer को अपने दिमाग़ में BCA403 के reminder thread के बगल में file कीजिए: दोनों "schedule पर कुछ करो" solve करते हैं; WinForms Timer UI-friendly, single-threaded cousin है। अगला lesson: menus और dialog boxes, plus VB.NET की error handling की 2 styles।
Summary
Key takeaways
- Components screen presence के बिना काम करते हैं और component tray में रहते हैं; controls इनके visible cousins हैं।
- ImageList same-sized images stock करता है जिन्हें TreeView, ListView और buttons index से reference करते हैं।
- ErrorProvider.SetError(ctrl, msg) bad input के बगल में blink करता है; SetError(ctrl, "") इसे clear करता है: modal interruptions के बिना validation।
- HelpProvider SetHelpString से per-control F1 help attach करता है।
- Timer Start() के बाद हर Interval MILLISECONDS पर Tick fire करता है, बार-बार, program को block किए बिना।
- 1 second = Interval 1000; एक कभी-start-न-हुआ Timer कभी tick नहीं करता।
- Memory hook: कोई face नहीं, tray में, फिर भी payroll पर।