Refresh user control instance inside a panel when a button is clicked - c#

I have these codes in my Mainform that when I click on a picturebox, a user control then loads in the a panel.
My problem is the datagridviews inside other controls doesn't get updated. When I make transactions in other user controls that affects other user controls.
What I want now is to automatically update those gridviews by refreshing the user control when their associated button is clicked.
private void pictureBox5_Click(object sender, EventArgs e)
{
panel2.Controls.Add(UCCustomer.Instance);
UCCustomer.Instance.Dock = DockStyle.Fill;
UCCustomer.Instance.BringToFront();
lblMenu.Text = "Customer Management";
}
private void pictureBox2_Click(object sender, EventArgs e)
{
panel2.Controls.Add(UCDelivery.Instance);
UCDelivery.Instance.Dock = DockStyle.Fill;
UCDelivery.Instance.BringToFront();
lblMenu.Text = "Delivery Management";
}
private void pictureBox3_Click(object sender, EventArgs e)
{
panel2.Controls.Add(UCReport.Instance);
UCReport.Instance.Dock = DockStyle.Fill;
UCReport.Instance.BringToFront();
lblMenu.Text = "Reports";
}
Here's an example on how I declared the instance for a user control. This is in the same format as other user controls.
private static UCCustomer _instance;
public static UCCustomer Instance
{
get
{
if (_instance == null)
_instance = new UCCustomer();
return _instance;
}
}

Related

Drag an item in listview to a form

I have two forms; one of them is containing listview, and another one is just a form.
I want to make a thing :
If I drag an item in listview to a Form, a messagebox would be pop up.
and the message would be text of the item.
However I don't know why 'SelectedItem' is null. When I trace the SelectedItem, it was null.
I found I have to use MouseDown and DragDrop events, but I have no idea how to use.
First one is the listview's code :
rListCtrl.MouseDown += rListCtrl_MouseDown;
rListCtrl.DragDrop += rListCtrl_DragDrop;
private void rListCtrl_MouseDown(object sender, MouseEventArgs e)
{
StringBuilder sb = new STringBuilder();
sb.Append(radListView1.SelectedItem.ToString());
testName = sb.ToString();
}
private void rListCtrl_DragDrop(object sender, DragEventArgs e){
{
MessageBox.Show(testName);
}
radListView1 is the name of listview.
The reason why SelectedItem is null, is that the Item only gets selected when you actually perform a click, not a mere MouseDown.
You can, however, use the IndexFromPoint method to get the Item the mouse had been placed on when the MouseDown Event was evoked:
private void radListView1_MouseDown(object sender, MouseEventArgs e)
{
int index = radListView1.IndexFromPoint(e.Location);
radListView1.SelectedIndex = index;
testName = radListView1.Items[index].ToString();
}
private void rListCtrl_DragDrop(object sender, DragEventArgs e){
{
MessageBox.Show(testName);
}
Form1:
public partial class Form1 : Form
{
Form2 f = new Form2();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
f.Show();
}
private void Form1_MouseEnter(object sender, EventArgs e)
{
if(f.data!= string.Empty)
{
MessageBox.Show(f.data);
f.data = string.Empty;
}
}
}
Form2:
public partial class Form2 : Form
{
public string data = string.Empty;
public Form2()
{
InitializeComponent();
listView1.ItemDrag += doDaragItem;
}
private void doDaragItem(Object sender, ItemDragEventArgs e)
{
data = e.Item.ToString();
}
}
Ron,
RadListView from the Telerik UI from WinForms suite handles the whole drag and drop operation by its ListViewDragDropService. Its PreviewDragOver event allows you to control on what targets the item(s) being dragged can be dropped on. The PreviewDragDrop event allows you to get a handle on all the aspects of the drag and drop operation, the source (drag) list view, the destination (target) control, as well as the item being dragged. Additional information is available in the following help article: https://docs.telerik.com/devtools/winforms/controls/listview/drag-and-drop/listviewdragdropservice
https://docs.telerik.com/devtools/winforms/controls/listview/drag-and-drop/drag-and-drop-using-raddragdropservice
You can also combine the RadDragDropService and OLE drag-and-drop functionality: https://docs.telerik.com/devtools/winforms/controls/listview/drag-and-drop/combining-raddragdropservice-and-ole-drag-and-drop
As to the specific code snippet, indeed, if you don't have a selected item in RadListView, the code in the MouseDown event won't extract the item's text. You need to get the element under the mouse and set the item as selected:
private void radListView1_MouseDown(object sender, MouseEventArgs e)
{
SimpleListViewVisualItem elementUnderMouse = this.radListView1.ElementTree.GetElementAtPoint(e.Location) as SimpleListViewVisualItem;
if (elementUnderMouse != null)
{
this.radListView1.SelectedItem = elementUnderMouse.Data ;
}
StringBuilder sb = new StringBuilder();
sb.Append(radListView1.SelectedItem.Text.ToString());
{
testName = sb.ToString();
}
MessageBox.Show(testName);
}
I hope this information helps.

How to initiate a button click of another page in a method from another cs file

I have a cs file for Drag drop Element. In the Method after the drop is completed (OnManipulationCompleted) I would like to initiate/trigger the 3 button clicks from another WPF page which has 3 buttons within the OnManipulationCompleted method.
namespace KinectDemos
{
public class DragDropElementController : IKinectManipulatableController
{
private ManipulatableModel _inputModel;
private KinectRegion _kinectRegion;
private DragDropElement _dragDropElement;
private bool _disposedValue;
public DragDropElementController(IInputModel inputModel, KinectRegion kinectRegion)
{
_inputModel = inputModel as ManipulatableModel;
_kinectRegion = kinectRegion;
_dragDropElement = _inputModel.Element as DragDropElement;
_inputModel.ManipulationStarted += OnManipulationStarted;
_inputModel.ManipulationUpdated += OnManipulationUpdated;
_inputModel.ManipulationCompleted += OnManipulationCompleted;
}
private void OnManipulationCompleted(object sender,
KinectManipulationCompletedEventArgs kinectManipulationCompletedEventArgs)
{**HERE I WOULD LIKE TO INITIATE THE BUTTON CLICKS**
}
The Other Wpf page which has these buttons having function for three buttons .After each button drop it would navigate to another page.
Public partial class Beauty : Usercontrol
{
Public void Tip_Click (object sender, RoutedEventArgs e)
{
Afterdrop page1 = new Afterdrop
this.content = page1;
}
Public void Tricks_Click (object sender, RoutedEventArgs e)
{
Afterdrop2 page2 = new Afterdrop2
this.content = page2;
}
Public void Invent_Click (object sender, RoutedEventArgs e)
{
Afterdrop3 page3 = new Afterdrop3
this.content = page3;
}
}
How will I do that ?Please help
You need to have an instance of the page you want to trigger events on. If that page exists then you can get that instance based on your application architecture otherwise there is no way to call non-static members without instantiating an object.
Once you get that page instance call the respective event handlers as methods. As an example if that instance is named targetPage then
targetPage.Tip_Click(null, new EventArgs());

How to show the previous User Control from current User Control?

I am new to C#. I am using windows forms.
As shown in screenshot, I have form1 with 4 buttons and 4 user controls.
click show UserControl1 and UserControl1 shows up.
click show UserControl2 and UserControl2 shows up.
click show UserControl3 and UserControl3 shows up.
click show UserControl4 and UserControl4 shows up.
what I want to do is: when I click Show Previous UC (on User Control4) , the previous User Control shows up.
for example:
click show UserControl2 and UserControl2 shows up.
click show UserControl4 and UserControl4 shows up.
and now I want when I click Show Previous UC, UserControl2 shows up (the previous user control).
How Can I show the previous User Control ( the last one which was shown before the current one)?
Please help me, Thank you.
public partial class Form1 : Form
{
UserControl1 UC1 = new UserControl1();
UserControl2 UC2 = new UserControl2();
UserControl3 UC3 = new UserControl3();
UserControl4 UC4 = new UserControl4();
public Form1()
{
InitializeComponent();
Controls.Add(UC1);
Controls.Add(UC2);
Controls.Add(UC3);
Controls.Add(UC4);
}
private void ShowUserControl1_Click(object sender, EventArgs e)
{
UC1.Visible = true;
UC2.Visible = false;
UC3.Visible = false;
UC4.Visible = false;
}
private void ShowUserControl2_Click(object sender, EventArgs e)
{
UC2.Visible = true;
UC1.Visible = false;
UC3.Visible = false;
UC4.Visible = false;
}
private void ShowUserControl3_Click(object sender, EventArgs e)
{
UC3.Visible = true;
UC1.Visible = false;
UC2.Visible = false;
UC4.Visible = false;
}
private void ShowUserControl4_Click(object sender, EventArgs e)
{
UC4.Visible = true;
UC3.Visible = false;
UC2.Visible = false;
UC1.Visible = false;
}
}
You need to keep track of which user control was the previous in the page (and possible also the current, unless you want to iterate the UCs to find out). Use fields for this in the page.
public partial class Form1 : Form
{
UserControl[] userControls = new []{
new UserControl1(),
new UserControl2(),
new UserControl3(),
new UserControl4()
};
UserControl previous;
UserControl current;
public Form1()
{
InitializeComponent();
foreach(var uc in UserControls)
{
uc.Click += ShowPrevControl_Click;
Controls.Add(uc);
}
}
In the event (you only need one)
private void ShowUserControl_Click(object sender, EventArgs e)
{
foreach(UserControl uc in UserControls)
{
if(uc.Name == (string)((Control)sender).Tag)
{
previous = current;
uc.Visible = true;
current = uc;
}
else
{
uc.Visible = false;
}
}
}
private void ShowPrevControl_Click(object sender, EventArgs e)
{
if (previous != null)
{
foreach(var uc in UserControls)
{
uc.Visible = false;
}
var temp = current;
previous.Visible = true;
current = previous;
previous = temp;
}
}
Set the Tag property for each button to hold the name of the UserControl it should control (UserControl1, UserControl2, UserControl3 or UserControl4).
Let all the buttons click events be handled by ShowUserControl_Click.
Create a new public event in your user controls (if all should handle clicks), that that page can handle using your ShowPrevControl_Clickmethod:
public UserControlx : UserControl
{
public event EventHandler Click;
public UserControlx()
{
Button.Click += Button_Click;
}
private void Button_Click(object sender, EventArgs e)
{
if(Click != null)
Click(this, EventArgs.Empty);
}
}
An easy solution is to add a member variable to your class.
So your class would look like:
public partial class Form1 : Form
{
UserControl prevControl;
// Etc...
So when you click the button for, say, UC2, you can set prevControl to UC2.
private void ShowUserControl2_Click(object sender, EventArgs e)
{
UC2.Visible = true;
prevControl = UC2;
UC1.Visible = false;
UC3.Visible = false;
UC4.Visible = false;
}
And on an event handler for the "Show previous control" button:
private void ShowPrevControl_Click(object sender, EventArgs e)
{
if (prevControl != null) prevControl.Visible = true;
}
UserControl is a reference in C#, which makes this possible.

Change background image of form as soon as button on another form is clicked

I am having two forms (A and B). In form B there are many buttons having different background image. On clicking any of the button I want to change the background image of form A to the background image of the button which was clicked instantly as it is always open behind the form.
formA mai = new formA();
private void button1_Click(object sender, EventArgs e)
{
mai.BackgroundImage = button1.BackgroundImage;
}
This is the code I am using although it changes the background image it doesn't change instantly but if I will open and close the form the background image will be changed.
I don't need like that I need it to change instantly.
Add a field in formB to refer to the formA instance which you want to change its BackgroundImage; and initialize it when you call formB
formB's code-behind:
public partial class formB : Form
{
public formA owner;
public formB()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (owner != null)
owner.BackgroundImage = button1.BackgroundImage;
}
private void button2_Click(object sender, EventArgs e)
{
if (owner != null)
owner.BackgroundImage = button2.BackgroundImage;
}
private void button3_Click(object sender, EventArgs e)
{
if (owner != null)
owner.BackgroundImage = button3.BackgroundImage;
}
}
formA's code-behind:
public partial class formA : Form
{
public formA()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
formB b = new formB();
b.owner = this;
b.ShowDialog();
}
}
Add this.Refresh()
formA mai = new formA();
private void button1_Click(object sender, EventArgs e)
{
mai.BackgroundImage = button1.BackgroundImage;
mai.BringToFront();
mai.Refresh();
}
Call mai.Invalidate() after setting the new image.

Is this the right way to show/hide user controls?

I just want to ask if this is good way of showing/hiding the user controls on the main form.
I have form1 with 3 buttons (button1, button2,button3) and I have user controls (user control1, user control2,user control3 and they contain nothing).
now click button1 and user control1 shows up, and click button2 and user control2 shows up and user control1 hides.... and so on ( so ever time you click a button a user controll shows up and hides the rest.
I used the following code and it worked perfectly as i wanted but my question is:
the UserControl.BringToFront() function brings the user control to the front and every time you click a button it brings that usercontrol to front, so what happens to other user controls? I mean the BringToFront() kind of places each user control on top of another and does not remove any previous user controls. I feel like something missing, something like "Remove" function to remove the previous UserControl. And what happens if I leave my code like this (without "Remove" function? Please help. Thank you.
Here is the code and it works very well:
user control1 name is UC1
user control2 name is UC2
user control2 name is UC3
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
UC1 uc1 = new UC1();
Controls.Add(uc1);
uc1.BringToFront();
}
private void button2_Click(object sender, EventArgs e)
{
UC2 uc2 = new UC2();
Controls.Add(uc2);
uc2.BringToFront();
}
private void button3_Click(object sender, EventArgs e)
{
UC3 uc3 = new UC3();
Controls.Add(uc3);
uc3.BringToFront();
}
}
Do not recreate the controls each time you press the buttons, just show/hide the controls:
private UC1 uc1 = new UC1() {
Visible = false
};
private UC2 uc2 = new UC2() {
Visible = false
};
private UC3 uc3 = new UC3() {
Visible = false
};
private void VisualizeUC(Control value) {
uc1.Visible = false;
uc2.Visible = false;
uc3.Visible = false;
value.Visible = true;
}
private void Form1_Load(object sender, EventArgs e) {
Controls.Add(uc1);
Controls.Add(uc2);
Controls.Add(uc3);
}
private void button1_Click(object sender, EventArgs e) {
VisualizeUC(uc1);
}
private void button2_Click(object sender, EventArgs e) {
VisualizeUC(uc2);
}
private void button3_Click(object sender, EventArgs e) {
VisualizeUC(uc3);
}
You were previously creating a new instance of each control every time a button was pressed which quickly adds up to lots of unnecessary controls.
Instead create and add only one instance of each control, and then hide/show as necessary:
// Put your controls here so they're accessible
UC1 uc1;
UC2 uc2;
UC3 uc3;
private void Form1_Load(object sender, EventArgs e)
{
// Do this on form load so it only happens once
// Instantiate your controls
uc1 = new UC1();
uc2 = new UC2();
uc3 = new UC3();
// Make them invisible
uc1.Visible = false;
uc2.Visible = false;
uc3.Visible = false;
// Add your controls
Controls.Add(uc1);
Controls.Add(uc2);
Controls.Add(uc3);
}
private void button1_Click(object sender, EventArgs e)
{
// You can keep using bring to front
uc1.BringToFront();
// OR
// Use show/hide
uc1.Show();
uc2.Hide();
uc3.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
uc2.BringToFront();
// OR show hide...
}
private void button3_Click(object sender, EventArgs e)
{
uc3.BringToFront();
}

Categories

Resources