string fontFamily = font.FontFamily.Name;
“System.NullReferenceException”型別的例外在 Ex0106App1.exe 中發生,但未在用戶代碼中進行處理
其他資訊: 未將物件參考設定到物件的實體。
下為代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Ex0106App1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Font font ;
private void Form1_Load(object sender,EventArgs e)
{
comboBoxWordsize.Items.Clear();
for(int i = 5; i <= 72; i++)
{
comboBoxWordsize.Items.Add(i);
}
font = richTextBoxShow.Font;
comboBoxWordsize.Text = font.Size.ToString();
}
private void checkBoxA_CheckedChanged(object sender, EventArgs e)
{
FontStyle fontStyle1, fontStyle2, fontStyle3, fontStyle4;
fontStyle1 = FontStyle.Regular;
fontStyle2 = FontStyle.Regular;
fontStyle3 = FontStyle.Regular;
fontStyle4 = FontStyle.Regular;
if (checkBoxA.Checked)
{
fontStyle1 = FontStyle.Bold;
}
if (checkBoxB.Checked)
{
fontStyle2 = FontStyle.Italic;
}
if (checkBoxC.Checked)
{
fontStyle3 = FontStyle.Underline;
}
if (checkBoxD.Checked)
{
fontStyle4 = FontStyle.Strikeout;
}
font = new Font(font, fontStyle1 | fontStyle2 | fontStyle3 | fontStyle4);
richTextBoxShow.Font = font;
}
private void radioButtonA_CheckedChanged(object sender, EventArgs e)
{
string fontFamily = font.FontFamily.Name;
if (radioButtonA.Checked)
{
fontFamily = radioButtonA.Text;
}
else if (radioButtonB.Checked)
{
fontFamily = radioButtonB.Text;
}
else if (radioButtonC.Checked)
{
fontFamily = radioButtonC.Text;
}
else fontFamily = radioButtonD.Text;
font = new Font(fontFamily, font.Size, font.Style);
richTextBoxShow.Font = font;
}
private void radioButtonRed_CheckedChanged(object sender, EventArgs e)
{
Color color = richTextBoxShow.ForeColor;
if (radioButtonRed.Checked)
{
color = Color.Red;
}
else if (radioButtonGreen.Checked)
{
color = Color.Green;
}
else if (radioButtonBlue.Checked)
{
color = Color.Blue;
}
else color = Color.Black;
}
private void comboBoxWordsize_SelectedIndexChanged(object sender, EventArgs e)
{
float size = float.Parse(comboBoxWordsize.Text);
font = new Font(font.FontFamily, size);
richTextBoxShow.Font = font;
}
private void Form1_Load_1(object sender, EventArgs e)
{
}
}
}
uj5u.com熱心網友回復:
string fontFamily = font.FontFamily.Name;這里除錯看下font是不是null
uj5u.com熱心網友回復:
不知道怎么處理,然后重寫了一遍沒問題了,bug悔一生,小心使得萬年船不過還是謝謝(問題是font 但菜鳥還不知道除錯)
uj5u.com熱心網友回復:
用 tyr catchc#很好除錯的,你在catch里面斷點,看具體報錯資訊,根據具體問題解決報錯
uj5u.com熱心網友回復:
一看就知道缺少經驗
try
{
}
catch (Exception ex)
{
Console.WriteLine(string.Format("例外資訊:{0}", ex.Message));
}
finally
{
}
先學會這個應用到你程式去 以后 少走幾年的彎路.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/102050.html
標籤:C#
