PDA

View Full Version : سوال: ساخت دکمه bold



omid.s_net
دوشنبه 08 اسفند 1390, 16:43 عصر
سلام وخسته نباشید
من دارم یه editor درست میکنم 3تا button گذاشتم که یکی bold یکی italic یکی underline می خواهم وقتی قسمتی ازمتن روانتخاب کردم وروی یکی ازاین 3گزینه کلیک کردم متن عوض بشه لطفا کمکم کنیدوبگیدچه کدی بایددراین دکمه هابزنم،ممنون

b.saminjad
دوشنبه 08 اسفند 1390, 21:36 عصر
سلام



System.Drawing.Font x= newFont(richTextBox1.Font.ToString(),richTextBox1. Font.Size,FontStyle.Bold );


if (richTextBox1.SelectionFont.Bold == false)

richTextBox1.SelectionFont = x;


else

{

x =
newFont(richTextBox1.Font.ToString(), richTextBox1.Font.Size, FontStyle.Regular);

richTextBox1.SelectionFont = x;

}



این واسه ضخیم شدن متن بود و کج شدن هم italic بنویس

rezayeman
دوشنبه 23 مرداد 1391, 15:25 عصر
این کد مشکل داره و مشکلش اینه که نمی تونی همزمان هم bold داشته باشی و هم italic در هر زمان فقط یکی از این خاصیتها را می توان داشت

از دوستان اگر کسی می تونه لطفا کمک کنه
ممنون

Mahmoud.Afrad
دوشنبه 23 مرداد 1391, 16:26 عصر
باید در قسمت استایل مواردی که می خواهید را با هم OR منطقی کنید مثلا FontStyle.Bold | FontStyle.Italic

rezayeman
سه شنبه 24 مرداد 1391, 08:13 صبح
جواب شما درست بود ولی وقتی بخوایم با 3 دکمه bold,italic,underline کار کنیم باید کلی شرط و شروط بزاریم

آیا راه حل بهتری هست؟

اگه کد این 3 دکمه رو بزارید ممنون میشم

rezayeman
سه شنبه 24 مرداد 1391, 13:22 عصر
کد رو بلدم می خواستم ببینم راه ساده تر نداره که این همه شرط نزاریم؟

if (txtDetail.SelectionFont.Bold)
{
if (txtDetail.SelectionFont.Italic & txtDetail.SelectionFont.Underline)
txtDetail.SelectionFont = new Font(txtDetail.SelectionFont, FontStyle.Italic | FontStyle.Underline);
else if (txtDetail.SelectionFont.Italic)
txtDetail.SelectionFont = new Font(txtDetail.SelectionFont, FontStyle.Italic);
else if (txtDetail.SelectionFont.Underline)
txtDetail.SelectionFont = new Font(txtDetail.SelectionFont, FontStyle.Underline);
else
txtDetail.SelectionFont = new Font(txtDetail.SelectionFont, FontStyle.Regular);

}
else
{
if (txtDetail.SelectionFont.Italic & txtDetail.SelectionFont.Underline)
txtDetail.SelectionFont = new Font(txtDetail.SelectionFont, FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
else if (txtDetail.SelectionFont.Italic)
txtDetail.SelectionFont = new Font(txtDetail.SelectionFont, FontStyle.Bold | FontStyle.Italic);
else if (txtDetail.SelectionFont.Underline)
txtDetail.SelectionFont = new Font(txtDetail.SelectionFont, FontStyle.Bold | FontStyle.Underline);
else
txtDetail.SelectionFont = new Font(txtDetail.SelectionFont, FontStyle.Bold);

}
txtDetail.Focus();

Mahmoud.Afrad
سه شنبه 24 مرداد 1391, 13:50 عصر
نه دیگه باید همه حالاتو در نظر بگیرید

من به اینصورت نوشتم
bool bold, italic, underline;
private void btnBold_Click(object sender, EventArgs e)
{
bold = !bold;
selectionFontSet();
}

private void btnItalic_Click(object sender, EventArgs e)
{
italic = !italic;
selectionFontSet();
}

private void btnUnderLine_Click(object sender, EventArgs e)
{
underline = !underline;
selectionFontSet();
}

private void selectionFontSet()
{
richTextBox1.HideSelection = false;

if (bold && !italic && !underline)
richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold);
else if (!bold && italic && !underline)
richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Italic);
else if (!bold && !italic && underline)
richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Underline);
else if (bold && italic && !underline)
richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold | FontStyle.Italic);
else if (bold && !italic && underline)
richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold | FontStyle.Underline);
else if (!bold && italic && underline)
richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Italic | FontStyle.Underline);
else if (bold && italic && underline)
richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
else
richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Regular);
}

rezayeman
سه شنبه 24 مرداد 1391, 15:02 عصر
منم فقط مال bold رو نوشتم
ببخشید کل کدو نذاشته بودم

private void btnBold_Click(object sender, EventArgs e)
{
if (txtDetail.SelectionFont.Bold)
{
if (txtDetail.SelectionFont.Italic & txtDetail.SelectionFont.Underline)
txtDetail.SelectionFont = new Font(txtDetail.SelectionFont, FontStyle.Italic | FontStyle.Underline);
else if (txtDetail.SelectionFont.Italic)
txtDetail.SelectionFont = new Font(txtDetail.SelectionFont, FontStyle.Italic);
else if (txtDetail.SelectionFont.Underline)
txtDetail.SelectionFont = new Font(txtDetail.SelectionFont, FontStyle.Underline);
else
txtDetail.SelectionFont = new Font(txtDetail.SelectionFont, FontStyle.Regular);
}
else
{
if (txtDetail.SelectionFont.Italic & txtDetail.SelectionFont.Underline)
txtDetail.SelectionFont = new Font(txtDetail.SelectionFont, FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
else if (txtDetail.SelectionFont.Italic)
txtDetail.SelectionFont = new Font(txtDetail.SelectionFont, FontStyle.Bold | FontStyle.Italic);
else if (txtDetail.SelectionFont.Underline)
txtDetail.SelectionFont = new Font(txtDetail.SelectionFont, FontStyle.Bold | FontStyle.Underline);
else
txtDetail.SelectionFont = new Font(txtDetail.SelectionFont, FontStyle.Bold);
}
txtDetail.Focus();
}


اینطوری احتیاج به تعریف متغیر سراسری هم نداره

واسه italic و underline هم همین تکرار میشه

uniqueboy_ara
سه شنبه 24 مرداد 1391, 17:24 عصر
این کلاسی که ضمیمه کردمیه UserControl هستش که هر 3تا کار رو به خوبی انجام میده ( Bold,Italic,Underline ) :
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MailUsers
{
class ARA_RichTextBox:RichTextBox
{

public void ToggleBold()
{
if (SelectionFont.Underline)
{
if (SelectionFont.Italic)
{
if (!SelectionFont.Bold)
SelectionFont = new Font(SelectionFont, FontStyle.Italic | FontStyle.Bold | FontStyle.Underline);
else

SelectionFont = new Font(SelectionFont, FontStyle.Italic | FontStyle.Underline);
}
else
{
if (!SelectionFont.Bold)
SelectionFont = new Font(SelectionFont, FontStyle.Bold | FontStyle.Underline);
else

SelectionFont = new Font(SelectionFont, FontStyle.Underline);
}
}
else
{
if (SelectionFont.Italic)
{
if (!SelectionFont.Bold)
SelectionFont = new Font(SelectionFont, FontStyle.Italic | FontStyle.Bold);
else

SelectionFont = new Font(SelectionFont, FontStyle.Italic);
}
else
{
if (!SelectionFont.Bold)
SelectionFont = new Font(SelectionFont, FontStyle.Bold);
else

SelectionFont = new Font(SelectionFont, FontStyle.Regular);
}
}
}
public void ToggleItalic()
{

if (SelectionFont.Underline)
{
if (SelectionFont.Bold)
{
if (!SelectionFont.Italic)
SelectionFont = new Font(SelectionFont, FontStyle.Italic | FontStyle.Bold | FontStyle.Underline);
else

SelectionFont = new Font(SelectionFont, FontStyle.Bold | FontStyle.Underline);
}
else
{
if (!SelectionFont.Italic)
SelectionFont = new Font(SelectionFont, FontStyle.Italic | FontStyle.Underline);
else

SelectionFont = new Font(SelectionFont, FontStyle.Underline);
}
}
else
{
if (SelectionFont.Bold)
{
if (!SelectionFont.Italic)
SelectionFont = new Font(SelectionFont, FontStyle.Italic | FontStyle.Bold);
else

SelectionFont = new Font(SelectionFont, FontStyle.Bold);
}
else
{

if (!SelectionFont.Italic)
SelectionFont = new Font(SelectionFont, FontStyle.Italic);
else

SelectionFont = new Font(SelectionFont, FontStyle.Regular);
}
}
}
public void ToggleUnderLine()
{
if (SelectionFont.Bold)
{
if (SelectionFont.Italic)
{
if (!SelectionFont.Underline)
SelectionFont = new Font(SelectionFont, FontStyle.Italic | FontStyle.Bold | FontStyle.Underline);
else
SelectionFont = new Font(SelectionFont, FontStyle.Italic | FontStyle.Bold);
}
else
{
if (!SelectionFont.Underline)
SelectionFont = new Font(SelectionFont, FontStyle.Bold | FontStyle.Underline);
else

SelectionFont = new Font(SelectionFont, FontStyle.Bold);
}
}
else
{
if (SelectionFont.Italic)
{
if (!SelectionFont.Underline)
SelectionFont = new Font(SelectionFont, FontStyle.Italic | FontStyle.Underline);
else

SelectionFont = new Font(SelectionFont, FontStyle.Italic);
}
else
{
if (!SelectionFont.Underline)
SelectionFont = new Font(SelectionFont, FontStyle.Underline);
else

SelectionFont = new Font(SelectionFont, FontStyle.Regular);
}
}
}
}
}

.
.
91259