gpt4 book ai didi

C# Combobox调用comboboxitem

转载 作者:太空宇宙 更新时间:2023-11-03 14:58:59 28 4
gpt4 key购买 nike

我正在开发一个 Windows 应用程序,您可以在其中阻止和允许程序。我对如何调用组合框项目有疑问。 inc 是组合框 1 中的一个项目,而 all 是组合框 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

最佳答案

更新

组合框项目可以选择但不能勾选对吗?您应该检查是否仅选择了它们。除非您构建自己的控件,该控件具有可以选中的复选框项目的组合框

我想我发现了你的问题,在你的 if 条件下,这种情况永远不会发生,因为 inc 和 all 都是 <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

关于C# Combobox调用comboboxitem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47236295/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com