pass picture from usercontrol to Form - c#

I have a main form with 1 panel,
the panel has 10 user controls and
every user control has on picture box.
Main form:
private void Form1_Load(object sender, EventArgs e)
{
Picturebox1.Image=....
for(int i=0;i<10;i++)
{
uscontrol a=new uscontrol()
{
usimage=Image.Fromfile....
};
panel1.Controls.Add(a);
}
}
User control:
public Image usimage
{
get { return imagebox.Image; }
set { imagebox.Image = value; }
}
How can I do that when I click on one of the user controls, it passes image of that user control to main form and shows it in Picturebox1,
thank you.

Firstly, you will need to add MouseClickEvent to every uscontrol. To achieve that, edit your Form1_Load to this.
Picturebox1.Image=....
for(int i=0;i<10;i++)
{
uscontrol a=new uscontrol()
{
usimage=Image.Fromfile....
};
a.MouseClick += new MouseEventHandler(USControl_Clicked); //Note this added line
panel1.Controls.Add(a);
}
And then you will need to add USControl_Clicked to your From1 code.
private void USControl_Clicked(object sender, MouseEventArgs e)
{
//Here we cast sender to your uscontrol class and access usimage
Picturebox1.Image = (sender as uscontrol).usimage;
}

Related

Access controls in created TabPage from separate function C# WinForms

I have the following code in my WinForms C# app:
private void button2_Click(object sender, EventArgs e)
{
var txtbox2 = new System.Windows.Forms.RichTextBox();
TabPage createdtabpage = new TabPage("I am a tab");
tabControl1.TabPages.Add(createdtabpage);
createdtabpage.Controls.Add(txtbox2);
}
And I want to access the text of txtbox2 when a separate button is clicked. I have this code:
private void button1_Click(object sender, EventArgs e)
{
//Either this:
string text = txtbox2.Text;
//or maybe this:
string text = createdtabpage.Controls[txtbox2]
}
However, this code doesn't work because the variables are not accessible to outside functions.Does anybody have a good way to access these TabPage controls from an outside function?
Thanks for any help
From the control tab, the tab is selected and then the desired control is found by name.
use this code
private void button1_Click(object sender, EventArgs e)
{
var textBox = (RichTextBox)tabControl1.SelectedTab.Controls["txtbox2"];
MessageBox.Show(textbox.Text);
}
Without a name, you have to navigate through all the controls, and for example, if you have multiple richTextBox controls, know the order in which they are located.
foreach (Control control in tabControl1.SelectedTab.Controls)
{
if(control is RichTextBox)
{
MessageBox.Show(control.Text);
}
}

Show Form side-by-side owner Form

I have a form. If someone presses a button, I want to show a second form "attached" to the original form, meaning that its left side is at the right side of the original form and they have the same height. In other words: they touch each other.
An answer seems to be Open Form next to Parent Form
However, there is a gap between the images. I want them to be exactly next to each other
main form:
private void ShowOtherForm()
{
using (var form = new OtherForm())
{
var dlgResult = form.ShowDialog(this);
ProcessDlgResult(dlgResult);
}
}
Other form, event handler Load
private void FormLoad(object sender, EventArgs e)
{
// show this form attached to the right side of my owner:
this.Location = new Point(this.Owner.Right, this.Owner.Top);
this.Height = this.Owner.Height;
}
Try to use ClientSize and Location
private void Form2_Load(object sender, EventArgs e)
{
var owner = this.Owner;
Location = new Point(owner.Location.X + owner.ClientSize.Width, owner.Location.Y);
Height = owner.Height;
}

On listbox double click open new form

Im using windowsForms c# have a listbox which is bound with data items ...So depending on double click of the listbox item i want the corressponding form open...thnx in adv
ListBox has an DoubleClick event. You can access it if you select object, open Events tab in a Properties window.
Double click it and Visual Studio will create an event handler for you like this:
public void ListBox1_OnDoubleClick(object sender, EventArgs e)
{
// here is your code
}
Now, you just need to enter your code.
If you want to open a form with a corresponding item then it will be something like:
public void ListBox1_OnDoubleClick(object sender, EventArgs e)
{
string text = listBox1.Text; // Don't forget to manipulate with it
Form1 form = new Form1();
form.Show();
}
Subscribe to DoubleClick event on ListBox
listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
private void listBox1_DoubleClick(object sender, EventArgs e)
{
// logic here
}
or
listBox1.DoubleClick += (s,e) => { /*logic here */};
I would use the MouseDoubleClick Event, it provides the cursor position in MouseEventArgs so you can easily detect which item got doubleclicked.
void Listbox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
int index = Listbox1.IndexFromPoint(e.Location);
if (index != ListBox.NoMatches)
{
object clickedItem = Listbox1.Items[index];
// open your form here
}
}

Populate textbox in a window using another window

I have an application with two windows. One window contains textboxes while the other window is the keyboard. My problem now is that when I click on my Keyboard window I encounter error. I cannot type on the textbox I clicked on the other window. Here is my code.
When I click one textbox from window I, I trigger this Mouse event.
private void TextBoxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
textBox = sender as TextBox;
textBox.Focus();
}
I used sender as TextBox because my textboxes are programmatically added to my window. I used it to get the name of the textbox.
And here is my code when I click a button from my Keyboard window.
Let used button 1:
private void button_numeric_1_Click(object sender, RoutedEventArgs e)
{
Screen1 screen1 = new Screen1();
screen1.textBox.Text += "1";
}
Screen1 is the window containing my textboxes.
How I possibly able to type text in my textbox using my created keyboard. I'm using C#. Anyone please help me out.
instead of using new Screen1(); you may need to use the actual instance of the screen, you may pass the same to the keyboard via constructor.
example
class Screen
{
Keyboard keyboard;
public Screen()
{
//pass the screen instance to the keyboard
keyboard = new Keyboard(this);
}
//...
private void TextBoxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
textBox = sender as TextBox;
textBox.Focus();
//open keyboard etc
keyboard.Show();
}
}
class Keyboard
{
private Screen screenInstance;
public Keyboard(Screen instance)
{
//store the instance in a member variable
screenInstance = instance;
}
//...
private void button_numeric_1_Click(object sender, RoutedEventArgs e)
{
//use the stored screen instance
screenInstance.textBox.Text += "1";
}
public void Show()
{
//display logic etc
}
}
above is just an example based on some assumptions, you may adjust/merge with your code as needed.
you may adjust the same to pass TextBox instance if you have multiple TextBoxes to be used
example
class Screen
{
Keyboard keyboard = new Keyboard();
private void TextBoxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
textBox = sender as TextBox;
textBox.Focus();
//open keyboard etc
keyboard.Show(textBox);
}
}
class Keyboard
{
private TextBox textBoxInstance;
private void button_numeric_1_Click(object sender, RoutedEventArgs e)
{
//use the stored TextBox instance
textBoxInstance.Text += "1";
}
public void Show(TextBox instance)
{
textBoxInstance = instance;
//display logic etc
}
}

C# Global Component Code

i'm new to C# i've been messing around to discover this language so far i've wrote many programs in my quest but now i'm stuck with one thing, i can't explain by words but codes can say what i want so here we go i know it's silly program but it's for education purpose only :D
Private void change()
{
anycontrol.BackColor = Color.Gold; // when this function called the control's BackColor will Change to gold
}
// example
private void TextBox1_Focused(object sender, EventArgs e)
{
Change(); // this suppose to change the color of the controls which is now textbox1 i want it to work on other controls such as buttons progressbars etc
}
now after i explained my problem i may ask you if you can to help it will be appreciated.
You can create a method that takes a Control and a Color as a parameter, and anything that inherits from Control (i.e. TextBox, DropDownList, Label etc.) will work with this:
void SetControlBackgroundColour(Control control, Color colour)
{
if (control != null)
{
control.BackColor = colour;
}
}
In your example, you could use it like this:
private void TextBox1_Focused(object sender, EventArgs e)
{
SetControlBackgroundColour(sender as Control, Color.Gold);
}
In response to the comments, you could then use this method in a recursive method that will set the background colour for each control on the form:
void SetControlBackgroundColourRecursive(Control parentControl, Color colour)
{
if (parentControl != null)
{
foreach (Control childControl in parentControl.Controls)
{
SetControlBackgroundColour(childControl, colour);
SetControlBackgroundColourRecursive(childControl);
}
}
}
And then call this function on your Form object (this) in your Form1_Load method (assuming the form is called Form1):
protected void Form1_Load(object sender, EventArgs e)
{
SetControlBackgroundColourRecursive(this, Color.Gold);
}

Categories

Resources