I have a pointing to null error but i dont see where the problem is since everything gets initalised before used. the points where the error acures i have given a blockquote. Like always im thankfull for every help.
Button topAddBut = null;
//Button botAddBut = null;
Button loadBut = null;
Button saveBut = null;
StackPanel topSP = null;
//StackPanel botSP = null;
public MainWindow()
{
InitializeComponent();
loadBut = new Button { Content = "Load", Width = 70, Height = 23 };
Canvas.SetRight(loadBut, 160);
Canvas.SetBottom(loadBut, 24);
canvas1.Children.Add(loadBut);
saveBut = new Button { Content = "Save", Width = 70, Height = 23 };
Canvas.SetRight(saveBut, 80);
Canvas.SetBottom(saveBut, 24);
canvas1.Children.Add(saveBut);
StackPanel topSP = new StackPanel { Width = 400, Height = 50 };
Canvas.SetLeft(topSP, 160);
Canvas.SetTop(topSP, 100);
AddWrapPanelTop();
AddTextBoxTop();
AddTopButton();
}
void AddTextBoxTop()
{
TextBox txtB1 = new TextBox();
txtB1.Text = "Text";
txtB1.Width = 75;
txtB1.Height = 75;
WrapPanel wp = (WrapPanel)topSP.Children[0];
wp.Children.Add(txtB1);
}
void AddWrapPanelTop()
{
WrapPanel myWrapPanel = new WrapPanel();
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);
myWrapPanel.Background = System.Windows.Media.Brushes.Magenta;
myWrapPanel.Orientation = Orientation.Horizontal;
myWrapPanel.Width = 4000;
myWrapPanel.HorizontalAlignment = HorizontalAlignment.Left;
myWrapPanel.VerticalAlignment = VerticalAlignment.Top;
topSP.Children.Add(myWrapPanel);
}
void AddTopButton()
{
TextBox txtB1 = new TextBox();
txtB1.Background = System.Windows.Media.Brushes.Magenta;
txtB1.Text = "Text";
txtB1.Width = 75;
txtB1.Height = 75;
topAddBut = new Button();
topAddBut.Click += new RoutedEventHandler(this.TopClick);
topAddBut.Content = "Add";
topAddBut.Width = 75;
// Add the buttons to the parent WrapPanel using the Children.Add method.
WrapPanel wp = (WrapPanel)topSP.Children[0];
wp.Children.Add(txtB1);
wp.Children.Add(loadBut);
this.topSP.Children.Add(wp);
}
void TopClick(Object sender, EventArgs e)
{
TextBox txtB1 = new TextBox();
txtB1.Background = System.Windows.Media.Brushes.Magenta;
txtB1.Text = "Text";
txtB1.Width = 75;
txtB1.Height = 75;
Button s = (Button)sender;
WrapPanel wp = (WrapPanel)s.Parent;
wp.Children.Remove(s);
wp.Children.Add(txtB1);
AddTopButton();
// Add the buttons to the parent WrapPanel using the Children.Add method.
}
}
}
You define the following:
StackPanel topSP = null;
Then you have
StackPanel topSP = new StackPanel { Width = 400, Height = 50 };
This is in your local scope though so will be destroyed when you exit the function
Finally you have
topSP.Children.Add(myWrapPanel);
This will still be set to null which is why your error occurs. Basically you are creating a local version of the same variable.
In order to resolve simply change the second definition to:
topSP = new StackPanel { Width = 400, Height = 50 };
Related
I create a stackpanel, and put some controls and 1 of these is a textbox, programaticly.
How can i create an event that, when i click on textbox i delete the stackpanel and everything inside?
Here is my code...
System.Windows.Controls.StackPanel Defesa = new StackPanel();
Defesa.Height = 120;
Defesa.Width = 140;
Panel_DE.Children.Add(Defesa);
Defesa.Background = new
SolidColorBrush((Color)ColorConverter.
ConvertFromString("#FF000000"));
Defesa.Margin = new Thickness(20, 0, 0, 0);
Ellipse Foto = new Ellipse();
Foto.Height = 80;
Foto.Width = 80;
Foto.StrokeThickness = 2;
SolidColorBrush borda = new SolidColorBrush();
borda.Color = Colors.White; ;
Foto.Stroke = borda;
Defesa.Children.Add(Foto);
ImageBrush camera = new ImageBrush();
camera.ImageSource = new BitmapImage(new
Uri(#"C:\Users\Vitor\documents\visual studio 2013\Projects\Manager
Treinador\Manager Treinador\Icons\photo-camera w.png"));
Foto.Fill = camera;
System.Windows.Controls.TextBlock Nome_def = new TextBlock();
Nome_def.Text = def_name;
Nome_def.Foreground = new
SolidColorBrush((Color)ColorConverter.ConvertFromString
("#FFF9F4F4"));
Nome_def.HorizontalAlignment = HorizontalAlignment.Center;
Defesa.Children.Add(Nome_def);
System.Windows.Controls.StackPanel Panel = new StackPanel();
Panel.Orientation = Orientation.Horizontal;
Defesa.Children.Add(Panel);
System.Windows.Controls.TextBlock But_Delet = new TextBlock();
But_Delet.Text = "X";
But_Delet.FontWeight = FontWeights.Bold;
But_Delet.Foreground = new
SolidColorBrush((Color)ColorConverter.ConvertFromString
("#FFF90707"));
Panel.Children.Add(But_Delet);
But_Delet.HorizontalAlignment = HorizontalAlignment.Left;
But_Delet.Cursor = Cursors.Hand;
But_Delete is the text box that i want to remove all...
Thanks
Since a TextBlock does not have a click event, you will need to simulate this by using OnPreviewMouseDown event. To do this:
add this to the end of your code:
But_Delet.PreviewMouseDown += ButDeletOnPreviewMouseDown;
Then add this event handler:
private void ButDeletOnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Panel_DE.Children.Remove(Defesa);
}
However, if there is a chance you will need to restore these controls, it would better to hide them using this event handler instead:
private void ButDeletOnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Defesa.Visibility = Visibility.Hidden;
}
I made 2 MouseEvents, they working, but the problem, that they working not like i have expected. I need these 2 events to be active when my mouse pointer is right in Grid Space, but now they working only if the pointer is on any Line.
my code:
// Grid 3 Rows.
Grid grid_Edit = new Grid();
Grid.SetRow(grid_Edit, 0);
Grid.SetColumn(grid_Edit, 1);
RowDefinition rowDef1 = new RowDefinition();
RowDefinition rowDef2 = new RowDefinition();
RowDefinition rowDef3 = new RowDefinition();
grid_Edit.RowDefinitions.Add(rowDef1);
grid_Edit.RowDefinitions.Add(rowDef2);
grid_Edit.RowDefinitions.Add(rowDef3);
grid_Edit.RowDefinitions[0].Height = new GridLength(1, GridUnitType.Star);
grid_Edit.RowDefinitions[1].Height = new GridLength(1, GridUnitType.Star);
grid_Edit.RowDefinitions[2].Height = new GridLength(1, GridUnitType.Star);
grid_Edit.MouseEnter += new MouseEventHandler(gridEdit_MouseEnter);
grid_Edit.MouseLeave += new MouseEventHandler(gridEdit_MouseLeave);
mainWindow_ref.Children.Add(grid_Edit);
// 3 lines
line1.Stroke = Brushes.White;
line1.X1 = 1;
line1.Stretch = Stretch.Fill;
Grid.SetRow(line1, 0);
line1.VerticalAlignment = VerticalAlignment.Center;
line2.Stroke = Brushes.White;
line2.X1 = 1;
line2.Stretch = Stretch.Fill;
Grid.SetRow(line2, 1);
line2.VerticalAlignment = VerticalAlignment.Center;
line3.Stroke = Brushes.White;
line3.X1 = 1;
line3.Stretch = Stretch.Fill;
Grid.SetRow(line3, 2);
line3.VerticalAlignment = VerticalAlignment.Center;
// add lines to grid_Edit
grid_Edit.Children.Add(line1);
grid_Edit.Children.Add(line2);
grid_Edit.Children.Add(line3);
private static void gridEdit_MouseLeave(object sender, MouseEventArgs e)
{
line1.Stroke = Brushes.White;
line2.Stroke = Brushes.White;
line3.Stroke = Brushes.White;
}
private static void gridEdit_MouseEnter(object sender, MouseEventArgs e)
{
line1.Stroke = Brushes.Black;
line2.Stroke = Brushes.Black;
line3.Stroke = Brushes.Black;
}
Set the background color of your Grid to Transparent, this will allow the Grid to capture the mouse events.
Answered, Thanks to Rohit Vats & Panagiotis Kanavos for their answers worked perfectly!
I want a ComboBox to go on the same line as a TextBox in a Stackpanel but it just puts it on the line below when the margins are the same.
The ComboBox and the Textboxs are being generated when I click a button.
C# Code:
int t = 0;
private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{
StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
TextBox x = new TextBox();
x.Name = "Title" + t;
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.VerticalAlignment = System.Windows.VerticalAlignment.Top;
x.Margin = new Thickness(0, 15, 0, 0);
ComboBox y = new ComboBox();
y.Name = "Combo" + t;
y.Text = (t + 1).ToString();
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.VerticalAlignment = System.Windows.VerticalAlignment.Top;
y.Margin = new Thickness(0, 15, 0, 0);
spStandard.Children.Add(x);
spStandard.Children.Add(y);
spStandard.Children.Add(sp);
t++;
}
int q = 0;
private void btnQuestion_Click(object sender, RoutedEventArgs e)
{
TextBox x = new TextBox();
x.Name = "Question" + q;
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(70, 15, 0, 0);
spStandard.Children.Add(x);
q++;
}
Picture of what is happening:
http://i.stack.imgur.com/F3Nk8.png
As you can see the Combobox object is getting put under the Textbox when I need it to the left of the Textbox.
Is there any way that i can get around this but by keeping the Stackpanel?
(I asked a question similar to this before but it wasn't for this exact reason.)
You should add it in a StackPanel sp with Orientation set to Horizontal instead of directly adding to outer panel.
Change
spStandard.Children.Add(x);
spStandard.Children.Add(y);
to
sp.Children.Add(x);
sp.Children.Add(y);
It looks like you are adding the elements to the wrong StackPanel. Instead of adding them to sp, which has Orientation = Orientation.Horizontal you add them to spStandard.
You should change:
spStandard.Children.Add(x);
spStandard.Children.Add(y);
to
sp.Children.Add(x);
sp.Children.Add(y);
I have a few information that i want to display inside the DockPanel but , it goes out of line in the DockPanel and some text got cut off like this :
i populate them in code behind , how do i set the dockpanel alignment such that the whole content can be placed inside it without being cut-off .
Heres the xaml code :
<Grid Background="#FF5EC136">
<DockPanel Height="894" HorizontalAlignment="Stretch" Margin="365,135,0,0" Name="dockPanel1" VerticalAlignment="Top" Width="1174" />
</Grid>
i don't know if my xaml.cs will be useful , so i just post it here most of the codes are unrelated tho :
private void PopulateQuestion(int activityID, int taskID)
{
IList<Model.questionhint> lstQuestionHints = qh.GetRecords(taskID, activityID);
StackPanel sp = new StackPanel();
foreach (Model.questionhint qhm in lstQuestionHints)
{
StackPanel sp1 = new StackPanel() { Orientation = Orientation.Horizontal };
TextBlock tb = new TextBlock();
tb.Text = qhm.QuestionContent;
tb.FontWeight = FontWeights.Bold;
tb.FontSize = 24;
sp1.Children.Add(tb);
TextBox tbox = new TextBox();
tbox.Width = 100;
tbox.FontSize = 24;
tbox.FontWeight = FontWeights.Bold;
if (qhm.Option1.Trim().Length > 0 &&
qhm.Option2.Trim().Length > 0)
{
sp1.Children.Add(tbox);
}
Label btn1 = new Label();
Label btn2 = new Label();
if (qhm.Option1.Trim().Length > 0)
{
btn1.Content = qhm.Option1;
btn1.Width = 110;
btn1.FontSize = 24;
btn1.FontWeight = FontWeights.Bold;
btn1.Foreground = (SolidColorBrush)new BrushConverter().ConvertFromString("#FFE22B2B");
sp1.Children.Add(btn1);
btn1.MouseLeftButtonDown += (source, e) =>
{
btn2.Foreground = (SolidColorBrush)new BrushConverter().ConvertFromString("#FFE22B2B");
btn1.Foreground = (SolidColorBrush)new BrushConverter().ConvertFromString("#FFF0FA08");
tbox.Text = btn1.Content.ToString();
};
}
if (qhm.Option2.Trim().Length > 0)
{
btn2.Content = qhm.Option2;
btn2.Width = 110;
btn2.FontSize = 24;
btn2.FontWeight = FontWeights.Bold;
btn2.Foreground = (SolidColorBrush)new BrushConverter().ConvertFromString("#FFE22B2B");
sp1.Children.Add(btn2);
btn2.MouseLeftButtonDown += (source, e) =>
{
btn1.Foreground = (SolidColorBrush)new BrushConverter().ConvertFromString("#FFE22B2B");
btn2.Foreground = (SolidColorBrush)new BrushConverter().ConvertFromString("#FFF0FA08");
tbox.Text =btn2.Content.ToString();
};
}
sp.Children.Add(sp1);
}
dockPanel1.Children.Add(sp);
Use WrapPanel instead of StackPanel. Replace following line in your code
StackPanel sp1 = new StackPanel() { Orientation = Orientation.Horizontal };
with
WrapPanel wp = new WrapPanel();
Here is my code to get values from XML file:
foreach (XmlNode node in DOC.SelectNodes("//CheckMarkObject"))
{
FbCheckMark checkmark = new FbCheckMark();
checkmark.Name = node.SelectSingleNode("Name").InnerText;
checkmark.Label = node.SelectSingleNode("Label").InnerText;
if (node.SelectSingleNode("IsChecked").InnerText == "0")
{
checkmark.IsChecked = false;
}
else
{
checkmark.IsChecked = true;
}
listCheckMarks.Add(checkmark);
}
Now the code to create control at runtime:
for (int i = 0; i < listCheckMarks.Count; i++)
{
if (listCheckMarks[i].checkMark.Equals(checkMark))
{
CheckBox cb = new CheckBox();
TextBlock cbtextblock = new TextBlock();
cbtextblock.Text = listCheckMarks[i].Label;
cbtextblock.Height = 27;
cbtextblock.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
cbtextblock.Margin = new Thickness(12, 20, 0, 0);
cbtextblock.VerticalAlignment = System.Windows.VerticalAlignment.Top;
cb.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
cb.VerticalAlignment = System.Windows.VerticalAlignment.Top;
cb.Margin = new Thickness(150, 21, 0, 0);
cb.Height = 50;
cb.Width = 100;
cb.Name = listCheckMarks[i].Name;
LayoutRoot.Children.Add(cbtextblock);
LayoutRoot.Children.Add(cb);
}
}
when i made change in my XML file i.e Create "CheckMark" tag two times. the result control overwrite on previous one. i want to arrange the new control under the previous one.
kindly suggest me what can i do ? use linear layout like in android?
thanks
Try to insert elements into StackPanel and set Orientation property for it.
Try that example:
StackPanel container = new StackPanel();
LayoutRoot.Children.Add(container);
for (int i = 0; i < listCheckMarks.Count; i++)
{
if (listCheckMarks[i].checkMark.Equals(checkMark))
{
StackPanel childContainer = new StackPanel();
childContainer.Orientation = Orientation.Horizontal;
CheckBox cb = new CheckBox();
TextBlock cbtextblock = new TextBlock();
cbtextblock.Text = listCheckMarks[i].Label;
cbtextblock.Height = 27;
cbtextblock.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
cbtextblock.Margin = new Thickness(12, 20, 0, 0);
cbtextblock.VerticalAlignment = System.Windows.VerticalAlignment.Top;
cb.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
cb.VerticalAlignment = System.Windows.VerticalAlignment.Top;
cb.Margin = new Thickness(150, 21, 0, 0);
cb.Height = 50;
cb.Width = 100;
cb.Name = listCheckMarks[i].Name;
childContainer.Children.Add(cbtextblock);
childContainer.Children.Add(cb);
container.Children.Add(childContainer);
}
}