I'm trying to make a list of numericUpDown.value and, when I press the button, it sets a random value from the presenter; but, when I press the button nothing happen, I see the value change but I think it just replace the numeric control with a value instead of set value.
The view :
public partial class Form1 : Form, IForm1
{
public List<decimal> _valueList { get; set; }
public Form1()
{
InitializeComponent();
this._valueList = new List<decimal> { numericUpDown1.Value, numericUpDown2.Value, numericUpDown3.Value, numericUpDown4.Value, numericUpDown5.Value, numericUpDown6.Value };
}
public List<decimal> ValueList
{
get => _valueList; set => _valueList = value;
}
public event EventHandler ButtonClick;
private void button1_Click(object sender, EventArgs e)
{
ButtonClick?.Invoke(sender, e);
}
}
Interface:
public interface IForm1
{
List<decimal> ValueList { get; set; }
event EventHandler ButtonClick;
}
Presenter:
public class PresenterForm1
{
private IForm1 _form1;
public PresenterForm1(IForm1 form1)
{
_form1 = form1;
form1.ButtonClick += ButtonClick;
}
private void ButtonClick(object sender, EventArgs e)
{
var random = new Random();
for(int i = 0; i < 6; i++)
{
_form1.ValueList[i] = random.Next(0, 10);
}
}
If I change the type to List<NumericUpDown> it works, but I think this is the wrong way:
foreach (var item in _form1.ValueList)
{
item.Value = random.Next(0, 99);
}
Related
I need to display the picturebox that is formed in the main form in the usercontrol, but also save subscriptions to this picturebox.
In this class, I fill in the fields and then use foreach to pass the picturebox to the usercontrol
public class UserCtr
{
public event Action<List<Users>> ClickPicture;
public event Action<List<Users>, Group> DoubleClickPicture;
public Group group { get; set; }
public PictureBox picture { get; set; }
public List<Users> users { get; set; }
public UserCtr(Group group, PictureBox picture, List<Users> users)
{
this.group = group;
this.picture = picture;
this.users = users;
this.picture.Click += PictureClick;
this.picture.MouseDoubleClick += PictureDoubleClick;
}
private void PictureClick(object s, EventArgs e)
{
ClickPicture(users);
}
private void PictureDoubleClick(object s, EventArgs e)
{
DoubleClickPicture(users, group);
}
}
foreach loop
foreach (var item in groups.users)
{
item.picture.Image = yellow;
panel.Controls.Add(new UserControl1(item.picture)
{
Text = item.user.Name,
//pb = item.picture
});
}
My usercontrol
public partial class UserControl1 : UserControl
{
public UserControl1(PictureBox pic)
{
InitializeComponent();
Load += (s, e) => pictureBox1 = pic;
Invalidate();
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
label1.Text = Text;
}
}
}
The label is filled in, and the picturebox is empty
I want to create a C# Windows Form application where there is a flow layout panel, filled with User Control elements. Every time I click an element, the some labels must show its attributes. However, the click event doesn't register. I added some breakpoints on the show_preview() method and it never calls the method. The Load event is calling show_preview but I want to do change the labels acordinging to a User Control being clicked.
This is the Form:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
load_plots();
}
private void load_plots()
{
ArrayList items = new ArrayList();
for (int i = 0;i<100;i++)
{
PlotItem plot = new PlotItem();
plot.Place = "Ilion";
plot.Price = i * 1000;
plot.Space = i * 10;
plot.Title = "Plot No." + i;
plot.Width = flowLayoutPanel1.Width - 30;
plot.Click += new EventHandler(this.show_preview);
items.Add(plot);
flowLayoutPanel1.Controls.Add(plot);
}
}
private void show_preview(object sender, EventArgs e)
{
PlotItem clicked = sender as PlotItem;
plot_title.Text = "Hello";
plot_size.Text = clicked.Space.ToString();
plot_value.Text = clicked.Price.ToString();
}
This is the Plot Item:
public partial class PlotItem : UserControl
{
public PlotItem()
{
InitializeComponent();
}
#region Properties
[Category("Plot Object")]
private String _place;
public String Place
{
get { return _place; }
set { _place = value; }
}
[Category("Plot Object")]
private double _price;
public double Price
{
get { return _price; }
set { _price = value; label_price.Text = value.ToString() + "€"; }
}
[Category("Plot Object")]
private String _use;
public String Use
{
get { return _use; }
set { _use = value; }
}
[Category("Plot Object")]
private double _space;
public double Space
{
get { return _space; }
set { _space = value; label_size.Text = value.ToString() + "m2"; }
}
[Category("Plot Object")]
private String _title;
public String Title
{
get { return _title; }
set { _title = value; label_title.Text = value; }
}
[Category("Plot Object")]
private Image _icon;
public Image Icon
{
get { return _icon; }
set { _icon = value; plot_preview.Image = value; }
}
#endregion
private void panel1_MouseEnter(object sender, EventArgs e)
{
this.BackColor = Color.Silver;
}
private void panel1_MouseLeave(object sender, EventArgs e)
{
this.BackColor = System.Drawing.SystemColors.Control;
}
Thanks for your time.
Hello I want to change the position of a form2 created from form1 with a thread in an other thread created in form2 with a struct
Edit:
I made an other post
There is an other problem,
How can I make Form2 stop thinking?
This is a similar code to keep it simple.
When Form1 is loaded a thread is created this thread runs a method with a infinite loop in it and at some time creates Form2 and keeps on looping the problem is that Form2 never stops thinking.
public partial class Form1 : Form
{//Form1
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread childThread = new Thread(new ThreadStart(loop);
childThread.Start();
}
public void loop()
{
int i = 0;
while (true)
{
if (i == 45)
{
Form2 f = new Form2();
f.Show();
}
i = i + 1;
}
}
}
}
public partial class Form2 : Form
{//Form2
public Form2()
{
InitializeComponent();
}
public struct Rect
{
public int Left { get; set; }
public int Top { get; set; }
public int Right { get; set; }
public int Bottom { get; set; }
}
private void Form2_Load(object sender, EventArgs e)
{
Thread childThread = new Thread(new ThreadStart(method));
childThread.Start();
}
public void method()
{
Rect move = new Rect();
move.Left = 100;
move.Top = 100;
this.Invoke(new MethodInvoker(() => { mover(move); }));
}
public void mover(Rect move)
{
this.Left = move.Left;
this.Top = move.Top;
}
}
You should create a method
void SetLeft(int left)
{
this.Left = left;
}
That you can invoke it from any thread:
if (this.InvokeRequired) {
this.Invoke(new MethodInvoker(() => { SetLeft(10); } );
} else {
SetLeft(10);
}
I have some label that should display actual amount of items that contain BindingList that bound to the DataGridView.
I tried to bind in this way:
CountOfLoadedItemsLabel.DataBindings.Add("Text", _items.Count, String.Empty);
But when BindingList updates, the label that bound to its Count property not changes.
Never used BindingList<T> but this worked for me:
public partial class Form1 : Form
{
private BindingList<Test> list;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.list = new BindingList<Test>
{
new Test(1,"Entry"),
new Test(2,"Another Entry")
};
dataGridView1.DataSource = new BindingSource(list,null);
list.ListChanged += list_ListChanged;
list.Add(new Test(3, "After Binding"));
}
void list_ListChanged(object sender, ListChangedEventArgs e)
{
CountOfLoadedItemsLabel.Text = string.Format("Items: {0}", list.Count);
}
}
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
public Test(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
Still learning c# and messing with GUI's, I ran into this problem and I have looked at the code over and over again and do not understand why my Order class will not be instantiated. Evrything looks good to me. Is there a basic concept I am missing here?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
newOrder = new Order ();
for (int i = 0; i < newOrder.menuEntree.Length; i++)
{
this.listBox.Items.Add(newOrder.menuEntree[i]);
}
}
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
newOrder.Entree = this.listBox.Text;
}
public class Order
{
public string[] pastryEntree = new string[] { "Baklava", "Croissant", "Blueberry Muffin" };
public decimal[] pastryPrice = new decimal[] { 3.00m, 2.50m, 1.75m };
private string entree;
private decimal entreePrice;
public Order()
{
entree = "";
entreePrice = 0;
}
public string Entree
{
get
{
return entree;
}
set
{
entree = value;
SetEntreePrice();
}
}
public decimal EntreePrice
{
get
{
return entreePrice;
}
}
public void SetEntreePrice()
{
for (int i = 0; i < pastryPrice.Length; i++)
{
if (pastryEntree[i] == entree)
{
entreePrice = pastryPrice[i];
}
}
}
}
}
}
You have not defined newOrder as a member of your Form1 class, and so the variable is undeclared when you try to instantiate it.
public partial class Form1 : Form
{
Order newOrder;
....
}
You might want to replace this:
private void Form1_Load(object sender, EventArgs e)
{
newOrder = new Order ();
for (int i = 0; i < newOrder.menuEntree.Length; i++)
{
this.listBox.Items.Add(newOrder.menuEntree[i]);
}
}
with
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < newOrder.menuEntree.Length; i++)
{
Order newOrder = new Order ();
this.listBox.Items.Add(newOrder.menuEntree[i]);
}
}
It is likely that you want to declare order within the for-loop. Otherwise all the orders would be the same instance.