I would like to test if a TextBox created earlier in the code exists. This TextBox is created in a "if test", this is why I want to test if it exists. But I don't know how I can test if it exists because I can't call the TextBox name because it doesn't exists..
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (((ComboBoxItem)typeproduit.SelectedItem).Content.ToString() == "Isolant")
{
TextBox EpIsolant = new TextBox
{
Width = 100,
Height = 29,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(209, 294, 0, 0)
};
MyPage.Children.Add(EpIsolant);
Label EpIsolantLabel = new Label
{
Content = "Ep isolant (mm)",
Width = 100,
Height = 29,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(209, 260, 0, 0)
};
MyPage.Children.Add(EpIsolantLabel);
}
else
{
// I want to test it here
// And if it exists, I want to remove it from MyPage.Children
}
}
Thanks for helping ! I couldn't find any help with Google
PS : When I try to change visibility, it's still not working:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TextBox EpIsolant = new TextBox
{
Width = 100,
Height = 29,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(209, 294, 0, 0)
};
MyPage.Children.Add(EpIsolant);
Label EpIsolantLabel = new Label
{
Content = "Ep isolant (mm)",
Width = 100,
Height = 29,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(209, 260, 0, 0)
};
MyPage.Children.Add(EpIsolantLabel);
EpIsolant.Visibility = Visibility.Hidden;
EpIsolantLabel.Visibility = Visibility.Hidden;
if (((ComboBoxItem)typeproduit.SelectedItem).Content.ToString() == "Isolant")
{
EpIsolant.Visibility = Visibility.Visible;
EpIsolantLabel.Visibility = Visibility.Visible;
}
else
{
EpIsolant.Visibility = Visibility.Hidden;
EpIsolantLabel.Visibility = Visibility.Hidden;
}
}
As already said, you could consider things such changing Visibility or IsEnabled property of Button to hide/disable it.
But if you want to stick with your solution, you could take out the variable outside the method:
private TextBox _epIsolant;
Then you can assign it a object:
_epIsolant = new TextBox
{
Width = 100,
Height = 29,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(209, 294, 0, 0)
};
and you can also check if it was created inside your method:
if(_epIsolant == null)
{
...
}
Related
I have a datagrid with modified column header which contain a button which shall open a popup.
This is written in code behind because of different data sources with different number of columns.
That's how it looks like:
Popups are stored in:
Dictionary<string, Popup> HeaderPopups = new Dictionary<string, Popup>();
And here the code behind:
dgMaterials.AutoGeneratingColumn += (ss, ee) =>
{
Button b = new Button() { Content = "...", Name = "btn_" + ee.PropertyName, Margin = new Thickness(3) };
b.Click += HeaderFilterButtonClick;
StackPanel stackPanel = new StackPanel() { Orientation = Orientation.Horizontal };
stackPanel.Children.Add(new TextBlock() { Text = ee.PropertyName, VerticalAlignment = VerticalAlignment.Center });
stackPanel.Children.Add(b);
ee.Column.Header = stackPanel;
Popup pop = new Popup() { Name = "pop_" + ee.PropertyName, Placement = PlacementMode.Bottom, PlacementTarget = b, StaysOpen = false, Width = 200, Margin = new Thickness(3) };
Border bord = new Border() { Background = Brushes.White, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1,1,1,1) };
pop.DataContext = bord;
HeaderPopups.Add(ee.PropertyName, pop);
StackPanel stack = new StackPanel() { Margin = new Thickness(5, 5, 5, 15) };
bord.DataContext = stack;
StackPanel stackButtons = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(0, 0, 0, 15) };
Button bAll = new Button() { Margin = new Thickness(0, 0, 0, 0), Name = "btnAll_" + ee.PropertyName };
bAll.Click += btnAllClick;
TextBlock txtAll = new TextBlock() { Text = "Select All", Foreground = Brushes.Blue, Cursor = Cursors.Hand };
bAll.Content = txtAll;
Button bNone = new Button() { Margin = new Thickness(10, 0, 0, 0), Name = "btnNone_" + ee.PropertyName };
bNone.Click += btnNoneClick;
TextBlock txtNone = new TextBlock() { Text = "Select None", Foreground = Brushes.Blue, Cursor = Cursors.Hand };
bNone.Content = txtNone;
stackButtons.Children.Add(bAll);
stackButtons.Children.Add(bNone);
ListBox list = new ListBox() { Name = "lst_" + ee.PropertyName, BorderThickness = new Thickness(0) };
stack.Children.Add(stackButtons);
stack.Children.Add(list);
};
So for each column a popup is generated and I have the popups with the keys Spec_No, Grade and Class in my HeaderPopups dictionary.
I want the appropriate popups to show up beneath the clicked button, like in the example from http://www.jarloo.com/excel-like-autofilter-in-wpf/
Look here:
My problem is to open these popups in HeaderFilterButtonClick-Event. I tried it with:
private void HeaderFilterButtonClick(object sender, RoutedEventArgs e)
{
Button b = sender as Button;
txtTest.Text += e.OriginalSource.ToString() + Environment.NewLine;
txtTest.Text += e.Source.ToString() + Environment.NewLine;
txtTest.Text += b.Name;
if (b.Name == "btn_Spec_No")
{
HeaderPopups["Spec_No"].IsOpen = true;
}
}
but it doesn't work.
Can anybody help?
Your Popup is currently empty and thus completely invisible.
You should set the Child property of it to the Border and also set the Child property of the Border to something for it to render:
Popup pop = new Popup() { ... };
Border bord = new Border() { Background = Brushes.White, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1, 1, 1, 1) };
bord.Child = new TextBlock() { Text = "some content..." };
pop.Child = bord;
The popup is opening and rendering, but it is empty, so it can't be seen.
the problem is here
Border bord = new Border() { Background = Brushes.White, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1,1,1,1) };
pop.DataContext = bord;
Datacontext is used to set Binding targets, which an empty popup has no bindings.
You need the fill the child object instead by changing the above into
Border bord = new Border() { Background = Brushes.White, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1,1,1,1) };
pop.Child = bord;
this sets the root of the popup container to ther Border object.
You will also have to do the same with the stack panel and border
StackPanel stack = new StackPanel() { Margin = new Thickness(5, 5, 5, 15) };
bord.Child = stack;
I have created prompt input box in which user enter two values and press button and i want to return values on button click and get these values in other method.
Here is my code
public static string ShowDialog(string text, string caption)
{
Form prompt = new Form()
{
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
StartPosition = FormStartPosition.CenterScreen
};
Label textLabel = new Label() { Left = 50, Top = 10, Text = text };
Label textLabel2 = new Label() { Left = 50, Top = 55, Text = text };
textLabel2.Text = "Replace with";
TextBox textBox = new TextBox() { Left = 50, Top = 70, Width = 200 };
TextBox textBox2 = new TextBox() { Left = 50, Top = 30, Width = 200 };
Button confirmation = new Button() { Text = "Replace", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(textBox2);
prompt.Controls.Add(textLabel2);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}
private void stringReplacedToolStripMenuItem_Click(object sender, EventArgs e)
{
string promptValue = Form1.ShowDialog("Find What", "Replace");
}
I want to get values of textbox and textbox2 in other method.Thanks
Okay, just return an array with the two textbox values.
public static string[] ShowDialog(string text, string caption)
{
Form prompt = new Form()
{
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
StartPosition = FormStartPosition.CenterScreen
};
Label textLabel = new Label() { Left = 50, Top = 10, Text = text };
Label textLabel2 = new Label() { Left = 50, Top = 55, Text = text };
textLabel2.Text = "Replace with";
TextBox textBox = new TextBox() { Left = 50, Top = 70, Width = 200 };
TextBox textBox2 = new TextBox() { Left = 50, Top = 30, Width = 200 };
Button confirmation = new Button() { Text = "Replace", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(textBox2);
prompt.Controls.Add(textLabel2);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? new string[] { textBox.Text, textBox2.Text } : null;
}
private void stringReplacedToolStripMenuItem_Click(object sender, EventArgs e)
{
string[] promptValue = Form1.ShowDialog("Find What", "Replace");
var textBoxValue = promptValue[0];
var textBox2Value = promptValue[1];
}
I've tried to create a form that will create buttons based on the number of items in database
my int count is equal to the number of count in my database
I'd like to add events or functions to the created label, that when i click the label it will display in the MesssageBox the name of the label
below is my code,
int Count;
List<Panel> pnl = new List<Panel>();
List<Label> tasklevel = new List<Label>();
List<Label> taskStatus = new List<Label>();
List<Label> taskname = new List<Label>();
List<PictureBox> image = new List<PictureBox>();
for (int i = 0; i < Count; i++)
{
Panel pan = new Panel();
pan.Name = "panel" + i;
pan.BackColor = Color.White;
pan.Dock = DockStyle.Top;
pan.Padding = new Padding(10, 0, 10, 10);
pan.Height = 80;
pnl.Add(pan);
Label lbl = new Label()
{
Name = "lbl" + i,
ForeColor = Color.Black,
//Dock = DockStyle.Left,
AutoSize = false,
Width = 0,
BackColor = Color.Silver,
TextAlign = ContentAlignment.MiddleLeft,
Padding = new Padding(10, 10, 10, 10),
Text = "Designing",
}; tasklevel.Add(lbl);
Label lblname = new Label()
{
Name = "lblname" + i,
ForeColor = Color.Black,
Dock = DockStyle.Left,
AutoSize = false,
Width = 0,
BackColor = Color.Gray,
TextAlign = ContentAlignment.MiddleLeft,
Padding = new Padding(10, 10, 10, 10),
Text = "Designing",
}; taskStatus.Add(lblname);
Label tskname = new Label()
{
Name = "tskName" + i,
ForeColor = Color.FromArgb(31, 31, 31),
Font = new Font("Segoe UI", 11, FontStyle.Regular),
Dock = DockStyle.Left,
AutoSize = false,
Width = 200,
BackColor = Color.Gainsboro,
TextAlign = ContentAlignment.MiddleCenter,
Padding = new Padding(10, 10, 10, 10),
Text = "Task Name",
}; taskname.Add(tskname);
PictureBox picBox = new PictureBox()
{
Name = "picBox" + i,
Dock = DockStyle.Fill,
AutoSize = false,
Image = DreametryMIS.Properties.Resources.f1,
SizeMode = PictureBoxSizeMode.StretchImage,
BackColor = Color.FromArgb(240,240,240),
Padding = new Padding(10, 10, 10, 10),
}; image.Add(picBox);
pan.Controls.Add(picBox);
pan.Controls.Add(lblname);
pan.Controls.Add(lbl);
pan.Controls.Add(tskname);
FlowPanel.Controls.Add(pan);
}
I'm new at c#, I hope some can help me
Implement an event handler like this:
public void label_Clicked(object sender, EventArgs e)
{
Label label = sender as Label;
if (label == null) return;
MessageBox.Show("Name: " + label.Name + " Text: " + label.Text);
}
You can now add this handler to the Click event of your labels, for example:
lblname.Click += label_Clicked;
tskname.Click += label_Clicked;
When one of the labels is clicked now, the method label_Clicked gets called with the label as argument for sender. So by casting the sender to Label you can easily access the clicked label's properties.
Note that you cannot add the handler inside the label's object initializer as you did with the other properties, but need to do it in an extra statement like I showed.
You can too:
lblname.Click += delegate { nameOfUrFunction (lblname) };
private void nameOfUrFunction (Label lbl)</code>
{
}
TableCell cel2 = new TableCell();
Label lbl2 = new Label();
lbl2.Text = s;
cel2.Controls.Add(lbl2);
tr.Cells.Add(cel2);
TableCell cel3 = new TableCell();
DropDownList ddlcountry = new DropDownList();
ddlcountry.ID = s;
cel3.Controls.Add(ddlcountry);
tr.Cells.Add(cel3);
table.Rows.Add(tr);
form1.Controls.Add(table);
DropDownList ddl1 = (DropDownList)form1.FindControl("CountryId");
DropDownList ddlstate = (DropDownList)form1.FindControl("StateId");
ddl1.AutoPostBack = true;
ddl1.SelectedIndexChanged += new EventHandler(ddl1_SelectedIndexChanged);
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
}
I want create WPF controls at Runtime dynamically. In the below code, I have added StackPanel at XAML and crated Button/TextBlock/Triggers at code behind. I want to see TextBlock under button at MouseEnter action and want to hide TextBlock at MouseLeave action.
This is not working as expected.
If add Button/Textblock code at XAML (commented now) Instead of creating them at Code behind(un commented now) then it is working fine.
Please let me know how to fix this? Not able understand why this not working If I create the button at runtime instead of XAML code. Thanks
XAML
<Grid>
<StackPanel Name="stackPanel5" >
<!--
<Button Name="Button1" Cursor="Hand" Content="Press Me"/>
<Border x:Name="TooltipBrd" BorderBrush="Black" BorderThickness="10,0,10,10" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed">
<TextBlock Margin="0,10,0,0" x:Name="myTextBlock" Text="Coded Test" />
</Border>
-->
</StackPanel>
</Grid>
code
public void LoadData()
{
//VisualState vs = new VisualState();
//vs.SetValue(FrameworkElement.NameProperty, YourStateName);
Button Button1 = new Button() { Cursor = Cursors.Hand, Content = "Press Me!" };
stackPanel5.Children.Add(Button1);
Border TooltipBrd = new Border()
{
BorderBrush = Brushes.Black,
BorderThickness = new Thickness(10, 10, 10, 10),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Visibility = Visibility.Collapsed
};
TextBlock txtB1 = new TextBlock() { Text = "Lakshman", Margin = new Thickness(0, 100, 0, 0) };
TooltipBrd.Child = txtB1;
stackPanel5.Children.Add(TooltipBrd);
System.Windows.Interactivity.EventTrigger trigger1 = new System.Windows.Interactivity.EventTrigger();
trigger1.EventName = "MouseEnter";
ChangePropertyAction action1 = new ChangePropertyAction();
action1.TargetName = "TooltipBrd";
action1.PropertyName = "Visibility";
action1.Value = Visibility.Visible;
trigger1.Actions.Add(action1);
trigger1.Attach(Button1);
System.Windows.Interactivity.EventTrigger trigger2 = new System.Windows.Interactivity.EventTrigger();
trigger2.EventName = "MouseLeave";
ChangePropertyAction action2 = new ChangePropertyAction();
action2.TargetName = "TooltipBrd";
action2.PropertyName = "Visibility";
action2.Value = Visibility.Collapsed;
trigger2.Actions.Add(action2);
trigger2.Attach(Button1);
}
At the XAML, create the event like this
<Button x:Name="sample_btn" MouseEnter="sample_btn_MouseEnter" />
now at the code behind,
private void logOut_btn_MouseEnter(object sender, MouseEventArgs e)
{
//your logic on mouse enter
}
Similarly, do for mouse leave and you are good to go.
Magnus(MM8) - Given beautiful solution for this.
https://social.msdn.microsoft.com/profile/magnus%20(mm8)/?ws=usercard-mini
**Just set the TargetObject property of the actions to TooltipBrd and your code will work:**
public void LoadData() {
//VisualState vs = new VisualState();
//vs.SetValue(FrameworkElement.NameProperty, YourStateName);
Button Button1 = new Button() { Cursor = Cursors.Hand, Content = "Press Me!" };
stackPanel5.Children.Add(Button1);
Border TooltipBrd = new Border() {
BorderBrush = Brushes.Black,
BorderThickness = new Thickness(10, 10, 10, 10),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Visibility = Visibility.Collapsed
};
TextBlock txtB1 = new TextBlock() { Text = "Lakshman", Margin = new Thickness(0, 100, 0, 0) };
TooltipBrd.Child = txtB1;
stackPanel5.Children.Add(TooltipBrd);
System.Windows.Interactivity.EventTrigger trigger1 = new System.Windows.Interactivity.EventTrigger();
trigger1.EventName = "MouseEnter";
ChangePropertyAction action1 = new ChangePropertyAction();
action1.**TargetObject** = TooltipBrd;
action1.PropertyName = "Visibility";
action1.Value = Visibility.Visible;
trigger1.Actions.Add(action1);
trigger1.Attach(Button1);
System.Windows.Interactivity.EventTrigger trigger2 = new System.Windows.Interactivity.EventTrigger();
trigger2.EventName = "MouseLeave";
ChangePropertyAction action2 = new ChangePropertyAction();
action2.**TargetObject** = TooltipBrd;
action2.PropertyName = "Visibility";
action2.Value = Visibility.Collapsed;
trigger2.Actions.Add(action2);
trigger2.Attach(Button1);
}
**If you set the TargetName property you need to call the RegisterName method on the StackPanel:**
int i = 0;
public void LoadData()
{
++i;
//VisualState vs = new VisualState();
//vs.SetValue(FrameworkElement.NameProperty, YourStateName);
Button Button1 = new Button() { Cursor = Cursors.Hand, Content = "Press Me!", Height = 150, Width=150 };
stackPanel5.Children.Add(Button1);
Border TooltipBrd = new Border()
{
BorderBrush = Brushes.Black,
BorderThickness = new Thickness(10, 10, 10, 10),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Visibility = Visibility.Collapsed
};
TextBlock txtB1 = new TextBlock() { Text = "Lakshman", Margin = new Thickness(0, 100, 0, 0) };
TooltipBrd.Child = txtB1;
stackPanel5.Children.Add(TooltipBrd);
System.Windows.Interactivity.EventTrigger trigger1 = new System.Windows.Interactivity.EventTrigger();
trigger1.EventName = "MouseEnter";
ChangePropertyAction action1 = new ChangePropertyAction();
//action1.TargetObject = TooltipBrd;
action1.TargetName = "TooltipBrd"+i;
action1.PropertyName = "Visibility";
action1.Value = Visibility.Visible;
trigger1.Actions.Add(action1);
trigger1.Attach(Button1);
System.Windows.Interactivity.EventTrigger trigger2 = new System.Windows.Interactivity.EventTrigger();
trigger2.EventName = "MouseLeave";
ChangePropertyAction action2 = new ChangePropertyAction();
action2.TargetName = "TooltipBrd"+i;
//action2.TargetObject = TooltipBrd;
action2.PropertyName = "Visibility";
action2.Value = Visibility.Collapsed;
trigger2.Actions.Add(action2);
trigger2.Attach(Button1);
**stackPanel5.RegisterName("TooltipBrd"+i, TooltipBrd);**
}
When you are creating controls dynamically, you have to call the RegisterName method for the runtime or you to be able to find them using this name.
On a tap event I would like to show a popup all within code behind, but my popup is not displaying?
void PopupDisplay_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
if (sender != null)
{
p = new Popup
{
Width = 480,
Height = 580,
HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
VerticalAlignment = System.Windows.VerticalAlignment.Center
};
Border b = new Border();
b.BorderBrush = new SolidColorBrush(Colors.Gray);
b.BorderThickness = new Thickness(2);
b.Margin = new Thickness(10, 10, 10, 10);
p.Child = b;
p.IsOpen = true;
}
}
Think you're trying to Popup over a top-level control like a Pivot which is very buggy.
See Popup with Pivots
If it was a Grid, it would pop up without problem. To fix this you will have to add it to the same visual level as the Pivot like so:
<Grid x:Name="ContentPanel" Margin="0,0,0,0">
<phone:Pivot x:Name="MainDisplay">
<!-- more code -->
</phone:Pivot>
</Grid>
Then in your code-behind
// I made with a thickness of 100, so we can see the border better
Popup p;
p = new Popup
{
Width = 480,
Height = 580,
VerticalOffset = 0
};
Border b = new Border();
b.BorderBrush = new SolidColorBrush(Colors.Red);
b.BorderThickness = new Thickness(100);
b.Margin = new Thickness(10, 10, 10, 10);
b.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
b.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
p.Child = b;
// add it to the same level as the pivot to over ride pivot
this.ContentPanel.Children.Add(p);
p.IsOpen = true;