0

I need help catching a bug. Below is the provided code.

This is the controls for the form. When user closes the dropdown, form will automatically add in another row:

        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--;
        }
    }

And this is the method to save the user inputs.

        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);
    }

and my xaml is here:

<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>

When i do a writeLine for the user input, what i get is this:

1,tth,1 2,tth,2 3,tth,3 4,tth,

but what user actually typed is this:

1,tth,1 2,tth,2 3,tth,3

SO i like to ask how come my while loop does not restrict the empty row and still allows it to enter?

Edit: when user does not close the last dropdown box, i get what user actually types, which is what i want, but i have no idea why is it like this.

leppie
  • 115,091
  • 17
  • 196
  • 297
Sheep
  • 47
  • 1
  • 9
  • 3
    Dude I don't know if you're aware of this, but there's a thing called [XAML](http://en.wikipedia.org/wiki/Extensible_Application_Markup_Language) which is normally used in WPF to define the UI instead of this bunch of horrible code. I strongly suggest you use that instead. – Federico Berasategui Apr 24 '13 at 02:49
  • 1
    You really really really need to read [this](http://stackoverflow.com/a/14382137/643085) - I mean you said `I need help catching a bug`, but you really need help understanding how WPF is actually supposed to be used. – Federico Berasategui Apr 24 '13 at 02:53
  • hi thx for the feedback but i do use xaml for my UI interfacing, just that validating is on my code behind and input gathering. – Sheep Apr 24 '13 at 03:22
  • 3
    you got it all completely wrong. Your code is all wrong. and again, UI is not Data, therefore in order to validate stuff you should really do that in a ViewModel, not in the code behind by casting all elements like that. Please post a screenshot of what you need so I can tell you what's the proper way to implement it in WPF. – Federico Berasategui Apr 24 '13 at 03:24
  • Put your `XAML` here. – Haritha Apr 24 '13 at 04:08
  • 1
    hi it sounds like my codes is horribly wrong. Could you provide a simple way of how to implement correctly? – Sheep Apr 24 '13 at 05:56

1 Answers1

0

Best to start over using a pattern like MVVM. There are numerous tutorials and books but I'll point one out to start with.

Chris Mosers has a nice WPF tutorialsite start with his explanation of MVVM.

It will be different to what you are used to, but in the end you'll be freed of the code you've shown. And no its not a 'simple way to implement', but if its a question of simple or good go for good.

Ralf de Kleine
  • 11,464
  • 5
  • 45
  • 87