バグを見つけるのに助けが必要です。以下は提供されたコードです。
これはフォームのコントロールです。ユーザーがドロップダウンを閉じると、フォームは自動的に別の行に追加されます:
private void addNewProfRow(object sender, EventArgs e) //all will invoke this generic method to display the visibility of the next
{
ComboBox selectedTest = (ComboBox)sender;
Canvas thisRow = (Canvas)selectedTest.Parent;
int index = Int32.Parse(thisRow.Name.Substring(thisRow.Name.Length - 1, 1)); //gets the num at the end of the name
if (thisRow is Canvas && index == profMultipleCount && index < 9) //needs to ensure that only the previous canvas can invoke the next canvas once selection is done
{
profMultipleCount++; //points to the next row of canvas
Canvas newRow = (Canvas)this.FindName("profileComp" + profMultipleCount); //finds the object and cast as Canvas
newRow.Visibility = Visibility.Visible; //makes it visible
}
//hides the textBox in the case that user added a test component he did not want
if (thisRow is Canvas && selectedTest.Text == "")
{
Canvas newRow = (Canvas)this.FindName("profileComp" + profMultipleCount);
testComp = (ComboBox)this.FindName("testComp" + profMultipleCount);
display = (CheckBox)this.FindName("profDisplay" + profMultipleCount);
testComp.Text = "";
display.IsChecked = false;
newRow.Visibility = Visibility.Hidden;
profMultipleCount--;
}
}
そして、これはユーザー入力を保存する方法です。
private void saveProfile(object sender, RoutedEventArgs e)
{
int i = 1;
Canvas newRow = (Canvas)this.FindName("profileComp" + i);
testComp = (ComboBox)this.FindName("testComp" + i);
while (testComp.Text!= "" && i < 10 && newRow.Visibility == Visibility.Visible) //order matters becoz newRow depends on i
{
//input the rowItem in the format (profileCode, testComp, displayable, action) of the excel sheet
display = (CheckBox)this.FindName("profDisplay" + i);
testComp = (ComboBox)this.FindName("testComp" + i);
userData.setMulRowData(profileTab.Tag.ToString() + "," + (profileCodeCB.Text + "," + testComp.Text + "," + display.IsChecked.Value.ToString()[0]));
Console.WriteLine(i + "," + (profileCodeCB.Text + "," + testComp.Text + "," + display.IsChecked.Value.ToString()[0]));
i++; profileIndex++;
newRow = (Canvas)this.FindName("profileComp" + i);
}
profileIndex++;
clearProfile(sender, e);
}
私のxamlはここにあります:
<Grid Background="White" Height="3200" VerticalAlignment="Top">
<Grid x:Name="panelTab" Height="410" Tag="Panel Component" VerticalAlignment="Top" Margin="10,0" IsEnabled="False">
<Label Content="Panel Component:" HorizontalAlignment="Left" VerticalAlignment="Top" Cursor="None" FontSize="16" FontFamily="Segoe UI Symbol" Width="167" FontWeight="Bold"/>
<Label Content="Panel Code:" HorizontalAlignment="Left" VerticalAlignment="Top" FontWeight="Bold" Margin="10,50,0,0"/>
<ComboBox x:Name="panelCodeCB" HorizontalAlignment="Left" Margin="134,54,0,0" VerticalAlignment="Top" Width="85" IsEditable="True" Tag="Panel Code"/>
<StackPanel x:Name="testCompList" HorizontalAlignment="Left" Height="274" Margin="10,92,0,0" VerticalAlignment="Top" Width="361">
<Canvas x:Name="comp1" Margin="0,0,8,0" Height="26">
<Label Content="Panel Component 1*:" HorizontalAlignment="Left" VerticalAlignment="Top" FontWeight="Bold"/>
<ComboBox x:Name="pComp1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Panel Component" Canvas.Left="134" Canvas.Top="5" DropDownClosed="addNewPRow"/>
</Canvas>
<Canvas x:Name="comp2" Margin="0,0,8,0" Height="26" Visibility="Hidden">
<Label Content="Panel Component 2:" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<ComboBox x:Name="pComp2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" IsEditable="True" Tag="Panel Component" Canvas.Left="134" Canvas.Top="5" DropDownClosed="addNewPRow"/>
</Canvas>
</StackPanel>
<TextBlock HorizontalAlignment="Left" Margin="496,0,0,0" VerticalAlignment="Top"><Hyperlink NavigateUri="C:\Users\Documents\Visual Studio 2012\Projects\User Upload\Data Upload\MainPanel.xaml"><Run Text="Add New..."/></Hyperlink></TextBlock>
<Button Name="panelBtn" Content="Save and Add Another" HorizontalAlignment="Left" Margin="418,344,0,0" VerticalAlignment="Top" Width="135" Click="savePanel"/>
</Grid>
ユーザー入力に対して writeLine を実行すると、次のようになります。
1,tth,1 2,tth,2 3,tth,3 4,tth,
しかし、ユーザーが実際に入力したのは次のとおりです。
1,tth,1 2,tth,2 3,tth,3
だから私はなぜ私のwhileループが空の行を制限せず、それでも入ることができるのか尋ねたいのですか?
編集: ユーザーが最後のドロップダウン ボックスを閉じない場合、ユーザーが実際に入力した内容を取得します。これは私が望むものですが、なぜこのようになるのかわかりません。