I'm trying assign materialDesign:PackIcon code behind. Here's my code.
for (int i = 0; i<PrinterNumber; i++)
{
Button btn_Restart = new Button();
btn_Restart.Name = string.Format("PrinterRestartButtton{0}", i);
btn_Restart.Width = 50;
btn_Restart.Height = 25;
btn_Restart.VerticalAlignment = VerticalAlignment.Center;
btn_Restart.HorizontalAlignment = HorizontalAlignment.Left;
btn_Restart.Content = "Restart";
//btn_Restart.Visibility = Visibility.Hidden;
btn_Restart.Click += Btn_Restart_Click;
btn_Restart.Style = (Style) Application.Current.TryFindResource("MaterialDesignRaisedDarkButton");
Grid.SetRow(btn_Restart, i + 2);
Grid.SetColumn(btn_Restart, 10);
grd_WorkArea.Children.Add(btn_Restart);
}
How to apply materialDesign:PackIcon Kind="Restart" to
btn_Restart.Content property?
Set the Content property to a PackIcon:
btn_Restart.Content = new MaterialDesignThemes.Wpf.PackIcon
{ Kind = MaterialDesignThemes.Wpf.PackIconKind.Restart };
Related
I have the following problem. I create a form with two NumericUpDown and I want to increment/decrement the value from them by 0.01. I tried to use NumericUpDownObject.Increment=0.01M, but there is no change after that, the value don't change. This is the code:
namespace Proiect1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i = 1;
int o = 1;
private void button1_Click(object sender, EventArgs e)
{
Label l = new Label();
Label l2 = new Label();
NumericUpDown t = new NumericUpDown();
NumericUpDown t2 = new NumericUpDown();
l.Top = i * 25;
l.Left = 3;
l2.Top = i * 25;
l2.Left = 63;
t.Value = 0;
t.Increment = 0.01M;
t.Top = i * 25;
t.Left = 30;
t2.Value = 0;
t2.Top = i * 25;
t2.Left = 90;
i++;
t.Size =new System.Drawing.Size(30, 30);
t2.Size = new System.Drawing.Size(30, 30);
l.Size = new System.Drawing.Size(25, 20);
l2.Size = new System.Drawing.Size(29, 20);
l.Text = "n" + o;
l2.Text = "w" + o;
o++;
panel1.Controls.Add(t);
panel1.Controls.Add(t2);
panel1.Controls.Add(l);
panel1.Controls.Add(l2);
panel1.AutoScroll = true;
}
Try this
This will setup the whole form when it's created, but only increment when clicked.
It looks like your solution is re-creating everything each time it's clicked.
P.s apols - been a while since I did Winforms/WPF
namespace Proiect1
{
public partial class Form1 : Form
{
NumericUpDown _t = new NumericUpDown();
NumericUpDown _t2 = new NumericUpDown();
Label _l = new Label();
Label _l2 = new Label();
public Form1()
{
InitializeComponent();
int i = 1;
int o = 1;
_l.Top = i * 25;
_l.Left = 3;
_l2.Top = i * 25;
_l2.Left = 63;
_t.Value = 0;
_t.Increment = 0.01;
_t.Top = i * 25;
_t.Left = 30;
_t2.Value = 0;
_t2.Top = i * 25;
_t2.Left = 90;
i++;
_t.Size =new System.Drawing.Size(30, 30);
_t2.Size = new System.Drawing.Size(30, 30);
_l.Size = new System.Drawing.Size(25, 20);
_l2.Size = new System.Drawing.Size(29, 20);
_l.Text = "n" + o;
_l2.Text = "w" + o;
o++;
panel1.Controls.Add(_t);
panel1.Controls.Add(_t2);
panel1.Controls.Add(l_);
panel1.Controls.Add(l_2);
panel1.AutoScroll = true;
}
private void button1_Click(object sender, EventArgs e)
{
_t.Increment = 0.01;
}
I need to add radio buttons dynamically in my windows form and in horizontal mode.
for (int i = 0; i <= r.Count; i++)
{
RadioButton rdo = new RadioButton();
rdo.Name = "id";
rdo.Text = "Name";
rdo.ForeColor = Color.Red;
rdo.Location = new Point(5, 30 );
this.Controls.Add(rdo);
}
You could do something like this:
FlowLayoutPanel pnl = new FlowLayoutPanel();
pnl.Dock = DockStyle.Fill;
for (int i = 0; i < 4; i++)
{
pnl.Controls.Add(new RadioButton() { Text = "RadioButton" + i });
}
this.Controls.Add(pnl);
You could also add the FlowLayoutPanel in the designer and leave that part out in the code.
To get the selected RadioButton use a construct like this:
RadioButton rbSelected = pnl.Controls
.OfType<RadioButton>()
.FirstOrDefault(r => r.Checked);
To use this the FlowLayoutPanel needs to be known in the calling method. So either add it to the Form in the designer (Thats what I would prefer) or create it as an instance member of the form and add it at runtime (this has no benefit).
You can do something like this
//This is my dynamic data list
List<ItemType> itemTypeData = new List<ItemType>()
RadioButton[] itemTypes = new RadioButton[ItemType.Count];
int locationX = 0;
for (int i = 0; i < ItemType.Count; i++)
{
var type = ItemType[i];
itemTypes[i] = new RadioButton
{
Name = type.Code,
Text = type.Code,
AutoSize = true,
Font = new System.Drawing.Font("Calibri", 11F, FontStyle.Regular),
Location = new Point(156 + locationX, 88),
};
this.Controls.Add(itemTypes[i]);
locationX += 80;
}
This works fine for me
on startup I'm generating a lot of controls 90 to be exact and everything is working ok EXCEPT for the labels they are not being drawn or something? they are there because I can click them and they show proper ID (click event) here's the genereation code
private static bool ClientsLoaded = false;
private static WebBrowser[] Clients = new WebBrowser[45];
private static Label[] ClientLabel = new Label[45];
private static int MaximizedClient = -1;
public Form1()
{
InitializeComponent();
int WBoffsetX = 0;
int WBoffsetY = 0;
int lbloffsetX = 0;
int lbloffsetY = 0;
for (int i = 0; i < 45; i++)
{
var wb = new WebBrowser();
Clients[i] = wb;
wb.ScrollBarsEnabled = false;
wb.Height = 12;
wb.Width = 12;
wb.Location = new Point(2 + WBoffsetX, 2 + WBoffsetY);
WBoffsetX += 13;
wb.ScriptErrorsSuppressed = true;
this.Controls.Add(wb);
ClientLabel[i] = new Label();
ClientLabel[i].Name = "lbl_" + i;
ClientLabel[i].Font = new Font("Arial", 12);
ClientLabel[i].ForeColor = System.Drawing.Color.White;
ClientLabel[i].Location = new Point(12 + lbloffsetX, 450 + lbloffsetY);
lbloffsetX += 22;
ClientLabel[i].Click += new EventHandler(lbl_click);
ClientLabel[i].Text = "C" + i + ": o";
this.Controls.Add(ClientLabel[i]);
}
}
I've tried adding a button with for(45) clientlabel[i].Refresh() and it did nothing I tried changing the visibilty of them all to false and then back to true and nothing however I did find 1 thing interesting if I hide lbl_1 label 2 text will appear if I had label 2 label 3 text will appear but if I change the previous label back to visible they stay invisible textwise
I can click in a line on the form and
private void lbl_click(object sender, EventArgs e)
{
int id = -1;
var s = sender.ToString();
for(int i = 0; i<=45; i++)
{
if (s.Contains("C" + i + ":"))
{
id = i;
}
}
MessageBox.Show("Hello label, " + id);
}
will pop up the proper ids etc
does anyone know what's causing this maybe? or how to fix it
Well, I don't know what is the problem. This code works well enough and it has only marginal differences with the original(AutoSize property, explicit statement of Height and Width, and minor Location adjustment):
for (int i = 0; i < ClientLabel.Length; i++)
{
// Web browsers
WebBrowser wb = new WebBrowser()
{
ScrollBarsEnabled = false,
Height = 12,
Width = 12,
Location = new Point(2 + WBoffsetX, 2 + WBoffsetY),
ScriptErrorsSuppressed = true
};
WBoffsetX += 13;
Clients[i] = wb;
// Labels
Label label = new Label()
{
Name = "label_" + i,
Text = "Data",
AutoSize = true,
Location = new Point(50 + lbloffsetX, 50 + lbloffsetY),
Width = 100,
Height = 20,
Font = new Font("Arial", 12),
ForeColor = System.Drawing.Color.White,
};
label.Click += new EventHandler(lbl_click);
ClientLabel[i] = label;
lbloffsetX += 30;
}
this.Controls.AddRange(Clients);
this.Controls.AddRange(ClientLabel);
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);
}
}
I have a WPF form that I have created programatically. It is basically a list of items with 2 DatePicker objects to specify a range of dates. I have everything working for the most part, but I need to fire some logic on the SelectedDateChanged event.
The problem is, since the DatePicker's are generated dynamically based on selections from a previous form, I need to use an anonymous listener (or whatever you call them in .NET). I have been unable to do it the way I know how and I cant seem to find any example or help through google either. Thanks in advance for any tips.
Generating DatePickers:
public SystemInterval(Role role)
{
this.role = role;
InitializeComponent();
lbls = new Label[role.RoleSystems.Length];
dp = new DatePicker[role.RoleSystems.Length * 2];
cks = new CheckBox[role.RoleSystems.Length];
ScrollViewer sv = new ScrollViewer();
sv.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
sv.VerticalAlignment = System.Windows.VerticalAlignment.Top;
sv.Margin = new Thickness(0,50,0,40);
Grid g = new Grid();
g.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
g.VerticalAlignment = System.Windows.VerticalAlignment.Top;
ColumnDefinition col1 = new ColumnDefinition();
col1.Width = new GridLength(46);
ColumnDefinition col2 = new ColumnDefinition();
col2.Width = GridLength.Auto;
ColumnDefinition col3 = new ColumnDefinition();
col3.Width = new GridLength(150);
ColumnDefinition col4 = new ColumnDefinition();
col4.Width = new GridLength(150);
g.ColumnDefinitions.Add(col1);
g.ColumnDefinitions.Add(col2);
g.ColumnDefinitions.Add(col3);
g.ColumnDefinitions.Add(col4);
sv.Content = g;
LayoutRoot.Children.Add(sv);
for (int r = 0; r < cks.Length; r++)
{
g.RowDefinitions.Add(new RowDefinition());
}
g.Height = (lbls.Length * 25) + (lbls.Length * 5);
for (int i = 0, j = 0; i < cks.Length; i++, j+=2)
{
cks[i] = new CheckBox();
cks[i].IsChecked = false;
cks[i].VerticalAlignment = System.Windows.VerticalAlignment.Center;
cks[i].HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
Grid.SetColumn(cks[i], 0);
Grid.SetRow(cks[i], i);
g.Children.Add(cks[i]);
lbls[i] = new Label();
lbls[i].Height = 25;
lbls[i].Content = role.RoleSystems[i].SystemName;
lbls[i].VerticalAlignment = System.Windows.VerticalAlignment.Center;
lbls[i].HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
Grid.SetColumn(lbls[i], 1);
Grid.SetRow(lbls[i], i);
g.Children.Add(lbls[i]);
dp[j] = new DatePicker();
dp[j].Height = 25;
dp[j].Width = 115;
dp[j].VerticalAlignment = System.Windows.VerticalAlignment.Center;
dp[j].HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
dp[j].IsEnabled = false;
dp[j].BlackoutDates.Add(new CalendarDateRange(new DateTime(0001, 1, 1), DateTime.Now.Subtract(TimeSpan.FromDays(1))));
Grid.SetColumn(dp[j], 2);
Grid.SetRow(dp[j], i);
g.Children.Add(dp[j]);
dp[j + 1] = new DatePicker();
dp[j + 1].Height = 25;
dp[j + 1].Width = 115;
dp[j + 1].VerticalAlignment = System.Windows.VerticalAlignment.Center;
dp[j + 1].HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
dp[j + 1].IsEnabled = false;
dp[j + 1].BlackoutDates.Add(new CalendarDateRange(new DateTime(0001, 1, 1), DateTime.Now));
Grid.SetColumn(dp[j + 1], 3);
Grid.SetRow(dp[j + 1], i);
g.Children.Add(dp[j + 1]);
cks[i].Click += new RoutedEventHandler(delegate(object s, RoutedEventArgs re)
{
CheckBox cb = (CheckBox)re.Source;
for (int r = 0; r < cks.Length; r++)
{
if (cks[r].Equals(cb))
{
dp[r * 2].IsEnabled = true;
dp[r * 2 + 1].IsEnabled = true;
}
}
});
}
The checkboxs have an anonymous event handler, however this does not work for the DatePicker.SelectedDateChanged event.
Do you mean an event handler using an anonymous method like this?
var dp = new DatePicker();
dp.SelectedDateChanged +=
(sender, args) =>
{
var picker = (DatePicker) sender;
/* do stuff here */
};