StringFormat.ToString 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将此 StringFormat 对象转换为可读的字符串。
public:
override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String
返回
此 StringFormat 对象的字符串表示形式。
示例
下面的示例旨在与 Windows 窗体 一起使用,它需要 PaintEventArgse
,它是 事件处理程序的Paint一个参数。 此代码执行以下操作:
将 StringFormat 对象转换为字符串。
绘制字符串。
更改 对象中的 StringFormat 某些属性。
绘制字符串。 字符串将不同,因为对象的属性 StringFormat 已更改。
void ToStringExample( PaintEventArgs^ e )
{
Graphics^ g = e->Graphics;
SolidBrush^ blueBrush = gcnew SolidBrush( Color::FromArgb( 255, 0, 0, 255 ) );
System::Drawing::Font^ myFont = gcnew System::Drawing::Font( "Times New Roman",14 );
StringFormat^ myStringFormat = gcnew StringFormat;
// String variable to hold the values of the StringFormat object.
String^ strFmtString;
// Convert the string format object to a string (only certain information
// in the object is converted) and display the string.
strFmtString = myStringFormat->ToString();
g->DrawString( String::Format( "Before changing properties: {0}", myStringFormat ), myFont, blueBrush, 20, 40 );
// Change some properties of the string format
myStringFormat->Trimming = StringTrimming::None;
myStringFormat->FormatFlags = (StringFormatFlags)(StringFormatFlags::NoWrap | StringFormatFlags::NoClip);
// Convert the string format object to a string and display the string.
// The string will be different because the properties of the string
// format have changed.
strFmtString = myStringFormat->ToString();
g->DrawString( String::Format( "After changing properties: {0}", myStringFormat ), myFont, blueBrush, 20, 70 );
}
public void ToStringExample(PaintEventArgs e)
{
Graphics g = e.Graphics;
SolidBrush blueBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));
Font myFont = new Font("Times New Roman", 14);
StringFormat myStringFormat = new StringFormat();
// String variable to hold the values of the StringFormat object.
string strFmtString;
// Convert the string format object to a string (only certain information
// in the object is converted) and display the string.
strFmtString = myStringFormat.ToString();
g.DrawString("Before changing properties: " + myStringFormat,
myFont, blueBrush, 20, 40);
// Change some properties of the string format
myStringFormat.Trimming = StringTrimming.None;
myStringFormat.FormatFlags = StringFormatFlags.NoWrap
| StringFormatFlags.NoClip;
// Convert the string format object to a string and display the string.
// The string will be different because the properties of the string
// format have changed.
strFmtString = myStringFormat.ToString();
g.DrawString("After changing properties: " + myStringFormat,
myFont, blueBrush, 20, 70);
}
Public Sub ToStringExample(ByVal e As PaintEventArgs)
Dim g As Graphics = e.Graphics
Dim blueBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
Dim myFont As New Font("Times New Roman", 14)
Dim myStringFormat As New StringFormat
' String variable to hold the values of the StringFormat object.
Dim strFmtString As String
' Convert the string format object to a string (only certain
' information in the object is converted) and display the string.
strFmtString = myStringFormat.ToString()
g.DrawString("Before changing properties: ", myFont, blueBrush, _
20, 40, myStringFormat)
' Change some properties of the string format.
myStringFormat.Trimming = StringTrimming.None
myStringFormat.FormatFlags = StringFormatFlags.NoWrap Or _
StringFormatFlags.NoClip
' Convert the string format object to a string and display the
' string. The string will be different because the properties of
' the string format have changed.
strFmtString = myStringFormat.ToString()
g.DrawString("After changing properties: ", myFont, blueBrush, _
20, 70, myStringFormat)
End Sub
注解
仅转换 属性的值 FormatFlags 。
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈