/// <summary> /// Получить бинарный код /// </summary> /// <param name="data">Входное число</param> /// <returns>Бинарный код</returns> public LongBits GetDKFromComparators(int data) { LongBits simpleCode = GetEKFromComparators(data); LongBits simplePositionCode = MathProcessor.GetEPKFromEK(simpleCode); return(MathProcessor.GetDK(simplePositionCode)); }
public static int[] GetStringIndexesOfDifferenceBits(LongBits first, LongBits second) { if (first.bitsString.Equals(second.bitsString)) { return new int[] { } } ; string firstString = first.ToString(); string secondString = second.ToString(); while (firstString.Length > secondString.Length) { secondString = "0" + secondString; } while (firstString.Length < secondString.Length) { firstString = "0" + firstString; } List <int> indexesOfBits = new List <int>(); for (int i = 0; i < firstString.Length; i++) { if (firstString[i] != secondString[i]) { indexesOfBits.Add(i); } } return(indexesOfBits.ToArray()); }
public static double[] LimitDeltaIndexes(DACEmulator emulator, int input, double threshold = 0.001) { double[] oldIndexes = emulator.DeltaIndexes; LongBits outputBinaryCode = emulator.GetDKFromComparators(input); LongBits inputBinaryCode = new LongBits(input, emulator.N); double k = 1.1; while (inputBinaryCode != outputBinaryCode) { for (int i = 0; i < outputBinaryCode.Length; i++) { if (emulator.DeltaIndexes[i] > -threshold && emulator.DeltaIndexes[i] < threshold) { return(null); } if (inputBinaryCode != outputBinaryCode) { emulator.DeltaIndexes[i] /= k; } } outputBinaryCode = emulator.GetDKFromComparators(input); } double[] critical = emulator.DeltaIndexes; emulator.DeltaIndexes = oldIndexes; return(critical); }
/// <summary> /// Получение массива ЕПК /// </summary> /// <param name="e">Массив ЕК</param> /// <returns>Массив ЕПК</returns> public static LongBits[] GetAllEPKFromEK(LongBits[] e) { LongBits[] b = new LongBits[e.Length]; for (int i = 0; i < b.Length; i++) { b[i] = GetEPKFromEK(e[i]); } return(b); }
private void buttonGetModel_Click(object sender, EventArgs e) { int n = 0; double coeff = 0, deltaCoeff = 0, deltaSM = 0; double[] masDelta; try { n = (int)numericUpDownN.Value; coeff = (double)numericUpDownK.Value; deltaCoeff = (double)numericUpDownDK.Value; { List <double> l = new List <double> { }; foreach (DataGridViewCell cell in dataGridViewDeltaI.Rows[0].Cells) { l.Add(Convert.ToDouble(cell.Value)); } masDelta = l.ToArray(); } deltaSM = (double)numericUpDownDUsm.Value; } catch { MessageBox.Show("Неверный формат входных параметров!"); return; } DACEmulator emulator = new DACEmulator(coeff, deltaCoeff, masDelta, deltaSM); voltagesQuantumStep = emulator.RealStep; int countNumbers = (int)Math.Pow(2, n); modelVoltages = new double[countNumbers]; idealVoltages = new double[countNumbers]; dataGridViewVect.Rows.Clear(); for (int x = 0; x < countNumbers; x++) { modelVoltages[x] = emulator.Uin(x); idealVoltages[x] = emulator.IdealUin(x); LongBits binaryCode = emulator.GetDKFromComparators(x); LongBits inCode = new LongBits(x, n); int[] errorInds = emulator.GetEKPErrorFromComparators(x); dataGridViewVect.Rows.Add(new object[] { inCode, binaryCode, inCode.ToLong(), binaryCode.ToLong(), string.Join(", ", errorInds) }); if (inCode != binaryCode) { dataGridViewVect.Rows[x].DefaultCellStyle.BackColor = errorCellBackColor; } } modelVoltageColor = Color.DarkOrchid; VoltageChartService chartService = new VoltageChartService(this.mainChart, "Входное напряжение", voltagesQuantumStep); chartService.AddInputVoltageList("Voltages", modelVoltages, modelVoltageColor, 2); chartService.AddInputVoltageList("Ideal voltages", idealVoltages, idealVoltageColor, 2); }
/// <summary> /// Получить массив ЕК /// </summary> /// <param name="value">Верхняя граница</param> /// <returns>Массив ЕК</returns> public static LongBits[] GetAllEK(int value) { LongBits[] singleCodes = new LongBits[value]; for (int i = 0; i < value; i++) { singleCodes[i] = GetEK(i, value - 1); } return(singleCodes); }
public static LongBits operator >>(LongBits bits, int count) { LongBits newbits = new LongBits(bits.bitsString.ToString(0, bits.Length - count)); for (int i = 0; i < count; i++) { newbits.bitsString.Insert(0, '0'); } return(newbits); }
public static LongBits operator |(LongBits first, LongBits second) { LongBits newbits = new LongBits(first.ToString()); for (int i = 0; i < newbits.Length; i++) { newbits[i] = (newbits[i] == 1 || second[i] == 1) ? 1 : 0; } return(newbits); }
public static LongBits operator <<(LongBits bits, int count) { LongBits newbits = new LongBits(bits.bitsString.ToString(count, bits.Length - count)); for (int i = 0; i < count; i++) { newbits.bitsString.Append('0'); } return(newbits); }
public static LongBits operator ~(LongBits bits) { LongBits newbits = new LongBits(bits.ToString()); for (int i = 0; i < newbits.Length; i++) { newbits[i] = (newbits[i] == 0) ? 1 : 0; } return(newbits); }
/// <summary> /// Преобразовать десятичное число в ЕК /// </summary> /// <param name="value">Десятичное число</param> /// <returns>ЕК</returns> public static LongBits GetEK(int value, int count) { LongBits singleCode = new LongBits(count); for (int i = 0; i < value; i++) { singleCode[i] = 1; } return(singleCode); }
/// <summary> /// Получение ЕПК /// </summary> /// <param name="e">ЕК</param> /// <returns>ЕПК</returns> public static LongBits GetEPKFromEK(LongBits e) { LongBits b = new LongBits(e.Length); for (int i = 0; i < b.Length; i++) { b[i] = e[i] & ~(i == b.Length - 1 ? 0 : e[i + 1]); } return(b); }
private void buttonGetModel_Click(object sender, EventArgs e) { int n = 0; double coeff = 0, deltaCoeff = 0, deltaSM = 0, deltaI = 0; try { n = (int)numericUpDownN.Value; coeff = (double)numericUpDownK.Value; deltaCoeff = (double)numericUpDownDK.Value; deltaI = (double)numericUpDownDI.Value; deltaSM = (double)numericUpDownDUsm.Value; } catch { MessageBox.Show("Неверный формат входных параметров!"); return; } if (graphForm != null && graphForm.Visible) { graphForm.SelectedPoint = new Point3D((float)deltaCoeff, (float)deltaSM, (float)deltaI); graphForm.RefreshGraph(); } DACEmulator emulator = new DACEmulator(n, coeff, deltaCoeff, deltaI, deltaSM); voltagesQuantumStep = emulator.RealStep; int countNumbers = (int)Math.Pow(2, n); modelVoltages = new double[countNumbers]; idealVoltages = new double[countNumbers]; dataGridViewVect.Rows.Clear(); for (int x = 0; x < countNumbers; x++) { modelVoltages[x] = emulator.Uin(x); idealVoltages[x] = emulator.IdealUin(x); LongBits binaryCode = emulator.GetDKFromComparators(x); LongBits inCode = new LongBits(x, n); int[] errorInds = emulator.GetEKPErrorFromComparators(x); dataGridViewVect.Rows.Add(new object[] { inCode, binaryCode, inCode.ToLong(), binaryCode.ToLong(), string.Join(", ", errorInds) }); if (inCode != binaryCode) { dataGridViewVect.Rows[x].DefaultCellStyle.BackColor = errorCellBackColor; } } toolStripMenuItemCopy.Visible = true; modelVoltageColor = Color.DarkOrchid; VoltageChartService chartService = new VoltageChartService(this.mainChart, "Входное напряжение", voltagesQuantumStep); chartService.AddInputVoltageList("Voltages", modelVoltages, modelVoltageColor, 2); chartService.AddInputVoltageList("Ideal voltages", idealVoltages, idealVoltageColor, 2); }
/// <summary> /// Получить идеальный сигнал /// </summary> /// <param name="data">Входное число</param> /// <returns>Идеальный сигнал</returns> public double IdealUin(int data) { LongBits x = new LongBits(data, N); double sum = 0; for (int i = 1; i <= N; i++) { sum += x[i - 1] * Math.Pow(2, -(N - i)); } return(Coeff * sum); }
/// <summary> /// Получить сигнал учитывая отклонения /// </summary> /// <param name="data">Входное число</param> /// <param name="deltaIsUp">Задания знака для delta</param> /// <returns>Сигнал учитывая отклонения</returns> public double Uin(int data, bool deltaIsUp = true) { LongBits x = new LongBits(data, N); double sum = 0; for (int i = 1; i <= N; i++) { sum += x[i - 1] * Math.Pow(2, -(N - i + (DeltaIndexes == null ? DeltaIndex : DeltaIndexes[i - 1]))); } double delta = deltaIsUp ? DeltaSM : -DeltaSM; return((Coeff + DeltaCoeff) * sum + (DeltaUsm() + delta)); }
/// <summary> /// Транспонирование матрицы /// </summary> /// <param name="bitMatrix">Матрица битов</param> /// <returns>Транспонированная матрица бмтов</returns> public static LongBits[] TransposeBitMatrix(LongBits[] bitMatrix) { LongBits[] transpose = new LongBits[bitMatrix[0].Length]; for (int i = 0; i < transpose.Length; i++) { transpose[i] = new LongBits(bitMatrix.Length); for (int j = 0; j < bitMatrix.Length; j++) { transpose[i][j] = bitMatrix[j][i]; } } return(transpose); }
/// <summary> /// Получить позиционный код /// </summary> /// <param name="data">Входное число</param> /// <returns>Позиционный код</returns> public LongBits GetEKFromComparators(int data) { int m = (int)Math.Pow(2, N) - 1; LongBits e = new LongBits(m); double deltaUsm = DeltaUsm(); double uop = MaxSignal(); double uin = Uin(data); for (int t = 1; t <= m; t++) { e[t - 1] = (uin + deltaUsm >= ((double)t / m) * uop) ? 1 : 0; } return(e); }
private static bool ErrorChecking(DACEmulator emulator) { int countNumbers = (int)Math.Pow(2, emulator.N); LongBits inputBinaryCode = new LongBits(0, emulator.N), outputBinaryCode = inputBinaryCode; for (int x = 0; x < countNumbers; x++) { inputBinaryCode = new LongBits(x, emulator.N); outputBinaryCode = emulator.GetDKFromComparators(x); if (inputBinaryCode != outputBinaryCode) { return(false); } } return(true); }
private void buttonCritical_Click(object sender, EventArgs e) { int n = 0, deltaIndex = 0; double coeff = 0, deltaCoeff = 0, deltaSM = 0; n = (int)numericUpDownN.Value; coeff = (double)numericUpDownK.Value; try { deltaSM = double.Parse(labelCriticalDsm.Text); } catch { MessageBox.Show("Критические параметры отсутствуют!"); return; } DACEmulator emulator = new DACEmulator(n, coeff, deltaCoeff, deltaIndex, deltaSM); voltagesQuantumStep = emulator.RealStep; int countNumbers = (int)Math.Pow(2, n); modelVoltages = new double[countNumbers]; idealVoltages = new double[countNumbers]; dataGridViewVect.Rows.Clear(); for (int x = 0; x < countNumbers; x++) { modelVoltages[x] = emulator.Uin(x); idealVoltages[x] = emulator.IdealUin(x); LongBits binaryCode = emulator.GetDKFromComparators(x); LongBits inCode = new LongBits(x, n); int[] errorInds = emulator.GetEKPErrorFromComparators(x); dataGridViewVect.Rows.Add(new object[] { inCode, binaryCode, inCode.ToLong(), binaryCode.ToLong(), string.Join(", ", errorInds) }); if (inCode != binaryCode) { dataGridViewVect.Rows[x].DefaultCellStyle.BackColor = errorCellBackColor; } } toolStripMenuItemCopy.Visible = true; modelVoltageColor = Color.FromKnownColor(KnownColor.Highlight); VoltageChartService chartService = new VoltageChartService(this.mainChart, "Входное напряжение при критическом δсм", voltagesQuantumStep); chartService.AddInputVoltageList("Voltages", modelVoltages, modelVoltageColor, 2); chartService.AddInputVoltageList("Ideal voltages", idealVoltages, idealVoltageColor, 2); }
/// <summary> /// Получить индексы битов с ошибками в последовательном позиционном коде /// </summary> /// <param name="data">Входное число</param> /// <returns>Индексы битов с ошибками в последовательном позиционном коде</returns> public int[] GetEKPErrorFromComparators(int data) { int m = (int)Math.Pow(2, N) - 1; LongBits idealSimpleCode = MathProcessor.GetEK(data, m); LongBits idealSimplePositionCode = MathProcessor.GetEPKFromEK(idealSimpleCode); LongBits realSimpleCode = GetEKFromComparators(data); LongBits realSimplePositionCode = MathProcessor.GetEPKFromEK(realSimpleCode); List <int> errors = new List <int>(); for (int i = 0; i < realSimpleCode.Length; i++) { if (idealSimplePositionCode[i] != realSimplePositionCode[i]) { errors.Add(i + 1); } } return(errors.ToArray()); }
public static double LimitDeltaCoeff(DACEmulator emulator, int input, double threshold = 0.001) { double oldDeltaCoeff = emulator.DeltaCoeff; LongBits inputBinaryCode = new LongBits(input, emulator.N); LongBits outputBinaryCode = emulator.GetDKFromComparators(input); double k = 1.1; while (inputBinaryCode != outputBinaryCode) { if (emulator.DeltaCoeff > -threshold && emulator.DeltaCoeff < threshold) { return(0); } emulator.DeltaCoeff /= k; outputBinaryCode = emulator.GetDKFromComparators(input); } double critical = emulator.DeltaCoeff; emulator.DeltaCoeff = oldDeltaCoeff; return(critical); }
/// <summary> /// Вывод формулы /// </summary> /// <param name="transpB">Массив ЕПК</param> /// <param name="a">ДК</param> /// <returns>Формулы ДК</returns> public static string[] Formules(LongBits[] b, out LongBits[] a) { LongBits[] transpB = TransposeBitMatrix(b); int n = GetN(transpB.Length + 1); a = new LongBits[n]; string[] formules = new string[n]; for (int i = 0; i < a.Length; i++) { a[i] = new LongBits(transpB[0].Length); a[i] = ~a[i]; string withoutDigit = string.Empty; string withDigit = string.Empty; int kmax = (int)Math.Pow(2, n - 1 - i) - 1; for (int k = 0; k <= kmax; k++) { int tmin = (int)Math.Pow(2, i) * (2 * k + 1); int tmax = (int)Math.Pow(2, i + 1) * (k + 1) - 1; for (int t = tmin; t <= tmax; t++) { withoutDigit += "¬b" + t; withDigit += "¬" + transpB[t - 1]; a[i] &= ~transpB[t - 1]; if (k != kmax || t != tmax) { withoutDigit += "ʌ"; withDigit += "ʌ"; } } } a[i] = ~a[i]; formules[i] = "¬(" + withoutDigit + ")=" + "¬(" + withDigit + ")=" + a[i]; } return(formules); }
/// <summary> /// Вычисление ДК /// </summary> /// <param name="b">ЕПК</param> /// <returns>ДК</returns> public static LongBits GetDK(LongBits b) { int n = GetN(b.Length + 1); LongBits a = new LongBits(n); for (int i = 0; i < a.Length; i++) { a[i] = a.Negative(i); int kmax = (int)Math.Pow(2, n - 1 - i) - 1; for (int k = 0; k <= kmax; k++) { int tmin = (int)Math.Pow(2, i) * (2 * k + 1); int tmax = (int)Math.Pow(2, i + 1) * (k + 1) - 1; for (int t = tmin; t <= tmax; t++) { a[i] &= b.Negative(t - 1); } } a[i] = a.Negative(i); } return(a); }
bool GetIndexesOfDiffs(LongBits first, LongBits second, out List <int> diffs) { //Все таки пришлось циклом сравнить. Ваня, не бей( diffs = null; if (first.Length != second.Length) { return(first == second); } diffs = new List <int>() { }; string sf = first.ToString(); string ss = second.ToString(); for (int i = 0; i < sf.Length; i++) { if (sf[i] != ss[i]) { diffs.Add(i); } } return(diffs.Count == 0); }
/// <summary> /// Вывод формулы /// </summary> /// <param name="b">ЕПК</param> /// <param name="a">ДК</param> /// <returns>Формула ДК</returns> public static string[] Formules(LongBits b, out LongBits a) { int n = GetN(b.Length + 1); a = new LongBits(n); string[] formules = new string[n]; for (int i = 0; i < a.Length; i++) { a[i] = a.Negative(i); string withoutDigit = string.Empty; string withDigit = string.Empty; int kmax = (int)Math.Pow(2, n - 1 - i) - 1; for (int k = 0; k <= kmax; k++) { int tmin = (int)Math.Pow(2, i) * (2 * k + 1); int tmax = (int)Math.Pow(2, i + 1) * (k + 1) - 1; for (int t = tmin; t <= tmax; t++) { withoutDigit += "¬b" + t; withDigit += b.Negative(t - 1); a[i] &= b.Negative(t - 1); if (k != kmax || t != tmax) { withoutDigit += "ʌ"; withDigit += "ʌ"; } } } a[i] = a.Negative(i); formules[i] = "¬(" + withoutDigit + ")=" + "¬(" + withDigit + ")=" + a[i]; } return(formules); }
/// <summary> /// Вычисление ДК /// </summary> /// <param name="b">Массив ЕПК</param> /// <returns>Массив ДК</returns> public static LongBits[] GetAllDK(LongBits[] b) { LongBits[] transpB = TransposeBitMatrix(b); int n = GetN(transpB.Length + 1); LongBits[] a = new LongBits[n]; for (int i = 0; i < a.Length; i++) { a[i] = new LongBits(transpB[0].Length); a[i] = ~a[i]; int kmax = (int)Math.Pow(2, n - 1 - i) - 1; for (int k = 0; k <= kmax; k++) { int tmin = (int)Math.Pow(2, i) * (2 * k + 1); int tmax = (int)Math.Pow(2, i + 1) * (k + 1) - 1; for (int t = tmin; t <= tmax; t++) { a[i] &= ~transpB[t - 1]; } } a[i] = ~a[i]; } return(a); }
public static ParamsContainer TestingDelta(int n, double coeff, DACEmulator.Delta delta, double initialStep = 1, double accuracy = 0.0001, double deltaCoeff = 0, double deltaIndex = 0, double deltaSM = 0, bool isAllowedValues = false) { int countNumbers = (int)Math.Pow(2, n); List <int> indexes = new List <int>(); ParamsContainer container = new ParamsContainer(n, coeff, deltaCoeff, deltaIndex, deltaSM); LongBits inputBinaryCode = new LongBits(0, n), outputBinaryCode = inputBinaryCode; DACEmulator emulator = new DACEmulator(n, coeff, deltaCoeff, deltaIndex, deltaSM); while (Math.Abs(initialStep * 2) > accuracy) { inputBinaryCode = outputBinaryCode; while (inputBinaryCode == outputBinaryCode) { emulator.DeltaCoeff = deltaCoeff; emulator.DeltaIndex = deltaIndex; emulator.DeltaSM = deltaSM; for (int x = 0; x < countNumbers; x++) { inputBinaryCode = new LongBits(x, n); outputBinaryCode = emulator.GetDKFromComparators(x); if (inputBinaryCode != outputBinaryCode) { break; } } if (inputBinaryCode == outputBinaryCode) { if (delta == DACEmulator.Delta.Coeff) { deltaCoeff += initialStep; } else if (delta == DACEmulator.Delta.Index) { deltaIndex += initialStep; } else if (delta == DACEmulator.Delta.SM) { deltaSM += initialStep; } } } if (delta == DACEmulator.Delta.Coeff) { deltaCoeff -= initialStep; } else if (delta == DACEmulator.Delta.Index) { deltaIndex -= initialStep; } else if (delta == DACEmulator.Delta.SM) { deltaSM -= initialStep; } initialStep /= 2; } //Корректировка значений, если необходимо if (isAllowedValues) { if (delta == DACEmulator.Delta.Coeff) { emulator.DeltaCoeff -= initialStep * 2; } else if (delta == DACEmulator.Delta.Index) { emulator.DeltaIndex -= initialStep * 2; } else if (delta == DACEmulator.Delta.SM) { emulator.DeltaSM -= initialStep * 2; } } for (int x = 0; x < countNumbers; x++) { indexes.AddRange(emulator.GetEKPErrorFromComparators(x).ToList()); inputBinaryCode = new LongBits(x, n); outputBinaryCode = emulator.GetDKFromComparators(x); if (inputBinaryCode != outputBinaryCode) { container.ErrorIndexesFromInputAndOutputCodes.Add(x); } container.InputBinaryCodes.Add(inputBinaryCode); container.OutputBinaryCodes.Add(outputBinaryCode); } container.DeltaCoeff = emulator.DeltaCoeff; container.DeltaIndex = emulator.DeltaIndex; container.DeltaSM = emulator.DeltaSM; container.ComparatorsErrorIndexes = indexes.Distinct().ToArray(); return(container); }
/// <summary> /// Создание документа docx с формулами ai /// </summary> /// <param name="transpB">Массив состовляющих ЕПК</param> /// <param name="a">ДК</param> /// <param name="path">Путь к результирующему файлу</param> public void GenerateDocument(LongBits[] b, out LongBits[] a, string path) { application = new Word.Application { Visible = false }; try { document = application.Documents.Add(); document.Range(0, 0).PageSetup.Orientation = WdOrientation.wdOrientLandscape; LongBits[] transpB = MathProcessor.TransposeBitMatrix(b); int n = MathProcessor.GetN(transpB.Length + 1); a = new LongBits[n]; SetMaxValueBar(n); for (int i = n - 1; i >= 0; i--) { List <string> formules = new List <string>(); a[i] = new LongBits(transpB[0].Length); a[i] = ~a[i]; int kmax = (int)Math.Pow(2, n - 1 - i) - 1; for (int k = 0; k <= kmax; k++) { int tmin = (int)Math.Pow(2, i) * (2 * k + 1); int tmax = (int)Math.Pow(2, i + 1) * (k + 1) - 1; for (int t = tmin; t <= tmax; t++) { if (formules.Count == 0 || formules[formules.Count - 1].Length > 250) { formules.Add(string.Empty); } formules[formules.Count - 1] += " b_" + t + notSign + " "; a[i] &= ~transpB[t - 1]; } } a[i] = ~a[i]; document.Range(0, 0).Text += "\n "; for (int index = formules.Count - 1; index >= 0; index--) { document.Range(0, 0).Text += "\n "; BuildFormula(notSign); BuildFormula(")", WdColor.wdColorWhite); BuildFormula(formules[index]); BuildFormula("(", WdColor.wdColorWhite); if (index == 0) { BuildFormula("a_" + i + "="); } else { BuildFormula(" "); } document.OMaths.BuildUp(); } PerformStepBar(); } document.SaveAs(path); } finally { //Закрывание ворда document.Close(); application.Quit(); } }
private void dataGridViewVect_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex < 0 || (e.ColumnIndex != 1 && e.ColumnIndex != 3)) { return; } DataGridView dataGridView = sender as DataGridView; LongBits inCode = dataGridView.Rows[e.RowIndex].Cells[0].Value as LongBits; LongBits binaryCode = dataGridView.Rows[e.RowIndex].Cells[1].Value as LongBits; int[] difference = LongBits.GetStringIndexesOfDifferenceBits(inCode, binaryCode); //кастомная отрисовка ячеек Выхода if (e.ColumnIndex == 1) { e.PaintBackground(e.ClipBounds, true); Font font = e.CellStyle.Font; TextFormatFlags flags = TextFormatFlags.NoPadding | TextFormatFlags.VerticalCenter; string text = (string)e.FormattedValue; List <string> subStrings = new List <string>(); int curInd = 0; foreach (int ind in difference) { subStrings.Add(text.Substring(curInd, ind - curInd)); subStrings.Add(text.Substring(ind, 1)); curInd = ind + 1; } subStrings.Add(text.Substring(curInd, text.Length - curInd)); bool errorState = false; Size size; Rectangle curBox = new Rectangle(e.CellBounds.X + 3, e.CellBounds.Y - 1, 0, e.CellBounds.Height); for (int i = 0; i < subStrings.Count; i++) { Font curFont = errorState ? errorFont : font; Color curColor = errorState ? errorCellTextColor : e.CellStyle.ForeColor; size = TextRenderer.MeasureText(e.Graphics, subStrings[i], curFont, e.CellBounds.Size, flags); curBox = new Rectangle(curBox.X + curBox.Width, curBox.Y, size.Width, curBox.Height); TextRenderer.DrawText(e.Graphics, subStrings[i], curFont, curBox, curColor, flags); errorState = !errorState; } int cellWidth = curBox.Location.X - e.CellBounds.Location.X + curBox.Width + 5; dataGridViewVect.Columns[1].Width = Math.Max(dataGridViewVect.Columns[1].Width, cellWidth); e.Handled = true; } else if (e.ColumnIndex == 3) { e.PaintBackground(e.ClipBounds, true); bool error = difference.Length > 0; Font font = error ? errorFont : e.CellStyle.Font; Color color = error ? errorCellTextColor : e.CellStyle.ForeColor; TextFormatFlags flags = TextFormatFlags.NoPadding | TextFormatFlags.VerticalCenter; string text = (string)e.FormattedValue; Size size = TextRenderer.MeasureText(e.Graphics, text, font, e.CellBounds.Size, flags); Rectangle Box = new Rectangle(e.CellBounds.X + 3, e.CellBounds.Y - 1, size.Width, e.CellBounds.Height); TextRenderer.DrawText(e.Graphics, text, font, Box, color, flags); int cellWidth = e.CellBounds.Location.X - e.CellBounds.Location.X + Box.Width + 8; dataGridViewVect.Columns[3].Width = Math.Max(dataGridViewVect.Columns[3].Width, cellWidth); e.Handled = true; } }