1、引发类型为“System.Windows.Forms.AxHost+InvalidActiveXStateException”的异常
出现"System.Windows.Forms.AxHost+InvalidActiveXStateException"异常多是引用第三方控件引起的。在NEW时,需要初始化该对象。
AxESACTIVEXLib.AxESActiveX ax = new AxESACTIVEXLib.AxESActiveX(); ((System.ComponentModel.ISupportInitialize)(this.ax)).BeginInit(); this.Controls.Add(ax); ((System.ComponentModel.ISupportInitialize)(this.ax)).EndInit();
注意:该解决方法的使用有待商榷,发现用这种方法会影响界面其它控件的显示。
2、PictureBox控件的Image属性显示网络图片
PictureBox1.Image = Image.FromStream(WebRequest.Create("https://www.baidu.com/img/bd_logo1.png").GetResponse().GetResponseStream());
3、事件的注册与注销的问题
某事件被多次注册时,那么该事件也将会多次触发,其对应的事件处理函数也将被多次调用。
g_kyTTS.SpeakCompletedFlagChanged += new KyTTS.SpeakCompletedFlagChangedEventHandler(g_kyTTS_SpeakCompletedFlagChanged); //注册事件
所以,注册事件后应该要考虑到在合适的地方进行事件的注销
g_kyTTS.SpeakCompletedFlagChanged -= new KyTTS.SpeakCompletedFlagChangedEventHandler(g_kyTTS_SpeakCompletedFlagChanged); //注销事件
4、通过正则表达式获取英文句子字符串中的英文单词数
MatchCollection mc = Regex.Matches("Hello,World!", @"d+.d+|w+"); int l_nSenWordCnt = mc.Count; //英文句子的单词数
5、播放嵌入到资源文件(.resx)的音频
SoundPlayer 类提供了加载和播放 .wav 文件的简单界面。SoundPlayer 类支持从文件路径、URL、包含 .wav 文件的 Stream 或包含 .wav 文件的嵌入资源中加载 .wav 文件。
System.Media.SoundPlayer simpleSound = new System.Media.SoundPlayer(Properties.Resources.ResourceManager.GetStream("Du")); simpleSound.Play();
6、文件(夹)上传到ftp时,出现“远程服务器返回错误: (550) 文件不可用(例如,未找到文件,无法访问文件)”异常
可能是如下原因:
(1)、URL路径不对,看看有没有多加空格,或者大小写问题;
(2)、权限是否足;
(3)、需要反复连接的时候,如GetFileList,需要递归获得所有文件,keepAlive则设成false,一个查询请求完了后就关闭。
7、跨线程访问控件的属性时,出现“线程间操作无效: 从不是创建控件“XXX”的线程访问它”异常
产生原因:
如果从非创建这个控件的线程中访问这个控件或者操作这个控件的话就会抛出这个异常。访问 Windows 窗体控件本质上不是线程安全的。如果有两个或多个线程操作某一控件的状态,则可能会迫使该控件进入一种不一致的状态。还可能出现其他与线程相关的bug,包括争用情况和死锁。确保以线程安全方式访问控件非常重要。
解决办法:
(1)、通过设置System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;来解决,在程序初始化的时候设置这个属性,而且使用的控件都是微软.Net Framework类库中的控件。
(2)、利用委托
delegate void SetTextCallBack(string text); private void SetText(string text) { if (this.txt_a.InvokeRequired) { SetTextCallBack stcb = new SetTextCallBack(SetText); this.Invoke(stcb , new object[] { text}); } else { this.txt_a.Text = text; } }
private void LoadData() { SetText("测试"); }
//窗体加载时,用线程加载数据 private void Frm_ImportManager_Load(object sender, EventArgs e) { ThreadStart ts = new ThreadStart(LoadData); Thread thread = new Thread(ts); thread.Name = "LoadData"; thread.Start(); }
(3)、使用BackgroundWorker控件
// This event handler starts the form's // BackgroundWorker by calling RunWorkerAsync. // // The Text property of the TextBox control is set // when the BackgroundWorker raises the RunWorkerCompleted // event. private void setTextBackgroundWorkerBtn_Click(object sender,EventArgs e) { this.backgroundWorker1.RunWorkerAsync(); } // This event handler sets the Text property of the TextBox // control. It is called on the thread that created the // TextBox control, so the call is thread-safe. // // BackgroundWorker is the preferred way to perform asynchronous // operations. private void backgroundWorker1_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e) { this.textBox1.Text = "This text was set safely by BackgroundWorker."; }
参考博文:http://www.cnblogs.com/luckboy/archive/2010/12/19/1910785.html
8、判断事件是否已经被注册过
用反射取出事件绑定的委托实例,然后用GetInvocationList就可以得到所有注册的方法了。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Reflection;
namespace WA { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Load+=new EventHandler(Form1_Load1); this.Load+=new EventHandler(Form1_Load2); PropertyInfo propertyInfo = (typeof(Form)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic); EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(this, null); FieldInfo fieldInfo = (typeof(Form)).GetField("EVENT_LOAD", BindingFlags.Static | BindingFlags.NonPublic); Delegate d = eventHandlerList[fieldInfo.GetValue(null)]; if (d != null) { foreach (Delegate de in d.GetInvocationList()) Console.WriteLine(de.Method.Name); } } private void Form1_Load1(object sender, EventArgs e) { //TODO } private void Form1_Load2(object sender, EventArgs e) { //TODO } } }
------------------------------------------------------2016-06-29------------------------------------------------------
9、控件的层次显示问题
//a在上 Controls.Add(a); Controls.Add(b); // b在上 Controls.Add(b); Controls.Add(a);
10、窗体添加到容器控件中去
Form g_std = new Form(); g_std.TopLevel = false; panel1.Controls.Add(g_std);
不能显示为模式窗体,既不能这样:
g_std.show(panel1);
------------------------------------------------------2016-08-25------------------------------------------------------
11、将.resx文件中的文件流写入系统临时文件夹
using System.IO;
/// <summary> /// 将音频资源文件写入系统临时文件夹 /// </summary> /// <returns></returns> private string WriteDuResToSysTempFolder() { string l_sTempPath = Path.GetTempPath(); //临时文件夹 string l_sAudioFileName = l_sTempPath + "Du.wav"; //临时文件夹下的文件 //先判断文件是否存在(如果不存在,将其写入临时文件夹) if (!File.Exists(l_sAudioFileName)) { //把资源里的音频文件变为字节数组 byte[] buf = StreamToBytes(Properties.Resources.Du); //写到临时文件里面 File.WriteAllBytes(l_sAudioFileName, buf); } return l_sAudioFileName; } /// <summary> /// 将 Stream 转成 byte[] /// </summary> /// <param name="stream"></param> /// <returns></returns> private byte[] StreamToBytes(Stream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin); return bytes; }
......