I am building a basic Image editor. In my app, if the user wants to resize the image a new form pops up and asks the user to input an new width and height for the image.
public partial class Form1 : Form
{
...
private void resizeToolStripMenuItem_Click(object sender, EventArgs e)
{
resize resizeForm = new resize();
resizeForm.ShowDialog();
}
...
}
I am wondering how I can get the values from the resizeForm and use them to alter the image on the parent form (the Form1 instance).
If this question needs clarification please let me know.
Thanks!
I assume there are a number of ways to do this. I'd probably use public properties on the resizeForm and then get those when the resizeForm.ShowDialog() returns.
if (resizeForm.ShowDialog() == DialogResult.OK) // or whatever
{
myVal = resizeForm.Val;
...
}
or something like that.
Setup properties in your "resize" class for the values you want to retrieve. For example, if you add a width property:
public int Width { get; set; }
you will be able to get the width from your Form1 class.
Add properties to your resize form that your main form can interrogate after the resize form is closed, like ...
DialogResult dr = resizeForm.ShowDialog();
if( dr != DialogResult.Cancel )
{
var newH = resizeForm.Height;
var newW = resizeForm.Width;
// do something with new vals.
}
Related
So, I have a project in which I allow editing of a DatagridView in a separate Form. I pass in the DatagridView object and its parent container to the constructor of the new Form.
This works well and I can edit the grid that way. But when I try to give it back by changing its parent back to the original form, I get this error :
Cannot convert type 'System.Windows.Forms.MenuItem' to 'System.Windows.Forms.Control'
Now both MenuItem, and Manual Entry directly inherit from Form.
Here is my code that takes the DataGridView from the original form (which works correctly)
public partial class ManualEntry : Form
{
private Data d;
DataGridView DataView;
MenuItem mi;
public ManualEntry(DataGridView ExcelDisplay, Data d, MenuItem menuItem)
{
InitializeComponent();
//Take the Datagridview from the MenuItem.
DataView = ExcelDisplay;
DataView.Parent = this;
mi = menuItem;
this.d = d;
this.DataView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.DataView.Location = new System.Drawing.Point(15, 76);
this.DataView.Size = new System.Drawing.Size(237, 211);
this.DataView.TabIndex = 5;
this.DataView.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.DataView_CellContentClick);
}
Now here is me trying to give it back. and of course it produces the error above.
private void FinishButton_Click(object sender, EventArgs e)
{
//move the datagridview back to the original form and give its old size,shape, and position back.
DataView.Parent = mi;
this.DataView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.DataView.Location = new System.Drawing.Point(12, 167);
this.DataView.Name = "ExcelDisplay";
this.DataView.Size = new System.Drawing.Size(250, 256);
this.DataView.TabIndex = 7;
this.Close();
}
I have also tried casting which does not work either.
DataView.Parent = (System.Windows.Forms.Control)mi;
Update
This shows that MenuItem is a Form as well.
public partial class MenuItem : Form
{
This shows that MenuItem is a Form as well.
Well, you have not convinced the compiler. You can tell from the error message that it thinks that your "mi" variable is a System.Windows.Forms.MenuItem. Do not use .NET class names for your own types, that just makes your life harder to troubleshoot bugs like this. Don't use variable names like "d" either. Choosing good names is a Very Important programmer's job.
The proper way is to preserve the control's Parent property so you can set it back. Roughly:
public partial class ManualEntry : Form
{
private Data DataViewData;
private DataGridView DataView;
private Point DataViewLocation;
private Control DataViewParent;
public ManualEntry(DataGridView ExcelDisplay, Data data)
{
InitializeComponent();
this.DataViewData = data;
this.DataView = ExcelDisplay;
this.DataViewLocation = ExcelDisplay.Location;
this.DataViewParent = ExcelDisplay.Parent;
this.DataView.Parent = this;
// etc...
}
protected override void OnFormClosing(FormClosingEventArgs e) {
base.OnFormClosing(e);
if (!e.Cancel) {
DataView.Parent = this.DataViewParent;
DataView.Location = this.DataViewLocation;
// etc..
}
}
}
This is simplest possible thing, but I cant update text on status bar. I just started working in c# but cannot find solution. I tried below code:
Mdiparent
public void StutasText(string text)
{
toolStripStatusLabel.Text = text;
}
Child form
MDIParent1 obj = new MDIParent1();
obj.StutasText("Hello world");
obj.Refresh();
Its not showing status text in the status bar.
Where did I go wrong?
In the MDI Parent form, I assume you have toolStripStatusLabel1. If you dont have, you can add this by clicking on the little black arrow in the menuStrip control.
Option 1
In your MDI Parent (let's assume, frmMain is the MDI Parent form) form where you have the StatusStrip, goto frmMain.Designer.cs file and find the place
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
make this,
public System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
Then from your child pages you can access like below.
ToolStripStatusLabel statusStrip=((frmMain)(frmMdiChild.MdiParent)).toolStripStatusLabel1
Option 2
Declare a public property which will return the toolStripStatusLabel1 control or method where you can set the text property of the toolStripStatusLabel1 in the MDI Parent form. If you return the menuStrip1 itself then you have access to all the properties of that control. If you declare a method, which will set the text property of the toolStripStatusLabel1 then you can only set the text. Decide on what you want, based upon your requirement.
An implementation which returns the menuStrip1 control.
public ToolStripStatusLabel GetStatusBar
{
get
{
return this.toolStripStatusLabel1;
}
}
then from your child pages, you can use,
ToolStripStatusLabel statusStrip=((frmMain)(frmMdiChild.MdiParent)).GetStatusBar;
Option 3
To make it little bit more prettier, you can declare a method in a common class. Then you can reuse this in other child forms.
public void ShowStatusbarMessage(Form frmMdiChild, string message, NotifierType notificationType)
{
ToolStripStatusLabel statusStrip=((frmMain)(frmMdiChild.MdiParent)).GetStatusBar;
statusStrip.Text = message;
if (notificationType == NotifierType.SuccessInfo)
{
statusStrip.ForeColor = System.Drawing.Color.Green;
}
else if (notificationType == NotifierType.Warning)
{
statusStrip.ForeColor = System.Drawing.Color.Orange;
}
else
{
statusStrip.ForeColor = System.Drawing.Color.Red;
}
}
Here, NotifierType is an enum
((mdiMain)MdiParent).toolStripStatusLabel.Text = "My Text";
//but you must change the modifier property of toolStripStatusLabel to public etc
You're creating a new instance of MDIParent1, not using the instance that is shown/the instance your child form belongs to.
You could try using
this.MdiParent
instead of
new MDIParent1()
((frmMDI)this.MdiParent).yourcontrol.yourproperty=yourvalue;
frmMDI is unique name of MDI form.
First
In "mdi parent name".Designer.cs, change the type or member private to public
Second In your code add the next code
(("mdi parent name")MdiParent).toolStripStatusLabel.Text = "your text";
I have a image showing in
picShowPicture.Image = Image.FromFile(textbox1.Text)
At the moment the image is showing on the main screen what I want is when a user selects a image from the database, it then open up in a new window?
how is this done?
Create a new Form in Designer and pick a PictureBox in it. And create a special method for example
public void SetPicture(Image image)
which will set image to PictureBox.
On selecting picture call:
YourForm form = new YourForm();
form.SetPicture(Image.FromFile(textbox1.Text));
form.ShowDialog();
Or you can dynamically create new form:
Form form = new Form();
PictureBox pictureBox = new PictureBox();
pictureBox.Dock = DockStyle.Fill;
pictureBox.Image = Image.FromFile(textbox1.Text);
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
form.Controls.Add(pictureBox);
form.ShowDialog();
I'm going to make the assumption that by "selects a image from the database" does not mean that you're (de)serializing BLOBs and just want the code to make the image display in a new window. I will further assume that you have a second form created in your project called "Form2" with an image viewer called "picImageViewerOnForm2".
var newImage = Image.FromFile(textbox1.Text);
var newForm = new Form2();
newForm.picImageViewerOnForm2.Image = newImage;
newForm.Show();
Add Form to application and put PictureBox on it, let's say that ID of PictureBox is "pictureBox1", then create public proterty on that form to access picutre box, like this :
public partial class ShowPictureForm : Form
{
public PictureBox ImagePictureBox { get { return this.pictureBox1; } }
public ShowPictureForm()
{
InitializeComponent();
}
}
then show that new form like this :
ShowPictureForm spf = new ShowPictureForm();
spf.ImagePictureBox.Image.FromFile(textbox1.Text)
spf.ShowDialog();
Add a new Windows Form to your project, named "ShowImageWindow".
Add a picturebox to the window, and the following code:
public Image ImageToShow { get; set; }
public ShowImageWindow()
{
InitializeComponent();
}
private void ShowImageWindow_Load(object sender, EventArgs e)
{
pictureBox1.Image = ImageToShow;
}
Then create and show the window as follows:
Image img = Image.FromFile(textBox1.Text);
ShowImageWindow frm = new ShowImageWindow();
frm.ImageToShow = img;
frm.ShowDialog();
frm.Dispose();
Create a new Form (new type derived from System.Windows.Forms.Form) that accept image path as constructor.
Say new form is ImageForm. Create PictureBox inside this form.
in the function ShowImageWindow (or similar in main form), call like following
ImageForm imageForm = new ImageForm(textbox1.Text)
imageForm.ShowDialog()
In the ctor of ImageForm, set the Image into picturebox control inside ImageForm
public ImageForm(String imagePath)
{
pictureBox1.Image = Image.FromFile(imagePath);
}
Create a new form with just picture box in it.
Then when forming that forms object pass image as parameter(of course you have to create a parameterised constructor of your form) and pass that image in a global image variable.
In Form_Load set that global image variable as your image controls image.
I have created a WindowsFormControlLibrary porject. It works fine, I can drop it on the forms,call its methods,etc ...
but now as a property of it, I am passing the name of a Label to it. and I want this custom control to be able to use that label name and for example change its font to bold .
so the question is that if I have a WinForm and I have a Label on that form and my custom control on that form, then how can I tell my custom control to do something with that label which I am passing its name to it?
Instead of sending in the name of the label, send in a reference to the actual label and then the custom control can both read the name if it needs to and change the label's font and other properties.
Be careful though, it can quickly get messy to keep track of what's happening if various forms and controls change controls on other forms etc.
Edit: Added code to do what you ask for in the comments
Code isn't tested so it might not be completely correct, but something similar to this should work.
foreach (Control c in Parent.Controls)
{
if (c is Label)
{
Label l = (Label)c;
// do stuff to label l
}
}
First, if you wish to access a Control from your UserControl, you will need to use the FindForm() method.
Second, you will be required to expose your TextBox control, for example, through a property of your form.
Then, you would need to know the type of this Form returned by this FindForm() method.
Once you know it, you need to type-cast this result to the correct type.
So, here a sample untested pseudo-code to give you the idea:
public partial class MyMainForm {
private TextBox textBox1;
public MyMainForm() {
textBox1 = new Textbox();
textBox1.Name = #"textBox1";
textBox1.Location = new Point(10, 10);
textBox1.Size = new Size(150, 23);
this.Controls.Add(textBox1);
}
public Font MyTextBoxFont {
get {
return textBox1.Font;
} set {
if (value == null) return;
textbox1.Font = value;
}
}
}
Then, assuming you have dropped your control on your form, your UserControl could have a property like so:
public partial class MyUserControl {
private Form GetContainerForm {
get {
return this.FindForm();
}
}
// And later on, where you need to set your TextBox's font:
private void SetContainerInputFieldFont(Font f) {
if (GetContainerForm == null) return; // Or throw, depending on what you need to do.
((MyMainForm)GetContainerForm).MyTextBoxFont = f
}
}
cool :) I just added a get set public property of type Label... it automatically lists all the label on the form.
Does anyone know a way to open a 2nd form in a .NET application with a certain tab selected in a tab control on that form?
This is what my current code looks like which just opens the form:
SettingsForm TheSettingsForm = new SettingsForm(this);
TheSettingsForm.Show();
SettingsForm.cs is the name of the 2nd form to be opened.
Thanks in advance,
You just need to expose a property on your form that allows the calling code to set the desired tab. You might do it just by index like this:
var form = new SettingsForm(this);
form.SelectedTab = 2;
form.Show();
The property on the form would just set the appropriate property on the tab control:
public int SelectedTab
{
get { return _tabControl.SelectedIndex; }
set { _tabControl.SelectedIndex = value; }
}
Do something like this:
SettingsForm TheSettingsForm = new SettingsForm(this);
TheSettingsForm.TabPanel.SelectedIndex = SettingsFormTabIndexes.MyDesiredTab;
TheSettingsForm.Show();
where you've made a property in TheSettingsForm that exposes the tab control and SettingsFormTabIndexes is a friendly enum naming all the tabs by index.
Add an event handler to the TabControl.SelectedIndexChanged event.
myTabControl.SelectedIndexChanged += myTabControl_SelectedIndexChanged;
private void myTabControl_SelectedIndexChanged(object sender, EventArgs e) {
TabControl tc = sender as TabControl;
if (tc.SelectedIndex == indexOfTabToShowFormOn) {
TheSettingsForm.Show();
}
}