




已閱讀5頁,還剩4頁未讀, 繼續免費閱讀
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
在一個窗體中設置另一個窗體的控件屬性分類:c#2008-10-13 20:051016人閱讀評論(0)收藏舉報textboxbuttonstringnull最經有學生問我怎么樣在一個窗體中設置另一個窗體的控件屬性,問題如下:有兩個form.一個form1,一個form2.怎么樣在form2中用代碼設置form1的textbox的屬性我考慮了有如下兩種方法:一、1、在form1類中定義一個靜態數據成員,來保存當前窗體對象,如下: public static Form1 f1 = null;2、 然后在form1窗體構造函數中,給上述靜態成員初始化,如下: public Form1() InitializeComponent(); p1 = this; 3、 在form2中調用form1,可以通過如下方式:”Form1.f1”,例如: Form1.f1.textBox1.Text=你想設置的值;以上所做的,只是讓你能夠訪問當前form1對象或form2窗體對象,如果想操作控件,可以直接修改控件的成員訪問符,即可以把”private”修改成”public”。當然最好的還是可以用增加公有屬性的做法,這樣不會破壞類本身的封裝。如下:public string Text_Box get return textBox1.Text; set textBox1.Text = value; 在form2的button_click事件中添加Form1.p1.Text_Box = 你想設置的值;這樣就ok了!源代碼如下Form1的代碼:1. usingSystem;2. usingSystem.Collections.Generic;3. usingSystem.ComponentModel;4. usingSystem.Data;5. usingSystem.Drawing;6. usingSystem.Linq;7. usingSystem.Text;8. usingSystem.Windows.Forms;9.10. namespaceWindowsFormsApplication111. 12. publicpartialclassForm1:Form13. 14. publicstaticForm1p1=null;15. publicForm1()16. 17. InitializeComponent();18. p1=this;19. 20.21. privatevoidbutton1_Click(objectsender,EventArgse)22. 23. Form2fr2=newForm2();24. fr2.Show();25. 26.27. publicvoidsetvalue(stringa)28. 29. textBox2.Text=a;30. 31.32. publicstringText_Box33. 34. getreturntextBox2.Text;35. settextBox2.Text=value;36. 37. 38. 39.Form2的代碼:15. usingSystem;16. usingSystem.Collections.Generic;17. usingSystem.ComponentModel;18. usingSystem.Data;19. usingSystem.Drawing;20. usingSystem.Linq;21. usingSystem.Text;22. usingSystem.Windows.Forms;23.24. namespaceWindowsFormsApplication125. 26. publicpartialclassForm2:Form27. 28. publicForm2()29. 30. InitializeComponent();31. 32.33. privatevoidbutton1_Click(objectsender,EventArgse)34. 35. Form1.p1.Text_Box=aaaa;36. 37. 38. 二、在Form1中定一個public方法和一個按鈕事件(打開Form2)40. /public方法用于修改TextBox的值41. publicvoidSetTextBox(stringstrValue)42. 43. this.textBox1.Text=strValue;44. 45.46. /按鈕事件47. privatevoidbutton1_Click(objectsender,System.EventArgse)48. 49. Form2f=newForm2();50. f.Tag=this;/把Form1的實例傳給Form2實例f的Tag屬性51. f.Show();52. 53.Form2中的按鈕事件60. privatevoidbutton1_Click(objectsender,System.EventArgse)61. 62. Form1f=(Form1)this.Tag;/獲取Form1實例63. f.SetTextBox(去修改Form1中的TextBox的值);/調用Form1的public方法修改Form1中TextBox的值64. 65.以上僅供參考,可能還有別的辦法。C# 操作內存是如何實現的呢?讓我們開始講解吧:我們先來看看C#中如何操作內存,也就是非托管的數據。這需要引用System.Runtime.InteropServices命名空間。該命名空間下的Marshal的一些靜態方法提供了這樣的功能:1. Marshal.ReadInt32() 2. /從指定內存地址讀取4位C#操作內存 3. Marshal.PtrToStringAnsi() 4. /從指定內存地址讀取字符串C#操作內存 5. Marshal.WriteInt32() 6. /將整數寫到指定內存地址C#操作內存 7. Marshal.WriteByte() 8. /將字符串寫到指定內存地址我們來看看具體的代碼: 9. usingSystem; 10. usingSystem.Text; 11. usingSystem.Runtime.InteropServices; 12. 13. internalsealedclassRCEvent 14. publicintEvent; 15. publicintFlag; 16. publicstringUser; 17. ; 18. /C#操作內存19. internalsealedclassRCEventAgent 20. internalstaticRCEventRead(IntPtrptr) 21. RCEventEvent=newRCEvent(); 22. 23. Event.Event=ReadEvent(ptr); 24. Event.Flag=ReadFlag(ptr); 25. Event.User=ReadUser(ptr); 26. 27. returnEvent; 28. 29. /C#操作內存30. internalstaticintReadEvent(IntPtrbasePtr) 31. returnMarshal.ReadInt32(basePtr); 32. 33. internalstaticintReadFlag(IntPtrbasePtr) 34. returnMarshal.ReadInt32(basePtr,4); 35. 36. internalstaticstringReadUser(IntPtrbasePtr) 37. returnMarshal.PtrToStringAnsi( 38. newIntPtr(basePtr.ToInt32()+8); 39. 40. 41. internalstaticvoidWrite(ClientEventEvent,IntPtrptr) 42. WriteEvent(ptr,Event.Event); 43. WriteFlag(ptr,Event.Flag); 44. WriteUser(ptr,Event.User); 45. 46. /C#操作內存47. internalstaticvoidWriteEvent( 48. IntPtrbasePtr,intvalue) 49. Marshal.WriteInt32(basePtr,value); 50. 51. internalstaticvoidWriteFlag( 52. IntPtrbasePtr,intflag) 53. Marshal.WriteInt32(basePtr,4,flag); 54. 55. internalstaticvoidWriteUser( 56. IntPtrbasePtr,stringuser) 57. WriteString(basePtr,user,8,40); 58. 59. privatestaticvoidWriteString( 60. IntPtrbasePtr,stringvalue,intoffset,intlength) 61. intpos=0; 62. bytebytes=Encoding.Default.GetBytes(value); 63. while(poslength) 64. if(posbytes.Length) 65. Marshal.WriteByte(basePtr,offset,bytespos); 66. else 67. Marshal.WriteByte(basePtr,offset,0); 68. 69. pos+; 70. offset+; 71. 72. /C#操作內存73. 這樣我們就可以通過ReadEvent和WriteEvent直接在c#中處理該結構體?;蛘咄ㄟ^ ReadXXX() 和 WriteXXX() 直接修改其字段。74. publicvoidDoSomething(IntPtrptr) 75. RCEventEvent=RCEventAgent.Read(ptr); 76. Event.Flag+; 77. RCEventAgent.Write(ptr,Event); 78. 79. /或者以下代碼 80. /RCEventAgent.WriteFlag(ptr,RCEventAgent.ReadFlag(ptr)+1); 81. C+中則可以直接將結構體地址傳給C#:82. #usingmscorlib.dll 83. #usingCuteSuProc.dll 84. 85. voidSomeMethod(RCEStruct*pEventStruc) 86. MyCSharpDll:DoSomething(pEventStruc); 87. C#操作內存讀寫方法是什么呢?讓我們來看看具體的實例實現:1. usingSystem.Runtime.InteropServices; 2. usingSystem.Text; 3. publicclassFunction 4. 5. /C#操作內存讀寫方法6. publicstaticbytePtrToByte(intPtr) 7. 8. byteb=Marshal.ReadByte(IntPtr)Ptr); 9. 10. returnb; 11. 12. 13. publicstaticcharPtrToChar(intPtr) 14. 15. byteb=Marshal.ReadByte(IntPtr)Ptr); 16. 17. return(char)b; 18. 19. 20. publicstaticshortPtrToShort(intPtr) 21. 22. shortb=Marshal.ReadInt16(IntPtr)Ptr); 23. 24. returnb; 25. 26. /C#操作內存讀寫方法27. publicstaticushortPtrToUShort(intPtr) 28. 29. ushortb=(ushort)Marshal.ReadInt16(IntPtr)Ptr); 30. 31. returnb; 32. 33. 34. publicstaticintPtrToInt(intPtr) 35. 36. intb=Marshal.ReadInt32(IntPtr)Ptr); 37. 38. returnb; 39. 40. 41. publicstaticuintPtrToUInt(intPtr) 42. 43. uintb=(uint)Marshal.ReadInt32(IntPtr)Ptr); 44. 45. returnb; 46. 47. 48. publicstaticlongPtrToLong(intPtr) 49. 50. longb=Marshal.ReadInt64(IntPtr)Ptr); 51. 52. returnb; 53. /C#操作內存讀寫方法54. 55. publicstaticulongPtrToULong(intPtr) 56.
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權】 IEC 60245-6:1994 FR-D Rubber insulated cables - Rated voltages up to and including 450/750 V - Part 6: Arc welding electrode cables
- 假如我會飛向外星球想象類作文12篇
- 物流管理與供應鏈管理實踐試題集
- 應急局考試試題及答案
- 音樂上冊考試試題及答案
- 六一售房部活動方案
- 六一孤兒活動方案
- 六一幼師汗巾活動方案
- 六一活動小媒婆活動方案
- 六一活動照片征集活動方案
- 揭陽惠來縣紀委監委等部門屬下事業單位招聘筆試真題2024
- 黨課課件含講稿:以作風建設新成效激發干事創業新作為
- 超市百貨考試試題及答案
- 2025全國農業(水產)行業職業技能大賽(水生物病害防治員)選拔賽試題庫(含答案)
- 蘇州市昆山市惠民物業管理有限公司招聘考試真題2024
- 模擬電子技術(山東聯盟-山東建筑大學)知到智慧樹期末考試答案題庫2025年山東建筑大學
- GA 1812.2-2024銀行系統反恐怖防范要求第2部分:數據中心
- 2024《整治形式主義為基層減負若干規定》全文課件
- 腸外營養及腸外營養制劑
- 人民幣發展史
- 學校食品安全檔案管理制度
評論
0/150
提交評論