I'm trying to create a contacts app. When I try to edit a contact it opens a window that looks like this: image
The code that loads the contact model is:
private void LoadContactToEdit() {
FirstNameTextBox.Text = contactToEdit.FirstName;
LastNameTextBox.Text = contactToEdit.LastName;
string[] phoneNumbers = contactToEdit.PhoneNumbers.Split('|');
int i = 0;
foreach(string phoneNumber in phoneNumbers) {
StackPanel sp = new StackPanel();
NameScope.SetNameScope(this, new NameScope());
sp.Name = $ "PNStackPanel{i}";
RegisterName(sp.Name, sp);
sp.Orientation = Orientation.Horizontal;
sp.HorizontalAlignment = HorizontalAlignment.Center;
TextBox txtbox = new TextBox();
txtbox.Name = $ "PNTextBox{i}";
txtbox.Text = phoneNumber;
txtbox.Width = 200;
txtbox.Background = Brushes.White;
txtbox.Margin = new Thickness(0, 10, 0, 0);
sp.Children.Add(txtbox);
if (i > 0) {
Button btn = new Button();
btn.Name = $ "PNButton{i}";
btn.Content = btn.Name;
btn.Width = 100;
btn.Click += DeletePhoneNumberTextBox;
btn.Margin = new Thickness(0, 10, 0, 0);
sp.Children.Add(btn);
}
PhoneNumberStackPanel.Children.Add(sp);
i++;
}
string[] emailAddresses = contactToEdit.EmailAddresses.Split('|');
i = 0;
foreach(string emailAddress in emailAddresses) {
StackPanel sp = new StackPanel();
sp.Name = $ "EAStackPanel{i}";
sp.Orientation = Orientation.Horizontal;
sp.HorizontalAlignment = HorizontalAlignment.Center;
TextBox txtbox = new TextBox();
txtbox.Name = $ "EATextBox{i}";
txtbox.Text = emailAddress;
txtbox.Width = 200;
txtbox.Background = Brushes.White;
txtbox.Margin = new Thickness(0, 10, 0, 0);
sp.Children.Add(txtbox);
if (i > 0) {
Button btn = new Button();
btn.Name = $ "EAButton{i}";
btn.Content = btn.Name;
btn.Width = 100;
//btn.Click += DeleteEmailAddressTextBox;
btn.Margin = new Thickness(0, 10, 0, 0);
sp.Children.Add(btn);
}
EmailAddressStackPanel.Children.Add(sp);
i++;
}
}
Look at the phone number code for now. I am trying to create buttons that dynamically deletes the stackpanel the are part of.
this is their code:
private void DeletePhoneNumberTextBox(object sender, RoutedEventArgs e){
string s = (sender as Button).Name;
char c = s[s.Length - 1];
string x = $ "PNStackPanel{c}";
StackPanel sp = PhoneNumberStackPanel.FindName(x) as StackPanel;
PhoneNumberStackPanel.Children.Remove(sp);
}
However only the last button deletes its stackpanel. I tried messing with itemsControl instead of stackpanels and I only got more confused. What do I do wrong?
[EDIT]: I converted the StackPanel into ItemsControl after some researching and removing items is a lot easier.
Related
I'm attempting to display some dynamically generated labels next to dynamically generated text boxes. The text boxes appear but the labels do not.
I've looked at several solutions and have tried to make sure I defined all the label properties. I looked at some threading related solution that seem unnecessary because i'm not changing visibility state, I would just like to pop up the labels next to the text boxes.
TextBox[] channelNames = new TextBox[numOfChannels];
GroupBox channelBox = new GroupBox();
Label[] labelNames = new Label[numOfChannels];
for (int currentChannelIndex = 0; currentChannelIndex < numOfChannels; currentChannelIndex++)
{
var txt = new TextBox();
channelNames[currentChannelIndex] = txt;
txt.Name = channelCollection[currentChannelIndex].PhysicalName;
txt.Text = "ben";
txt.Location = new Point(200, 32 + (currentChannelIndex * 28));
txt.Visible = true;
this.channelBox.Controls.Add(channelNames[currentChannelIndex]);
var lbl = new Label();
labelNames[currentChannelIndex] = lbl;
lbl.AutoSize = true;
lbl.Name = channelCollection[currentChannelIndex].PhysicalName;
lbl.Size = new Size(55, 13);
lbl.TabIndex = 69;
lbl.Text = channelCollection[currentChannelIndex].PhysicalName;
lbl.Location = new Point(175, 32 + (currentChannelIndex * 28));
lbl.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.channelBox.Controls.Add(labelNames[currentChannelIndex]);
}
Here are a few things to check:
1) You are setting "AutoSize" and "Size". Trying removing "Size"
2) The default font is likely too large for the (55,13) size.
Try explicitly setting the font to something small to see if it shows up.
Does channelCollection[currentChannelIndex].PhysicalName actually contain a non-Empty string? e.g.:
class Something
{
public string PhysicalName { get; set; }
}
private void AddLabels()
{
Something[] channelCollection = new Something[]
{
//Applying this to Label.Text makes it "invisible"
new Something() { PhysicalName = "" }
};
var currentChannelIndex = 0;
var txt = new TextBox();
txt.Name = channelCollection[currentChannelIndex].PhysicalName;
txt.Text = "ben";
txt.Location = new Point(200, 32);
txt.Visible = true;
this.Controls.Add(txt);
var lbl = new Label();
lbl.AutoSize = true;
lbl.Name = channelCollection[currentChannelIndex].PhysicalName;
lbl.Size = new Size(55, 13);
lbl.TabIndex = 69;
lbl.Text = channelCollection[currentChannelIndex].PhysicalName;
lbl.Location = new Point(175, 32);
lbl.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.Controls.Add(lbl);
}
I actually had an exception that downstream of this code block that was causing the issue. I assumed that i would see the labels since the exception was thrown after the call to the label. Thanks for your suggestions.
I am building a window from code.
I add a header TextBlock and then some various elements for a listBox in which each element is made out of
Grid ---> children[0] a TextBlock to explain what is the element
---> children1 a TextBox or combobox to input data.
At the end a button.
After having generated put the focus on the first element (generally the first TextBox) so that the user can immediately input data. Up to that everything works.
--ADD--
I forgot to mention that every element = Grid = Textblock + TextBox or combobox, is an element of the parent listBox. So I would like to move from every textbox or combobox independently from the listbox element they are in.
After that I want the user to be able to move to next element (textbox or combobox) through TAB. So when generating the previous windows I use a counter and set
tb.TabIndex = tabIndex++;
or
ComboBox.TabIndex = tabIndex++;
Now the problem is that after having input data in the first TextBox when I press TAB the caret gets lost and so the focus...
---ADD2---
Here is the code:
private void PreparareTraceFieldGrid(ListBox lbTracefields, ObservableCollection<Tuple<string, object, object>> obc, bool IsReadOnly)
{
bool IsFocusSet = false;
/*---TITOLO---*/
OutlinedTextBlock otbTitle = CreateOtbForTraceFieldInputWindow(Languages.Word(eWords.InsertTracefields));
lbTracefields.Items.Add(otbTitle);
/*----CICLO----*/
int tabIndex = 0;
foreach (var item in obc)
{
Grid gridInternal = new Grid();
gridInternal.Background = Brushes.Transparent;
ColumnDefinition cd1 = new ColumnDefinition();
cd1.Width = new GridLength(300, GridUnitType.Pixel);
ColumnDefinition cd2 = new ColumnDefinition();
cd2.Width = new GridLength(1.0, GridUnitType.Star);
gridInternal.ColumnDefinitions.Add(cd1);
gridInternal.ColumnDefinitions.Add(cd2);
OutlinedTextBlock otb = CreateOtbForTraceFieldInputWindow(item.Item1);
otb.FontSize = easyRunData.OptionsGraphical.FontSize;
otb.Stroke = new SolidColorBrush(easyRunData.OptionsGraphical.StrokeColour);
otb.StrokeThickness = easyRunData.OptionsGraphical.StrokeSize;
Grid.SetColumn(otb, 0);
gridInternal.Children.Add(otb);
string strDataType = item.Item2.ToString();
eTraceFieldDataTypes eDataType = (eTraceFieldDataTypes)Enum.Parse(typeof(eTraceFieldDataTypes), strDataType);
switch (eDataType)
{
case eTraceFieldDataTypes.STRING:
case eTraceFieldDataTypes.FLOAT:
case eTraceFieldDataTypes.INTEGER:
case eTraceFieldDataTypes.STRING_READONLY:
TextBox tb = new TextBox();
tb.CaretBrush = Brushes.White;
tb.FontSize = easyRunData.OptionsGraphical.FontSize;
tb.MinWidth = 200;
tb.Height = easyRunData.OptionsGraphical.FontSize + 15;
tb.VerticalAlignment = VerticalAlignment.Center;
tb.Background = Brushes.Transparent;
tb.Margin = new Thickness(10);
tb.TabIndex = tabIndex++;
tb.KeyDown += (senderEvent, args) => { AnalyzeTraceFields_IF_EMPTY(easyRunData.Tracefields.TraceFieldsUser, (Grid)lbTracefields.Parent); };
if (eDataType == eTraceFieldDataTypes.STRING_READONLY && !IsReadOnly)
tb.BorderBrush = Brushes.Yellow;
else
tb.BorderBrush = Brushes.Gainsboro;
Grid.SetColumn(tb, 1);
gridInternal.Children.Add(tb);
lbTracefields.Items.Add(gridInternal);
if (!IsFocusSet)
{
tb.Focus();
IsFocusSet = true;
}
break;
case eTraceFieldDataTypes.BOOL:
case eTraceFieldDataTypes.BOOL_READONLY:
Border brd = new Border();
brd.Margin = new Thickness(10);
brd.MinWidth = 200;
brd.Height = easyRunData.OptionsGraphical.FontSize + 15;
if (eDataType == eTraceFieldDataTypes.BOOL_READONLY && !IsReadOnly)
brd.BorderBrush = Brushes.Yellow;
else
brd.BorderBrush = Brushes.Gainsboro;
brd.BorderThickness = new Thickness(3);
ComboBox cmb = new ComboBox();
cmb.VerticalAlignment = VerticalAlignment.Center;
cmb.Background = Brushes.Transparent;
cmb.Items.Add(Languages.Word(eWords.True));
cmb.Items.Add(Languages.Word(eWords.False));
cmb.BorderBrush = Brushes.Gainsboro;
cmb.BorderThickness = new Thickness(3);
cmb.Margin = new Thickness(0);
cmb.TabIndex = tabIndex++;
cmb.DropDownClosed += (senderEvent, args) => { AnalyzeTraceFields_IF_EMPTY(easyRunData.Tracefields.TraceFieldsUser, (Grid)lbTracefields.Parent); };
Grid.SetColumn(brd, 1);
brd.Child = cmb;
gridInternal.Children.Add(brd);
lbTracefields.Items.Add(gridInternal);
if (!IsFocusSet)
{
cmb.Focus();
IsFocusSet = true;
}
break;
default:
break;
}
}
I am dynamically generating rows in a XAML grid:
for (int i = 0; i < AssociatedSteps.Length; i++)
{
//if (i == 0 || AssociatedSteps[i].OriginPkg != AssociatedSteps[i-1].OriginPkg)
//{
//Define new Row to add
RowDef = new RowDefinition();
RowDef.Height = new GridLength(60);
//Add row definition to Grid
WorkPackageViewResults.RowDefinitions.Add(RowDef);
//Define the control that will be added to new row
Text1 = new TextBlock();
Text1.Text = AssociatedSteps[i].SF01;
Text1.Width = Settings.workPackageColumn5Width;
Text1.TextAlignment = TextAlignment.Center;
Text2 = new TextBlock();
Text2.Text = AssociatedSteps[i].SF10;
Text2.Width = Settings.workPackageColumn5Width;
Text2.TextAlignment = TextAlignment.Center;
Text3 = new TextBlock();
Text3.Text = AssociatedSteps[i].OriginPkg;
Text3.Width = Settings.workPackageColumn5Width;
Text3.TextAlignment = TextAlignment.Center;
Button1 = new Button();
Button1.Content = AssociatedSteps[i].Description;
Button1.Width = Settings.workPackageColumn5Width;
Button1.Click += new RoutedEventHandler(ProgressUpdateButton_Click);
Button1.Tag = AssociatedSteps[i].StepID;
//create stackpanel and define which row to add the stackpanel to
StackP = new StackPanel();
//StackP.Background = new SolidColorBrush(Colors.Wheat);
StackP.SetValue(Grid.RowProperty, i);
StackP.Orientation = Orientation.Horizontal;
StackP.Margin = new Thickness(0, 0, 0, 0);
PercentComplete = new TextBlock();
PercentComplete.Text = (Convert.ToDouble(AssociatedSteps[i].ToDateQty) / Convert.ToDouble(AssociatedSteps[i].MTOQty)).ToString();
PercentComplete.Width = Settings.workPackageColumn5Width;
PercentComplete.TextAlignment = TextAlignment.Center;
//add your control to the stackpanel
StackP.Children.Add(Text1);
StackP.Children.Add(Text2);
StackP.Children.Add(Text3);
StackP.Children.Add(Button1);
StackP.Children.Add(PercentComplete);
//add the stackpanel to the grid
WorkPackageViewResults.Children.Add(StackP);
}
I'm trying to use LINQ to get access to the grid being programatically updated above:
<Grid Name="WorkPackageViewResults">
</Grid>
I'm not seeing they type of options you would for a cell. I want to to access a particular rows PercentComplete TextBlock so I can update the text. How do I accomplish this?
Since there's only one StackPanel you could say e => e.GetType() == typeof(StackPanel). If you ever have many, then add a Name to the StackPanel and pull it by that.
When I set a Margin from the Top to 15 for the TextBox:
x.Margin = new Thickness(100, 15, 0, 0);
This works fine and everything it ok, but then I want to make a ComboBox also appear 15px from the top - it doesn't work.
y.Margin = new Thickness(0, 15, 0, 0);
This is the code for the button I click to create the ComboBox and the TextBox:
int t = 0;
private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{
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.Margin = new Thickness(100, 15, 0, 0);
spStandard.Children.Add(x);
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.Margin = new Thickness(0, 15, 0, 0);
spStandard.Children.Add(y);
t++;
}
Here is a picture of what happens when I run the application - it shows where the ComboBox gets put:
Here is a solution for your problem, you shouldn't create controls in code behind but it will get the job done:
int t = 0;
private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{
//a new stackpanel is used to arrange items Horizontally for each line
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.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.Margin = new Thickness(0, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
spStandard.Children.Add(sp);
t++;
}
I have some trouble when i'm trying to click my dynamic controls in StackPanel.
I'm adding controls to Grid in this way...
void Opt()
{
TextBlock Title_1 = new TextBlock();
TextBlock Title_2 = new TextBlock();
CheckBox Kwota_exists = new CheckBox();
TextBox Title = new TextBox();
StackPanel Frame = new StackPanel();
Button OK = new Button();
Title_1.Text = "Dodaj kategorię";
Title_2.Text = "Aktywne kategorię";
Kwota_exists.Content = "Stała kwota?";
Title.Text = "Nazwa kategorii";
OK.Content = "Dodaj";
OK.IsEnabled = true;
OK.IsHitTestVisible = true;
OK.IsTabStop = true;
OK.ClickMode = ClickMode.Release;
Frame.IsHitTestVisible = true;
Kwota_exists.Checked +=Kwota_exists_Checked;
Title_1.FontSize = 50;
Title_2.FontSize = 50;
Title.FontSize = 20;
Frame.Height = 100;
Frame.Width = 400;
Title_1.Margin = new Thickness(0, 0, 0, 0);
Title_2.Margin = new Thickness(0, 220, 0, 0);
Frame.Margin = new Thickness(0, 70, 0, 0);
Frame.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
Frame.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
Frame.Orientation = Orientation.Horizontal;
Frame.Background = new SolidColorBrush(Windows.UI.Color.FromArgb(145, 56, 234, 21));
Frame.Children.Add(Kwota_exists);
Frame.Children.Add(Title);
Frame.Children.Add(OK);
GrdContent.Children.Add(Frame);
GrdContent.Children.Add(Title_1);
GrdContent.Children.Add(Title_2);
}
But when i'm trying to click button or check checkbox controls doesn't seem to response (unclickable).
Looks like i can't access them or i'm doing something wrong. I would be gradefull if someone explain me where i'm doing mistake.