简体   繁体   English

文本框保存最后输入的值

[英]TextBox hold last entered value

I have a project with Form and WPF(xaml) that popup with button click in the Form.我有一个带有表单和 WPF(xaml) 的项目,该项目通过在表单中单击按钮弹出。 The WPF have TextBoxes that the user can enter some values then to click to use these values in my project. WPF 具有文本框,用户可以输入一些值,然后单击以在我的项目中使用这些值。 what Im trying to achieve is that in the first call the user enter values and if he call it again, the TextBoxes shows the last entered values.我试图实现的是,在第一次调用中,用户输入值,如果他再次调用它,TextBoxes 会显示最后输入的值。 Also I want to prevent the user from entered value for example here less than 1.另外我想防止用户输入例如小于 1 的值。

Here is my WPF code:这是我的 WPF 代码:

<Window x:Class="Alarms.AlarmsFC_Popup"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Alarms"
        mc:Ignorable="d"
        Title="Alarms" Height="150" Width="525" Background="LightSteelBlue" Icon="warningred.ico">
    <Border Padding="10">
    <StackPanel>
            <!--IO Fields-->
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <!--Begin Alarm-->
                <StackPanel Grid.Column="0" Margin="0 0 10 0">
                    <TextBlock Text="Number From" FontWeight="Black"/>
                    <TextBox x:Name="BeginAlarmBox1" Padding="1" Margin="0 5 0 0" Background="White" Text="1"></TextBox>
                </StackPanel>

                <!--End Alarm-->
                <StackPanel Grid.Column="1" Margin="0 0 0 0">
                    <TextBlock Text="Number To" FontWeight="Black"/>
                    <TextBox x:Name="EndAlarmBox1" Padding="1" Margin="0 5 0 0" Background="White" Text="32"></TextBox>
                </StackPanel>
            </Grid>

            <!--Buttons-->
            <Grid Margin="0,10,0,0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Button Name="BtnExport" Content="Export" Grid.Column="0" Margin="0,10,10,0" Padding="8" Click="BtnExport_Click" />
                <Button Name="BtnCancel" Content="Cancel" Grid.Column="1" Margin="10,10,0,0" Padding="8" Click="BtnCancel_Click" />
            </Grid>

        </StackPanel>
    </Border>
</Window>

And the code behind以及背后的代码

public partial class AlarmsFC_Popup : Window
    {
        public AlarmsFC_Popup(int BAlarm)
        {
            InitializeComponent();
            BeginAlarm = BAlarm;
        }
        public int BeginAlarm
        {
            get { return int.Parse(BeginAlarmBox1.Text); }
            set { 
                BeginAlarmBox1.Text = value.ToString();
                if (value < 1) MessageBox.Show("Value has to be 1 or higher!");
                }
        }
        private void BtnExport_Click(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrEmpty(BeginAlarmBox1.Text)) && (!String.IsNullOrEmpty(EndAlarmBox1.Text))
            {
                BeginAlarm = int.Parse(BeginAlarmBox1.Text);

                    MessageBox.Show(String.Format("Your for loop start at {0}", BeginAlarm.ToString()));
                    this.Close();
            }
            else
            {
                MessageBox.Show("One or more fields are empty. Fill all fields then press Export!");
            }

        }
        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show("You clicked me at " + e.GetPosition(this).ToString());
        }
    }

Then I call it in my main Form然后我在我的主窗体中调用它

private void BtnAlarms_Click(object sender, EventArgs e)
        {

            AlarmsFC_Popup Alarms = new AlarmsFC_Popup(1, 32);
            Alarms .ShowDialog();
            int FirstAlarm = Alarms .BeginAlarm;
        }

What I'm doing wrong?我做错了什么? why it doesn't hold my last entered value, instead it always shows me 1 for the begin alarm为什么它不保存我最后输入的值,而是总是显示 1 表示开始警报

You need to store the value somewhere, otherwise it goes away once you close the window.您需要将值存储在某处,否则一旦关闭 window,它就会消失。

You could for example do this using a private field in the main window:例如,您可以使用主 window 中的私有字段来执行此操作:

int _beginAlarm = 1;
private void BtnAlarms_Click(object sender, EventArgs e)
{
    AlarmsFC_Popup Alarms = new AlarmsFC_Popup(_beginAlarm, 32);
    Alarms.ShowDialog();
    _beginAlarm = Alarms.BeginAlarm;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM