Lm th? no ?: Ki?m tra cc lo?i h?nh c?a m?t ?i t?ng COM (System.__ComObject) v?i Visual C#.NET

D?ch tiu ? D?ch tiu ?
ID c?a bi: 320523 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny


Trong Microsoft.NET Framework, cc GetType.Type nh i?u hnh cho m?t ?i t?ng COM s? tr? l?i cc System.__ComObject l?p h?c. Trong m?t s? t?nh hu?ng m? ha, b?n c th? ph?i bi?t cc l?p h?c c? th? cho m?t ?i t?ng. V d?, m?t khch hng t? ?ng ha cho Microsoft Excel c th? xc ?nh lo?i c?a ?i t?ng m ng?i dng ? ch?n trong Excel.

Trong Microsoft Visual C#.NET, cc nh nh i?u hnh l tng t? nh m?t di?n vin nh i?u hnh, ngo?i tr? r?ng n s?n l?ng tr?ng v? chuy?n ?i th?t b?i thay v? trong vi?c nng cao m?t ngo?i l?. B?n c th? s? d?ng cc nh nh i?u hnh ? so snh m?t ?i t?ng COM v?i m?t lo?i h?nh c? th?. N?u cc di?n vin khng tr? v? khng, vi?c chuy?n ?i l thnh cng.

T?ng b?c th? t?c sau y ch?ng t? lm th? no b?n c th? s? d?ng cc nh nh i?u hnh ? ki?m tra xem lo?i COM l m?t ?i t?ng b?ng cch s? d?ng Visual C#.NET.

T?ng b?c

  1. T?o m?t d? n ?ng d?ng Windows m?i trong Visual C#.NET. Theo m?c ?nh, Form1 ?c t?o ra.
  2. Thm m?t tham chi?u ?n Microsoft Excel ?i t?ng th vi?n. thc hin vic nay, hay lam theo nhng bc sau:
    1. Trn cc D an tr?nh n, nh?p vo Thm ti li?u tham kh?o.
    2. Trong cc Thm ti li?u tham kh?o h?p tho?i h?p, b?m vo cc COM tab, b?m vo cc Microsoft Excel ?i t?ng th vi?n, sau b?m Ch?n.

      Chu y Microsoft Office 2003 bao g?m chnh Interop h?i ?ng (PIAs). Microsoft Office XP khng bao g?m PIAs, nhng h? c th? ?c t?i v?.? bi?t thm chi ti?t v? Office XP PIAs, nh?p vo s? bi vi?t sau y ? xem bi vi?t trong c s? ki?n th?c Microsoft:
      328912Thng tin: Microsoft Office XP PIAs c s?n ? t?i v?
    3. Ch?p nh?n l?a ch?n c?a b?n, nh?p vo Ok.
  3. Trn cc Xem tr?nh n, ch?n H?p cng c?, v sau thm m?t nt v m?t h?p vn b?n ? Form1.
  4. ? hi?n th? c?a s? m? cho cc h?nh th?c, b?m p vo Form1.
  5. Thay th? cc handler Form1_Load
    private void Form1_Load(object sender, System.EventArgs e)
    v?i nh?ng i?u sau:
    Excel.ApplicationClass oExcel;
    Excel.Workbooks oBooks;
    Excel.Workbook oBook;
    private void Form1_Load(object sender, System.EventArgs e)
    	//Start Microsoft Excel with a new workbook and give control
    	//to the user.
    	oExcel = new Excel.ApplicationClass();
    	oBooks = oExcel.Workbooks;
    	oBook = oBooks.Add(System.Reflection.Missing.Value);
    	//Layout the controls on Form1 and set up the Click event 
    	//handler for the button.
    	textBox1.Text ="";
    	textBox1.Width = 200;
    	button1.Text = "Get Selection";
    	this.button1.Click += new System.EventHandler(this.ButtonClick);
    private void ButtonClick(object sender, System.EventArgs e)
    	object o = oExcel.Selection;
    	//Display a message about the selection type in Excel.
    	if((o as Excel.Range)!=null)
    		textBox1.Text = "Selection is Excel.Range";
    	else if((o as Excel.Rectangle)!=null)
    		textBox1.Text = "Selection is Excel.Rectangle";
    	else if((o as Excel.ChartObject)!=null)
    		textBox1.Text = "Selection is Excel.ChartObject";
    	else if((o as Excel.ChartArea)!=null)
    		textBox1.Text = "Selection is Excel.ChartArea";
    	// ... There are many additional Selection types you could check for if needed ...
    		textBox1.Text = "Selection is Unknown";
  6. Thm d?i y B?NG CCH S? D?NG n?i dung module M?:
    using Excel = Microsoft.Office.Interop.Excel;

Dng th?

  1. Nh?n F5 ? xy d?ng v ch?y chng tr?nh. Khi cc h?nh th?c t?i, Microsoft Excel b?t ?u v?i m?t b?ng tnh m?i.
  2. S?p x?p Form1 v cc c?a s? ?ng d?ng Excel ? cc b?n c th? xem c? hai.
  3. Trong Excel, b?m vo b?t k? t? bo trong b?ng tnh.
  4. Nh?p vo C ?c l?a ch?n ngy Form1. H?p vn b?n hi?n th? "L?a ch?n l Excel.Range".
  5. Trn cc Chn Menu trong Excel, b?m B?ng x?p h?ng, sau b?m K?t thc trong thu?t s? bi?u ?.
  6. Nh?p vo C ?c l?a ch?n ngy Form1. H?p vn b?n hi?n th? "L?a ch?n l Excel.ChartArea".
  7. Trong Excel, v? m?t h?nh ch? nh?t trn b?ng tnh, v sau ch?n h?nh ch? nh?t.

    Chu y B?n c th? s? d?ng cc H?nh ch? nh?t ki?m sot trn cc B?n v? thanh cng c? ? v? m?t h?nh ch? nh?t trn b?ng tnh.
  8. Nh?p vo C ?c l?a ch?n ngy Form1. H?p vn b?n hi?n th? "L?a ch?n l Excel.Rectangle".

Thu?c tnh

ID c?a bi: 320523 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Excel 2002 Standard Edition
T? kha:
kbpia kbautomation kbhowtomaster kbmt KB320523 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:320523

Cung cp Phan hi


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com