Controlling a PictureBox from a class - c#

I have a little project in C#, (Windows Forms Application). I have on the form 77 PictureBoxes (pictureBox1, pictureBox2, pictureBox3, ...) and I want to control them but from a new class (Access.cs), by declaring a new one picturebox in the class to control all the pictures.
Because it would be too long if I pass through each pictureBox and add a click method and Copy + Paste the code and change the pictureBox number each time.
I've set the pictures as public and tried the following code:
Access.cs:
using System.Windows.Forms;
public class Access
{
PictureBox picBox = new PictureBox();
public void PictureClicked()
{
picBox.Image = Properties.Resources.apple;
}
}
Form1.cs:
private void pictureBox1_Click(object sender, EventArgs e)
{
Access ac = new Access();
ac.PictureClicked();
}
but the code didn't work!!

I dont really get what you want to do but you could try to send the object to your Access class:
private void pictureBox1_Click(object sender, EventArgs e)
{
Access ac = new Access();
ac.PictureClicked(sender);
}
public void PictureClicked(Object Sender)
{
picBox = (PictureBox)Sender;
picBox.Image = Properties.Resources.apple;
}

Access.Cs
public void pictureBox1_Click(object sender, EventArgs e)
{
PictureBox pi = (PictureBox)sender;
pi.Image = Properties.Resources.alert__2_;
}
Form1.Cs
private void pictureBox2_Click(object sender, EventArgs e)
{
Form1 c =new Form1();
c.pictureBox1_Click(sender, e);
}
Here pictureBox2_Click this event for all picturebox

Related

Show TextBox on every TabPage

I attached a TextBox to the first TabPage of a TabControl. I would like to display the same TextBox object on every TabPage. I tried to add the control to the tabControl Collection but unfortunately it's not working.
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox);
}
Button b;
public Form1()
{
InitializeComponent();
b = new Button() { Text = "Prueba" };
}
private void Form1_Load(object sender, EventArgs e)
{
AddButtonToTabControl();
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
AddButtonToTabControl();
}
public void AddButtonToTabControl()
{
tabControl1.SelectedTab.Controls.Add(b);
}
I missed two methods. It's working now!
tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting);
void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox);
}

how to put Background image property for trackbar

I am trying to get the background image fot the trackbar only. I used following snippet but the image is not visible.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}[enter image description here][1]
private void trackBar1_Scroll(object sender, EventArgs e)
{
//this.trackBar1.
this.trackBar1.BackgroundImage =
Image.FromFile(#"C:\Users\310276521\Documents\Aftab\Untitled.jpg");
}
}
I want to implement this kind of layout:
try this
private void trackBar1_Scroll(object sender, EventArgs e)
{
//this.trackBar1.
this.trackBar1.BackgroundImage = Image.FromFile(#"C:\\Users\\310276521\\Documents\\Aftab\\Untitled.jpg");
}
Please make sure you are having the image in that location

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.

Custom controls and event handling?

I have a windows custom control with a picturebox and a button on it's default panel. This windows custom control will be added to a windows form. There is another windows form called form2. when the user double clicks on the custom control, it should load form2. in designer when i double clicked the custom control on the form, it creates a load() event for that custom control. But i need double click event, how this can be done?
here is the graphical view of what happens
Here is code in the control
[DefaultEvent("DoubleClick")]
public partial class cntrlImageLoader : UserControl
{
public cntrlImageLoader()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = Environment.SpecialFolder.MyPictures.ToString();
if (ofd.ShowDialog() == DialogResult.OK)
{
pbImage.Image = Image.FromFile(ofd.FileName);
}
}
private void pbImage_Click(object sender, EventArgs e)
{
this.cntrlImageLoader_DoubleClick(pbImage, e);
}
private void cntrlImageLoader_Load(object sender, EventArgs e)
{
}
private void cntrlImageLoader_DoubleClick(object sender, EventArgs e)
{
}
}
Here is the calling code on form1
private void cntrImLdrFront_DoubleClick(object sender, EventArgs e)
{
//this.cntrImLdrFront.pbImage.DoubleClick += new EventHandler(pbImage_DoubleClick);
}
FrmImageViewer f; // this is form2
private void pbImage_DoubleClick(object sender, EventArgs e)
{
f= new FrmImageViewer();
f.MdiParent = this.MdiParent;
f.Show();
}
Assign the attribute [DefaultEvent("DoubleClick")] at the class declaration.
[DefaultEvent("DoubleClick")]
public partial class MyControl : UserControl
{
}
This will creates event which you have set by default on double click of control at design time where you have placed your user control.
EDITED:
[DefaultEvent("LoadPicture")]
public partial class cntrlImageLoader : UserControl
{
public delegate void LoadPictureEventHandler(object sender, LoadPictureEventArgs e);
public event LoadPictureEventHandler LoadPicture;
private void pbImage_DoubleClick(object sender, EventArgs e)
{
if (LoadPicture != null)
{
LoadPictureEventArgs ev = new LoadPictureEventArgs();
LoadPicture(this, ev);
if (ev.Picture != null)
{
pbImage.Image = ev.Picture;
}
}
}
}
Create another class and give that class name to LoadPictureEventArgs
public class LoadPictureEventArgs : EventArgs
{
public Image Picture {get; set;}
public LoadPictureEventArgs(Image _picture)
{
Picture = _picture
}
public LoadPictureEventArgs()
: base()
{
}
}
HOW TO USE IT?
//FORM1
private void cntrImLdrFron_LoadPicture(object sender, LoadPictureEventArgs e)
{
Image img = null;
//LOAD YOUR IMAGE HERE
e.Picture = img;
}

Multiple Window Problems - C# WPF

I am have trouble using multiple windows in WPF and switching between them using a button. In theory my application should have 2 buttons, one forward and one back each on respectively changing the window to the previous and next window.
Unfortunately I get a Stackoverflow error, through my research I feel that it has something to do with me creating new windows that are creating the window again when the previous window is created, thus making a horrible loop. However I don't know where I can put the window creation code to stop this problem or if there are other ways to fix this.
Here is code for my windows:
First Window
public partial class Presenation_1 : Window
{
Presentation_2 p2 = new Presentation_2();
MainWindow M = new MainWindow();
public Presenation_1()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
p2.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
M.Show();
}
}
Second Window
public partial class Presentation_2 : Window
{
Presentation_3 p3 = new Presentation_3();
Presenation_1 p1 = new Presenation_1();
public Presentation_2()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
p3.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
p1.Show();
}
}
Third Window
public partial class Presentation_3 : Window
{
Presentation_4 p4 = new Presentation_4();
Presentation_2 p2 = new Presentation_2();
public Presentation_3()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
p4.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
p2.Show();
}
}
Fourth Window
public partial class Presentation_4 : Window
{
Presentation_3 p3 = new Presentation_3();
MainWindow M = new MainWindow();
public Presentation_4()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
M.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
p3.Show();
}
}
Thanks in advance
Don't create your Windows before the button is clicked, you can create them in the event handler:
private void btnForward_Click(object sender, RoutedEventArgs e)
{
var p2 = new Presentation_2();
this.Close();
p2.Show();
}
When you create a windows, you create 2 other windows with
new Presentation_X()
This new windows is automaticaly show and itself open 2 other windows.
You can create this windows once in the Mainwindow (auto hide this one), pass the reference in constructor and not close these windows. Quick example (not tested) :
public partial class Presenation_X : Window
{
private Window preview;
private Window next;
public Presenation_X(Window w1, Window w2)
{
this.preview = w1;
this.next = w2;
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.next.Show();
this.Hide();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.preview.Show();
this.Hide();
}
}

Categories

Resources