can anyone answer my question in the title?
It goes like this
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
public void Load_Main_Function()
{
MessageBox.Show("I'm called from the User Control inside flowpanellayout");
}
}
public partial class FlowPanel : UserControl
{
public FlowPanel()
{
InitializeComponent();
}
public void Load_Sample_Forms()
{
for (int i = 0; i < 5; i++)
{
Item_User_Control Item = new Item_User_Control();
Item.Name.Text = "Test Item " + i;
Item.Price.Text = i;
Item.button.Name = i;
flowLayoutPanel1.Controls.Add(Item);
}
}
}
public partial class Item_User_Control : UserControl
{
public Item_User_Control()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
//Here comes my problem..
//The method I tried
Main mainForm = new Main();
mainForm.Load_Main_Function();
//I was thinking that this Item_User_Control was cast inside the loop of Flowpanel Form
//that there must be a conflict somehow or this form can't be seen during the inialization
//of Main Form and that's why the function in Main Form can't be called from
//Item_User_Control button event
}
}
I'm making an POS using C# Windows Forms and I'm stuck in this problem:) The layout works it shows 5 Item_User_Control inside flowpanellayout, it's just when clicking the button, nothing happens
need help guys, thanks!!!
Basic information about the event system can be found here
In your code you could do it something like:
public partial class Main : Form
{
public Main()
{
InitializeComponent();
FlowPanel.FlowItemClicked += FlowPanel_FlowItemClicked;
}
private void FlowPanel_FlowItemClicked(object sender, EventArgs e)
{
MessageBox.Show("I'm called because A flow item in the flow panel was clicked.");
}
}
public partial class FlowPanel : UserControl
{
public event EventHandler FlowItemClicked; // Event to notify any item getting clicked.
public FlowPanel()
{
InitializeComponent();
}
public void Load_Sample_Forms()
{
for (int i = 0; i < 5; i++)
{
Item_User_Control Item = new Item_User_Control();
Item.Name.Text = "Test Item " + i;
Item.Price.Text = i;
Item.button.Name = i;
// Handle FlowItemClicked event from this item.
item.ItemClicked += (o,e) => { FlowItemClicked?.Invoke(this, EventArgs.Empty;) };
flowLayoutPanel1.Controls.Add(Item);
}
}
}
public partial class Item_User_Control : UserControl
{
public event EventHandler ItemClicked; // Event to notify the item getting clicked.
public Item_User_Control()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
// When the button gets clicked, raise the ItemClicked event.
ItemClicked?.Invoke(this, EventArgs.Empty);
}
}
You can try to make an instance of your Main Form "Globally Accessible":
// On Main Form
public partial class Main : Form
{
public static Main Instance { get; private set; }
public Main()
{
InitializeComponent();
Instance = this; // Initiate an instance of your form
}
public void Load_Main_Function()
{
MessageBox.Show("I'm called from the User Control inside flowpanellayout");
}
}
// On FlowPanel - your custom control. Remember to call your Load_Sample_Forms() method.
public partial class FlowPanel : UserControl
{
public FlowPanel()
{
InitializeComponent();
Load_Sample_Forms();
}
public void Load_Sample_Forms()
{
for (int i = 0; i < 5; i++)
{
Item_User_Control Item = new Item_User_Control();
Item.Name = "Test Item " + i;
Item.Price.Text = i.ToString();
Item.button.Name = i.ToString();
flowLayoutPanel1.Controls.Add(Item);
}
}
}
// On Item_User_Control
public partial class Item_User_Control : UserControl
{
public Item_User_Control()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
Main.Instance.Load_Main_Function(); // Execute any main function regardless of signature;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.