I am trying to make a new Time table app, where the user enters rowNo and colNo. I have done this code so far, but I get a Null reference exception, whenever I try to retrieve a textbox from the array.
XAML
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" Background="{x:Null}">
<Grid Name="bigGrid" Height="780" Width="1300">
<Grid.Background>
<ImageBrush Stretch="Fill" ImageSource="/TimeTableBackground.jpg"/>
</Grid.Background>
<StackPanel Name="panel" Height="128" Margin="0,-3,0,0" VerticalAlignment="Top" Background="#1D000000">
<TextBlock TextWrapping="Wrap" Text="Time Table" FontSize="24" FontStyle="Italic" FontFamily="Segoe WP" FontWeight="Bold" Foreground="#FFC83DA2"/>
<TextBlock TextWrapping="Wrap" Text="Edit Table" Height="72" Padding="0" FontSize="64" FontFamily="Comic Sans MS" FontStyle="Italic" Foreground="#FFFF6BD7"/>
</StackPanel>
<Grid Name="grid1" HorizontalAlignment="Stretch" Height="{Binding ElementName=LayoutRoot,Path=ActualHeight}" Width="5000" VerticalAlignment="Bottom" Background="#1C000000" Margin="-14,0,14,19" ShowGridLines="True" />
</Grid>
</ScrollViewer>
<!--Sample code showing usage of ApplicationBar-->
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar.edit.rest.png" Text="Button 1" Click="ApplicationBarIconButton_Click"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar.save.rest.png" Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
XAML is just to know the names of controls. However, C# code is the one.
C#
public partial class Edit_page : PhoneApplicationPage
{
private TextBox[,] tbArray = new TextBox[9,9];
int rowNo = 6, colNo = 6;
public Edit_page()
{
InitializeComponent();
//for loops to add rows and columns to the main grid
for (int j = 0; j <= rowNo; j++)
{
RowDefinition Row = new RowDefinition();
grid1.RowDefinitions.Add(Row);
Row.MinHeight = 80;
}
for (int q = 0; q <= colNo; q++)
{
ColumnDefinition Col = new ColumnDefinition();
grid1.ColumnDefinitions.Add(Col);
Col.MinWidth = 128;
}
grid1.Width = 128 * (colNo);
//for loop to add a text block to each cell.
for (int h = 1; h <= rowNo; h++)
{
tbArray[h, 0] = new TextBox() { TextWrapping = TextWrapping.Wrap, AcceptsReturn = Convert.ToBoolean(1) };
tbArray[h, 0].InputScope = new InputScope { Names = { new InputScopeName { NameValue = InputScopeNameValue.Text } } };
tbArray[h, 0].Background = new SolidColorBrush(Colors.Red);
tbArray[h, 0].SetValue(Grid.RowProperty, h);
tbArray[h, 0].SetValue(Grid.ColumnProperty, 0);
grid1.Children.Add(tbArray[h, 0]);
}
for (int h = 1; h <= colNo; h++)
{
tbArray[0,h] = new TextBox() { TextWrapping = TextWrapping.Wrap, AcceptsReturn = Convert.ToBoolean(1) };
tbArray[0, h].InputScope = new InputScope { Names = { new InputScopeName { NameValue = InputScopeNameValue.Text } } };
tbArray[0, h].Background = new SolidColorBrush(Colors.Red);
tbArray[0, h].SetValue(Grid.RowProperty, 0);
tbArray[0, h].SetValue(Grid.ColumnProperty, h);
grid1.Children.Add(tbArray[0, h]);
}
for (int i = 1; i <= rowNo; i++)
开发者_JAVA技巧 {
for (int k = 1; k <= colNo; k++)
{
tbArray[i, k] = new TextBox() { TextWrapping = TextWrapping.Wrap, AcceptsReturn = Convert.ToBoolean(1) };
tbArray[i, k].InputScope = new InputScope { Names = { new InputScopeName { NameValue = InputScopeNameValue.Text } } };
//name each textbox in the grid
tbArray[i, k].Name = string.Format("tb{0}{1}", i, k);
tbArray[i, k].Text = string.Format("afandem{0}{1}", i, k);
//ScrollViewer scroll = new ScrollViewer();
//scroll.SetValue(Grid.RowProperty, i);
//scroll.SetValue(Grid.ColumnProperty, k);
//scroll.Content = tb;
tbArray[i, k].SetValue(Grid.RowProperty, i);
tbArray[i, k].SetValue(Grid.ColumnProperty, k);
grid1.Children.Add(tbArray[i, k]);
}
}
}
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
//var isoStore = IsolatedStorageFile.GetUserStoreForApplication();
////For loops to create files
// for (int i = 0; i <= rowNo; i++)
// {
// for (int j = 0; j <= colNo; j++)
// {
// string fileName = string.Format("{0}{1}.txt", i, j);
// using (var file = isoStore.OpenFile(fileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
// {
// using (var writer = new StreamWriter(file))
// {
// writer.Write(tbArray[i, j].Text);
// }
// }
// }
// }
grid1.Visibility = Visibility.Collapsed;
Grid testGrid = new Grid();
bigGrid.Children.Add(testGrid);
for (int j = 0; j <= rowNo; j++)
{
RowDefinition Row = new RowDefinition();
testGrid.RowDefinitions.Add(Row);
Row.MinHeight = 80;
}
for (int q = 0; q <= colNo; q++)
{
ColumnDefinition Col = new ColumnDefinition();
testGrid.ColumnDefinitions.Add(Col);
Col.MinWidth = 128;
}
testGrid.Width = 128 * (colNo);
for (int i = 0; i <= rowNo; i++)
{
for (int j = 0; j <= colNo; j++)
{
TextBlock tB = new TextBlock();
tB.Text = tbArray[i, j].Text;
tB.SetValue(Grid.RowProperty, i);
tB.SetValue(Grid.ColumnProperty, j);
testGrid.Children.Add(tB);
}
}
}
}
When you're populating tbArray
you're using the following loops
for (int h = 1; h <= rowNo; h++) { ... }
for (int h = 1; h <= colNo; h++) { ... }
Since h
is being initialized to 1
instead 0
in both cases, the tbArray[0,0]
element does not get initialized and is null
; hence the NullReferenceException
.
I'm not really sure what your app is supposed to do but there probably is a better way to go about adding items than doing everything in code-behind as you're doing.
For instance, if you're trying to add additional rows when the user clicks an application bar button, then you should be using a ListBox
instead of the Grid
. The ItemTemplate
of the ListBox
would then contain a Grid
having 6 columns (or a StackPanel
with horizontal orientation), and a TextBlock
within each cell. You can then bind each TextBlock
's Text
property to an ObservableCollection
and simply add new text to all the collections, this will automatically update the UI.
There are several tutorials on XAML bindings and ObservableCollection
s if you Google it. Also, unless you're putting very little text in each of those TextBlock
s, 6 columns is going to be too wide for a phone screen.
精彩评论