C# 向GroupBox问题添加对象
我正在尝试向GroupBox添加不同的对象。当我单击“添加标题”按钮时,我可以在Groupbox中显示组合框和文本框 我现在想做的是,当他们单击“添加问题”时,我希望能够将问题对象(组合框、文本框)添加到与上次添加的标题相同的分组框中。 C#代码: 就在下面C# 向GroupBox问题添加对象,c#,wpf,runtime,C#,Wpf,Runtime,我正在尝试向GroupBox添加不同的对象。当我单击“添加标题”按钮时,我可以在Groupbox中显示组合框和文本框 我现在想做的是,当他们单击“添加问题”时,我希望能够将问题对象(组合框、文本框)添加到与上次添加的标题相同的分组框中。 C#代码: 就在下面 public partial class CreateNewStandard : Page { 所以它对所有的方法都是可见的。然后我得到了这个错误 试试这个 private void btnAddTitle_Click(object
public partial class CreateNewStandard : Page
{
所以它对所有的方法都是可见的。然后我得到了这个错误
试试这个
private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{
CurrentSortItem++;
SortItems.Add(CurrentSortItem);
StackPanel outerSp = new StackPanel() { Orientation = Orientation.Vertical };
StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
gp = new GroupBox();
ComboBox y = new ComboBox();
y.Name = "Combo" + CurrentSortItem;
y.SelectedItem = CurrentSortItem;
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.Margin = new Thickness(20, 15, 0, 0);
foreach (int item in SortItems)
{
y.Items.Add(item);
}
TextBox x = new TextBox();
x.Name = "Title" + CurrentSortItem;
x.Text = "Title...";
x.FontWeight = FontWeights.Bold;
x.FontStyle = FontStyles.Italic;
x.TextWrapping = TextWrapping.Wrap;
x.Height = 25;
x.Width = 200;
x.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
x.Margin = new Thickness(12, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
outerSp.Children.Add(sp);
gp.Content = outerSp;
spStandard.Children.Add(gp);
}
private void ViewQuestions(StackPanel sp)
{
var stackPanel=gp.Content as StackPanel;
if(stackPanel!=null)
{
stackPanel.Children.Add(sp);
}
else
gp.Content=sp;
}
List<int> SortItems1 = new List<int>();
int CurrentSortItem1 = 0;
int Count = 0;
private void btnQuestion_Click(object sender, RoutedEventArgs e)
{
if (SortItems.Count == 0)
{
MessageBox.Show("You must add a title before adding a question", "ERROR", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
Count++;
CurrentSortItem1++;
SortItems1.Add(CurrentSortItem1);
ComboBox y = new ComboBox();
y.Name = "Combo" + CurrentSortItem1;
y.SelectedItem = CurrentSortItem1;
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.Margin = new Thickness(20, 15, 0, 0);
foreach (int item in SortItems1)
{
y.Items.Add(item);
}
TextBox x = new TextBox();
x.Name = "Question" + CurrentSortItem1;
x.Text = "Question...";
x.FontStyle = FontStyles.Italic;
x.TextWrapping = TextWrapping.Wrap;
x.Height = 25;
x.Width = 500;
x.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
x.AcceptsReturn = true;
x.Margin = new Thickness(100, 15, 0, 0);
TextBox z = new TextBox();
z.Name = "Points" + CurrentSortItem;
z.FontWeight = FontWeights.Bold;
z.Height = 25;
z.Width = 45;
z.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
z.Margin = new Thickness(250, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
sp.Children.Add(z);
outerSp.Children.Add(sp);
ViewQuestions(sp);
}
private void btnAddTitle\u单击(对象发送者,路由目标)
{
CurrentSortItem++;
SortItems.Add(当前sortitem);
StackPanel outerSp=new StackPanel(){Orientation=Orientation.Vertical};
StackPanel sp=new StackPanel(){Orientation=Orientation.Horizontal};
gp=新的分组框();
组合框y=新组合框();
y、 Name=“Combo”+CurrentSortItem;
y、 选择EdItem=CurrentSortItem;
y、 高度=25;
y、 宽度=45;
y、 HorizontalAlignment=System.Windows.HorizontalAlignment.Left;
y、 边缘=新厚度(20,15,0,0);
foreach(SortItems中的int项)
{
y、 项目。添加(项目);
}
TextBox x=新的TextBox();
x、 Name=“Title”+CurrentSortItem;
x、 Text=“Title…”;
x、 FontWeight=FontWeights.Bold;
x、 FontStyle=FontStyles.Italic;
x、 TextWrapping=TextWrapping.Wrap;
x、 高度=25;
x、 宽度=200;
x、 HorizontalAlignment=System.Windows.HorizontalAlignment.Left;
x、 边缘=新厚度(12,15,0,0);
sp.Children.添加(y);
sp.Children.添加(x);
外层子层添加(sp);
gp.内容=外包装;
spStandard.Children.Add(gp);
}
私人无效视图问题(StackPanel sp)
{
var stackPanel=gp.Content作为stackPanel;
如果(stackPanel!=null)
{
stackPanel.Children.Add(sp);
}
其他的
gp.含量=sp;
}
List SortItems1=新列表();
int CurrentSortItem1=0;
整数计数=0;
私有void b查询\单击(对象发送方,路由目标)
{
if(SortItems.Count==0)
{
Show(“在添加问题之前必须添加标题”,“错误”,MessageBoxButton.OK,MessageBoxImage.Information);
}
其他的
{
计数++;
CurrentSortItem1++;
SortItems1.Add(当前sortitem1);
组合框y=新组合框();
y、 Name=“Combo”+CurrentSortItem1;
y、 选择EdItem=CurrentSortItem1;
y、 高度=25;
y、 宽度=45;
y、 HorizontalAlignment=System.Windows.HorizontalAlignment.Left;
y、 边缘=新厚度(20,15,0,0);
foreach(SortItems1中的int项)
{
y、 项目。添加(项目);
}
TextBox x=新的TextBox();
x、 Name=“问题”+CurrentSortItem1;
x、 Text=“问题…”;
x、 FontStyle=FontStyles.Italic;
x、 TextWrapping=TextWrapping.Wrap;
x、 高度=25;
x、 宽度=500;
x、 HorizontalAlignment=System.Windows.HorizontalAlignment.Left;
x、 AcceptsReturn=true;
x、 余量=新厚度(100,15,0,0);
TextBox z=新的TextBox();
z、 Name=“Points”+CurrentSortItem;
z、 FontWeight=FontWeights.Bold;
z、 高度=25;
z、 宽度=45;
z、 HorizontalAlignment=System.Windows.HorizontalAlignment.Left;
z、 余量=新厚度(250,15,0,0);
sp.Children.添加(y);
sp.Children.添加(x);
sp.Children.添加(z);
外层子层添加(sp);
查看问题(sp);
}
您正在覆盖它。虽然您需要将它们添加到已存在的stackPanel。代码未经测试,我正在尝试给您一个想法。我希望这会有所帮助。这是用于覆盖的吗?因为我不希望它覆盖,我只希望它像在堆栈面板中一样显示在下面。但它需要在标题的组框中?不是吗标题的groupbox?gp是标题的groupbox,但当他们添加问题时,需要将其添加到gp。您是否复制并粘贴了上述代码。上述代码将其添加到标题groupbox,我希望gp是类级别字段。
public partial class CreateNewStandard : Page
{
private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{
CurrentSortItem++;
SortItems.Add(CurrentSortItem);
StackPanel outerSp = new StackPanel() { Orientation = Orientation.Vertical };
StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
gp = new GroupBox();
ComboBox y = new ComboBox();
y.Name = "Combo" + CurrentSortItem;
y.SelectedItem = CurrentSortItem;
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.Margin = new Thickness(20, 15, 0, 0);
foreach (int item in SortItems)
{
y.Items.Add(item);
}
TextBox x = new TextBox();
x.Name = "Title" + CurrentSortItem;
x.Text = "Title...";
x.FontWeight = FontWeights.Bold;
x.FontStyle = FontStyles.Italic;
x.TextWrapping = TextWrapping.Wrap;
x.Height = 25;
x.Width = 200;
x.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
x.Margin = new Thickness(12, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
outerSp.Children.Add(sp);
gp.Content = outerSp;
spStandard.Children.Add(gp);
}
private void ViewQuestions(StackPanel sp)
{
var stackPanel=gp.Content as StackPanel;
if(stackPanel!=null)
{
stackPanel.Children.Add(sp);
}
else
gp.Content=sp;
}
List<int> SortItems1 = new List<int>();
int CurrentSortItem1 = 0;
int Count = 0;
private void btnQuestion_Click(object sender, RoutedEventArgs e)
{
if (SortItems.Count == 0)
{
MessageBox.Show("You must add a title before adding a question", "ERROR", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
Count++;
CurrentSortItem1++;
SortItems1.Add(CurrentSortItem1);
ComboBox y = new ComboBox();
y.Name = "Combo" + CurrentSortItem1;
y.SelectedItem = CurrentSortItem1;
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.Margin = new Thickness(20, 15, 0, 0);
foreach (int item in SortItems1)
{
y.Items.Add(item);
}
TextBox x = new TextBox();
x.Name = "Question" + CurrentSortItem1;
x.Text = "Question...";
x.FontStyle = FontStyles.Italic;
x.TextWrapping = TextWrapping.Wrap;
x.Height = 25;
x.Width = 500;
x.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
x.AcceptsReturn = true;
x.Margin = new Thickness(100, 15, 0, 0);
TextBox z = new TextBox();
z.Name = "Points" + CurrentSortItem;
z.FontWeight = FontWeights.Bold;
z.Height = 25;
z.Width = 45;
z.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
z.Margin = new Thickness(250, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
sp.Children.Add(z);
outerSp.Children.Add(sp);
ViewQuestions(sp);
}