반응형

 

목표

 

사용자가 TextBox 에 값 입력하기위해 마우스커서 올려놓았을때 자동으로 값이 Clear 되고
새로운 값을 입력하지 않은 채 커서를 옮기면 이전에 입력해놓은 값이 유지되는 기능

 

 

 

 

 

방법

 

1. 마우스 포인터가 컨트롤의 영역으로 들어왔을때 "" 값을 임의로 넣어준다.

2. TextBox 안의 내용에 변화를 감지하여 아무런 변화가 없다면 미리 저장해 놓은 이전의 값을 저장

 

 

xaml

<TextBox x:Name="tbx_Value1" Grid.Row="0" Grid.Column="1" Height="25" GotFocus="Tbx_Value1_GotFocus" LostFocus="Tbx_Value1_LostFocus" TextChanged="Tbx_Value1_TextChanged"/>
<TextBox x:Name="tbx_Value2" Grid.Row="1" Grid.Column="1" Height="25" GotFocus="Tbx_Value2_GotFocus" LostFocus="Tbx_Value2_LostFocus" TextChanged="Tbx_Value2_TextChanged"/>
<TextBox x:Name="tbx_Value3" Grid.Row="2" Grid.Column="1" Height="25" GotFocus="Tbx_Value3_GotFocus" LostFocus="Tbx_Value3_LostFocus" TextChanged="Tbx_Value3_TextChanged"/>

xaml.cs

    public partial class TextboxEx_Window : Window
    {
        private ElementId LineId;
        private Document doc;
        string Value1 = "0";
        string Value2 = "0";
        string Value3 = "0";

        public TextboxEx_Window(ExternalCommandData commandData, ElementId Id, Element element)
        {
            InitializeComponent();
            LineId = Id;
            doc = commandData.Application.ActiveUIDocument.Document;
            SetValue();
        }

        private void SetValue()
        {
            tbx_Value1.Text = "0";
            tbx_Value2.Text = "0";
            tbx_Value3.Text = "0";
        }

        // TextBox 안의 내용에 변화가 생겼을때
        private void Tbx_Value1_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (tbx_Value1.Text == "0") return;
            if (!Value1.Equals(tbx_Value1.Text) && !tbx_Value1.Text.Equals(""))
            { Value1 = tbx_Value1.Text; }
        }
        private void Tbx_Value2_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (tbx_Value2.Text == "0") return;
            if (!Value2.Equals(tbx_Value2.Text) && !tbx_Value2.Text.Equals(""))
            { Value2 = tbx_Value2.Text; }
        }
        private void Tbx_Value3_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (tbx_Value3.Text == "0") return;
            if (!Value3.Equals(tbx_Value3.Text) && !tbx_Value3.Text.Equals(""))
            { Value3 = tbx_Value3.Text; }
        }

        //  textbox 가 클릭되었을때 
        private void Tbx_Value1_GotFocus(object sender, MouseEventArgs e)
        {
            if (tbx_Value2.Text.Equals("")) { tbx_Value2.Text = Value2; }
            if (tbx_Value3.Text.Equals("")) { tbx_Value3.Text = Value3; }
            tbx_Value1.Text = "";
        }
        private void Tbx_Value2_GotFocus(object sender, MouseEventArgs e)
        {
            if (tbx_Value1.Text.Equals("")) { tbx_Value1.Text = Value1; }
            if (tbx_Value3.Text.Equals("")) { tbx_Value3.Text = Value3; }
            tbx_Value2.Text = "";
        }
        private void Tbx_Value3_GotFocus(object sender, MouseEventArgs e)
        {
            if (tbx_Value1.Text.Equals("")) { tbx_Value1.Text = Value1; }
            if (tbx_Value2.Text.Equals("")) { tbx_Value2.Text = Value2; }
            tbx_Value3.Text = "";
        }

        // textbox 가 클릭되지 않았을때 
        private void Tbx_Value1_LostFocus(object sender, MouseEventArgs e)
        { if (tbx_Value1.Text == "") { tbx_Value1.Text = Value1; }}
        private void Tbx_Value2_LostFocus(object sender, MouseEventArgs e)
        { if (tbx_Value2.Text == "") { tbx_Value2.Text = Value2; }}
        private void Tbx_Value3_LostFocus(object sender, MouseEventArgs e)
        { if (tbx_Value3.Text == "") { tbx_Value3.Text = Value3; }}
    }

 

반응형

+ Recent posts