请帮助纠正我的代码! [英] Help correct my codes please!

查看:143
本文介绍了请帮助纠正我的代码!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用文本框A,B,C& d;它们只共享一个LEAVE EVENT,我粘贴的那个。

I am working with text boxes A, B, C & D; they are all sharing only one LEAVE EVENT, the one I have pasted bellow.

private void txtA_Leave(object sender, EventArgs e)
        {
            string[] y = new string[] { "txtA", "txtB", "txtC", "txtD"};
            if (y.Contains(ActiveControl.Name) && ActiveControl.Text == string.Empty)
            {
                ActiveControl.Text = 0.0;
                ActiveControl.TextAlign = HorizontalAlignment.Center;
            }
        }

当我为B,C或D留下A时,我预计会执行 txtA_Leave 但它无法正常工作;有人帮我修改代码。



谢谢。

When I leave A for B, C or D, I expected txtA_Leave to execute but its not working; someone help me to amend the code please.

Thank you.

推荐答案

首先,你不能在 Text 属性中放置一个double,你需要在其中加一个字符串,例如0.0而不是 0.0



此外,使用发件人 ActiveControl >属性。



最好使用 String.IsNullOrEmpty 而不是 = = String.Empty 因为现在您还要检查字符串是否为空。如果你认为只有空白字符串为'空',那么使用 IsNullOrWhiteSpace



y 方法外的变量;这避免了每次调用方法时都会重新创建它。

First of all, you can't have put a double in the Text property, you need to put a string in it, for example "0.0" instead of 0.0.

Also, instead of ActiveControl, use the sender attribute.

It is also better to use String.IsNullOrEmpty instead of == String.Empty because now you also check whether the string is null. And if you consider a whitespace-only string as 'empty', then use IsNullOrWhiteSpace.

Put the y variable outside the method; this avoids that it gets recreated every time the method gets called.
string[] y = new string[] { "txtA", "txtB", "txtC", "txtD"};
private void txtA_Leave(object sender, EventArgs e)
        {
            if (!(sender is TextBox))
            {
                 return; // 'sender' cannot be converted to a TextBox
            }
            TextBox control = sender as TextBox;
            if (y.Contains(control.Name) && String.IsNullOrEmpty(control.Text))
            {
                control.Text = 0.0;
                control.TextAlign = HorizontalAlignment.Center;
            }
        }



有关发件人属性的更多信息:

http://stackoverflow.com/questions/1303145/net -events-what-are-object-sender-eventargs -e [ ^ ]


More information about the sender attribute:
http://stackoverflow.com/questions/1303145/net-events-what-are-object-sender-eventargs-e[^]


删除ActiveControl的东西:每个事件都会将你引发它的类的实例交给你 sender 参数。

所以:

Drop the ActiveControl stuff: every Event hands you the instance of the class that raised it as the sender parameter.
So:
private static string[] y = new string[] { "txtA", "txtB", "txtC", "txtD"};
private void txtA_Leave(object sender, EventArgs e)
    {
        TextBox tb = sender as TextBox;
        if (tb != null)
            {   
            if (y.Contains(tb.Text))
            {
                tb.Text = "0.0";
                tb.TextAlign = HorizontalAlignment.Center;
            }
        }


这篇关于请帮助纠正我的代码!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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