C#Combobox调用comboboxitem

时间:2017-11-11 09:17:29

标签: c#

我正在开发一个可以阻止和允许程序的Windows应用程序。我有一个关于如何调用组合框的问题。 inc是组合框1内的一个项目,所有项目都是组合框2中的项目。我想创建一个条件,如果选择了两个组合框,则运行此项。我怎么做 ?谢谢

      private void addSubmitBtn_Click(object sender, RoutedEventArgs e)
    {
        foreach (string path in pathT.Text.Split(';'))
            if (File.Exists(path))
            {
                if ( inc.SelectedIndex > -1 && all.SelectedIndex > -1)
                {
                    FWRule(path, NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN, (blo.Checked) ? NET_FW_ACTION_.NET_FW_ACTION_BLOCK : NET_FW_ACTION_.NET_FW_ACTION_ALLOW, ((Control)sender).Tag.ToString());
                }
                if (outg.Checked || all.Checked)
                {
                    FWRule(path, NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT, (blo.Checked) ? NET_FW_ACTION_.NET_FW_ACTION_BLOCK : NET_FW_ACTION_.NET_FW_ACTION_ALLOW, ((Control)sender).Tag.ToString());
                }
            }
        ((System.Windows.Controls.Panel)this.Parent).Children.Remove(this);
    }

    private void FWRule(string path, NET_FW_RULE_DIRECTION_ d, NET_FW_ACTION_ fwaction, string action)
    {
        try
        {
            INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(
            Type.GetTypeFromProgID("HNetCfg.FWRule"));
            firewallRule.Action = fwaction;
            firewallRule.Enabled = true;
            firewallRule.InterfaceTypes = "All";
            firewallRule.ApplicationName = path;
            firewallRule.Name = "PRST: " + System.IO.Path.GetFileName(path);
            INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
            firewallRule.Direction = d;
            if (action == "1") firewallPolicy.Rules.Add(firewallRule);
            else
            {
                ((System.Windows.Controls.Panel)this.Parent).Children.Remove(this);
            }
        }
        catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message, "ERROR"); }
    }

    private void addCancelBtn_Click(object sender, RoutedEventArgs e)
    {
        ((System.Windows.Controls.Panel)this.Parent).Children.Remove(this);
    }

    private void addBackBtn_Click(object sender, RoutedEventArgs e)
    {
        ((System.Windows.Controls.Panel)this.Parent).Children.Remove(this);
    }

    private void pathT_TextChanged(object sender, TextChangedEventArgs e)
    {

    }

    private void blo_CheckedChanged(object sender, EventArgs e)
    {

    }

    private void al_CheckedChanged(object sender, EventArgs e)
    {

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog fd = new OpenFileDialog();
        fd.Filter = "Executable|*.exe";
        fd.Multiselect = true;
        if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            pathT.Text = String.Join(";", fd.FileNames);
        }
    }


}

}

这是XAML部分

    <UserControl x:Class="WpfDeepTest.Views.policyAddFunc"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:md="http://materialdesigninxaml.net/winfx/xaml/themes"
         xmlns:local="clr-namespace:WpfDeepTest.Views"
         mc:Ignorable="d" 
         d:DesignHeight="700" d:DesignWidth="960">


<Grid Background="{DynamicResource MaterialDesignPaper}">
    <Grid.RowDefinitions>
        <RowDefinition Height=".5*"/>
        <RowDefinition Height="3*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height=".5*"/>
    </Grid.RowDefinitions>

    <Grid Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="20 0 0 0">
        <Button Name="addBackBtn" Style="{DynamicResource MaterialDesignFloatingActionButton}" Width="40" Height="40" Click="addBackBtn_Click">
            <md:PackIcon Kind="ChevronLeft" Height="30" Width="30"/>
        </Button>
    </Grid>

    <md:Card Grid.Row="1" Margin="15 10" Padding="100 0" Height="400">
        <StackPanel VerticalAlignment="Center">
            <Grid Margin="0 0 0 0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="1.5*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="Application Path: " VerticalAlignment="Center" FontSize="25" Foreground="#616161" Padding="10 0 0 0"/>
                <TextBox Name="pathT" Grid.Column="1" Background="#F5F5F5" VerticalContentAlignment="Center" FontSize="20" md:HintAssist.Hint="Ex: InboundPolicy#1" Padding="5 0" Margin="0,0,57.2,10.2" TextChanged="TextBox_TextChanged_1"/>
                <Button Content="..." Grid.Column="1" HorizontalAlignment="Left" Margin="372,-1,0,0" VerticalAlignment="Top" Width="47" Height="36" Click="Button_Click"/>
            </Grid>

            <Grid Margin="0 30 0 0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="1.5*"/>
                </Grid.ColumnDefinitions>

            </Grid>

            <Grid Margin="0 30 0 0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="280*"/>
                    <ColumnDefinition Width="138*"/>
                    <ColumnDefinition Width="13*"/>
                    <ColumnDefinition Width="31*"/>
                    <ColumnDefinition Width="66*"/>
                    <ColumnDefinition Width="171*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="Directions: " VerticalAlignment="Center" FontSize="25" Foreground="#616161" Padding="10 0 0 0" Margin="0,-25,0,25"/>
                <ComboBox x:Name="Combobox1"  Grid.Column="1" Background="#F5F5F5" VerticalContentAlignment="Center" FontSize="20" Padding="5 0" Margin="0,-31,0.2,39" Grid.ColumnSpan="5">
                    <ComboBoxItem Name="all" Content="All" HorizontalAlignment="Left" Width="417.6"/>
                    <ComboBoxItem  Name="inc" IsSelected="True" >OutBound</ComboBoxItem>
                    <ComboBoxItem Name="outg">InBound</ComboBoxItem>
                </ComboBox>
            </Grid>

            <Grid Margin="0 30 0 0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="280*"/>
                    <ColumnDefinition Width="108*"/>
                    <ColumnDefinition Width="61*"/>
                    <ColumnDefinition Width="163*"/>
                    <ColumnDefinition Width="87*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="Action: " VerticalAlignment="Center" FontSize="25" Foreground="#616161" Padding="10 0 0 0" Grid.ColumnSpan="2" Margin="2,-34,106,32.4"/>
                <ComboBox x:Name="Combobox2" SelectedItem="" Grid.Column="1" Background="#F5F5F5" VerticalContentAlignment="Center" FontSize="20" Padding="5 0" Margin="2,-30,-1.8,36.4" Grid.ColumnSpan="4">
                    <ComboBoxItem Name="blo" IsSelected="True">Block</ComboBoxItem>
                    <ComboBoxItem Name="al">Allow</ComboBoxItem>
                </ComboBox>
            </Grid>
        </StackPanel>
    </md:Card>

    <StackPanel Orientation="Horizontal" Grid.Row="2" HorizontalAlignment="Center">
        <Button Name="addSubmitBtn" Style="{DynamicResource MaterialDesignRaisedButton}" VerticalAlignment="Top" Margin="10 20" Content="Submit" Width="150" Height="35" TabIndex="1" Click="addSubmitBtn_Click"/>
        <Button Name="addCancelBtn" Style="{DynamicResource MaterialDesignRaisedLightButton}" VerticalAlignment="Top" Margin="10 20" Content="Cancel" Width="150" Height="35" Click="addCancelBtn_Click"/>
    </StackPanel>
</Grid>

这是该程序的屏幕截图 enter image description here

1 个答案:

答案 0 :(得分:0)

<强>更新

可以选择组合框项目但是没有选中吗?你应该检查它们是否只被选中。除非您构建自己的控件,其中包含可以检查的复选框项目的组合框

我认为我发现了你的问题,在你的情况下,情况永远不会发生因为inc和所有都是<ComboBox x:Name="Combobox1"的一部分,而你的代码中你说if ( inc.SelectedIndex > -1 && all.SelectedIndex > -1)这两个不可能在同一时间选择;)

例如,这应该有效,因为它有一个OR运算符if (outg.Checked || all.Checked),只应该选择其中一个。

忽略以下部分,是一个解决方案,认为您使用的是Win Forms

您可以使用selectedIndexChanged

就像这样:

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.getValues(this.comboBox2, this.comboBox3);

        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.getValues(this.comboBox2, this.comboBox3);
        }

        public void compareValues(String value1, String value2)
        {
            if(value1.Equals("inc") && value2.Equals("all"))
            {
                this.runThis();
            }
        }

        public void runThis()
        {
            //do your thing here :)
            this.label1.Text = "run this!!!";
        }

        public void getValues(ComboBox cmb2, ComboBox cmb3)
        {
            String cmb1Value = "";
            String cmb2Value = "";

            try{
                cmb1Value = this.comboBox2.SelectedItem.ToString();
            }catch (Exception exception){
                //not both set
            }

            try
            {
                cmb2Value = this.comboBox3.SelectedItem.ToString();
            }catch (Exception exception){
                //not both set
            }

            this.compareValues(cmb1Value, cmb2Value);
        }

上述代码示例,运行:

enter image description here

相关问题