文字框回车 [英] TextBox Carriage Return

查看:76
本文介绍了文字框回车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我想知道是否有任何新人如何从Multiline设置为True的.Net TextBox(在PC应用程序中)检索运输退回单?
因此,如果TextBox文本如下所示:

你好

世界

我想知道C/R在那里并将它们存储到数据库中,以便在重新显示它们的地方.

目前我不使用RichTextBox,因为我存储的数据应该很简单,并且保存到数据库中的字段长度为100个字符(不要问我是否要这个更大的; O)).

无论如何,如果有人可以提供帮助,那就太好了!

干杯

水壶


-------------------------------------------------- -------
来自OP:

谢谢大家的即时答复!还不在那里.

我们正在使用Alpha(VMS)数据库,该数据库不具备与SQL相同的功能,并且存储各种类型的字符也无济于事(Chr(13),``\ r''等.).

我正在考虑将自己的特殊字符存储在字符串中,同时循环遍历TextBox的".Lines"属性,然后在读出后以某种方式在文本框中插入换行符并丢弃该字符. br/>
还是谢谢你

J

Hi I was wondering if anyone new how to retrieve a Carriage Return from a .Net TextBox (in a PC App.) that has Multiline set to True?
So if the TextBox text looks like this:

hello

world

I''d like to know that the C/R''s are there and store them (to database) as such, so that on redisplaying they are there.

For the moment I''m not using a RichTextBox as the data I''m storing is supposed to be simple and the field in the DB that it''s saving to is 100 chars long (don''t ask me to make this bigger ;O) ).

Anyway, if anyone can help, that would be great!

Cheers

Jugs


---------------------------------------------------------
From OP:

Thank you all for your immediate answers! Not quite there yet.

We''re using an Alpha (VMS) database, which doesn''t have the same facilities as SQL, and storing various types of characters doesn''t help yet either (Chr(13), ''\r'' etc.).

I''m thinking of storing my own special characters in the string whilst looping through the ''.Lines'' property of the TextBox, and then on reading back out somehow insert a line break in the text box and discard the character.

Thanks anyway

J

推荐答案

将文本框的AcceptsReturn设置为true

问候
Espen Harlinn
Set AcceptsReturn of the TextBox to true

Regards
Espen Harlinn


按照Espen的建议进行操作,然后执行以下操作:

Do as Espen suggested and then go somewhat like this:

...
    String myTextBoxTextWithCarriageReturns = textBox1.Text;
    bool success = WriteTextBoxData(someConnectionString, myTextBoxTextWithCarriageReturns);
...

private bool WriteTextBoxData(String connectionString, String text)
{
    bool retVal = true;
    string queryString = "INSERT INTO TextData text VALUES(@text)";
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Parameters.Add(new SqlParameter("text", SqlDbType.VarChar,100).Value = text);
        try
        {
            connection.Open();
            command.ExectueNonQuery();
        }
        catch(Exception ex)
        {
             //ToDo: Add something here: i.e. log exception
             retVal = false;
        }
        finally
        {
             connection.Close();
        }
    }
    return retVal;
}



我的代码假设数据库中的字段为varchar(100)类型,因此可能需要进行一些调整(即主键等).即使通过SqlParameters类传递参数,即使文本中包含回车符,此代码也可以工作.此类进行正确的编码,以便生成有效的SQL.如果手动构建SQL语句,则必须自己对CR进行编码.

希望对您有所帮助!

最好的问候,
Manfred



My code assumes that the field in the DB is type varchar(100), so some adjustments might have to be made (i.e. primary key etc.). This code works even if the text has carriage returns in it as the parameter is passed with the SqlParameters class. This class does the right encoding so that valid SQL is generated. If you build the SQL statement by hand you would have to encode the CRs yourself.

Hope that helps!

Best Regards,
Manfred


[ ^ ]文章可能会对您有所帮助.
This[^] article might help you.


这篇关于文字框回车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆