10 Replies - 29444 Views - Last Post: 15 July 2010 - 02:47 PM Rate Topic: -----

#1 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

How to access controls of another form

Posted 15 July 2010 - 09:50 AM

Hey guys, in my Auto-Typer program I am giving the user the ability to edit a message that they have already entered into the listbox. I have made a new EditForm and in the MainForm I made it show that form when the edit selection button is pressed. When the form opens up the message that was selected before clicking the button should show up in the textbox and then the save button should close the form and edit the selected message to what they changed, but I do not know how to access the MainForm's controls (the listbox) from the EditForm. I think when I call edit form I can pass the selected item into the constructor but I do not know how to make it save. Does anyone have any suggestions on how to access other forums controls?

I don't think you guy's need any code, but if you do just post.

Thanks for any help. It's really appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: How to access controls of another form

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: How to access controls of another form

Posted 15 July 2010 - 10:08 AM

There are two ways you can do this:
  • Declare the other form's controls as public and access them as otherFormName.ControlName.PropertyName
  • Expose the control's properties safely by using get/set functions in the parent form.

The second method is preferable.

Click here for more information.

This post has been edited by born2c0de: 15 July 2010 - 10:10 AM

Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4436
  • View blog
  • Posts: 7,709
  • Joined: 08-June 10

Re: How to access controls of another form

Posted 15 July 2010 - 10:14 AM

What I would suggest doing is exposing a public property on your Edit Form. The name can be whatever makes sense to you, but for now I'll call it EditResult.

//in EditForm:
public string EditResult
{
  get { return editTextBox.Text; } //whatever value you want to return
}


And call that in your Main form:
//in MainForm:
EditForm editForm = new EditForm();
DialogResult result = editForm.ShowDialog();
if(result == DialogResult.OK) //or whichever result you choose
  SetMessage(editForm.EditResult);//whatever logic you use to update your messages

This post has been edited by insertAlias: 15 July 2010 - 10:14 AM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,661
  • Joined: 02-June 10

Re: How to access controls of another form

Posted 15 July 2010 - 10:26 AM

Generally speaking I consider directly accessing Form2 from Form1 to be a bad idea. It tightly binds the two forms together. If you make a change on Form2 such as changing a property name or control name then you break every reference to it in From1.

The more robust way to do this is to raise an event on Form1 and have Form2 react to it. Isolating your classes (forms are classes), and reacting to events are key concepts on C#. Maybe this can help.


Bulding an application - Part 1
Was This Post Helpful? 1
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4436
  • View blog
  • Posts: 7,709
  • Joined: 08-June 10

Re: How to access controls of another form

Posted 15 July 2010 - 10:34 AM

If Form2's only purpose is to be a modal child of Form1, tight binding isn't a bad thing. You control when it's up, what the DialogResult is, and how the property is set.

For example, an OpenFileDialog is used much the same way as my example. You don't create an event on your main form and have the OpenFileDialog react to it. You just create an instance, open it, block until it's done, check it's result, and if all's OK, use the property/method you need.

Basically, what I'm saying is that for Dialogs, tight binding can be a good thing. If the form is not a Dialog but another Form, then yes, the more correct way is to use events on one or both of the forms based on which direction the information needs to flow.

Edit:

Quote

If you make a change on Form2 such as changing a property name or control name then you break every reference to it in From1.


That's exactly what the Refactor menu was made for.

This post has been edited by insertAlias: 15 July 2010 - 10:35 AM

Was This Post Helpful? 3
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,661
  • Joined: 02-June 10

Re: How to access controls of another form

Posted 15 July 2010 - 10:39 AM

All very true. So long as the OP's send form really is just a small dialog - as you say, like an OpenFileDialog - that is the right way to handle it.
Was This Post Helpful? 0
  • +
  • -

#7 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: How to access controls of another form

Posted 15 July 2010 - 11:19 AM

OK, I did what you guys said and it still doesn't work. Here is what I have.

This is in MainForm.cs
public string messageText
        {
            get { return messageListbox.SelectedItem.ToString(); }
            set { messageListbox.SelectedItem = value; }
        }


This is in EditForm.cs
private void saveButton_Click(object sender, EventArgs e)
        {
            mainForm.messageText = editTextbox.Text;
            this.Close();
        }


Can someone please tell me why this isn't working?
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,661
  • Joined: 02-June 10

Re: How to access controls of another form

Posted 15 July 2010 - 11:34 AM

When you say "doesn't work" can you be more specific?

I'm going to guess that setting the text doesn't automatically select in your listbox.
You're going to have write a little more than just
set { messageListbox.SelectedItem = value; }


You're going to have to loop through the items of the listbox and compare the text of the item to the text that came in. If they match, then select that index.

for (int Loop = 0; Loop < messageListBox.Items.Count-1; Loop++)
{
   if (messageListBox.Items[Loop].ToString().ToLower() == value.ToLower())
   {
     messageListBox.SelectedIndex = Loop;
     break;  // Break out of the loop since we found our match
   }
}

Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4436
  • View blog
  • Posts: 7,709
  • Joined: 08-June 10

Re: How to access controls of another form

Posted 15 July 2010 - 11:59 AM

View PostC++ Programmer, on 15 July 2010 - 05:19 PM, said:

OK, I did what you guys said and it still doesn't work. Here is what I have.

This is in MainForm.cs
public string messageText
        {
            get { return messageListbox.SelectedItem.ToString(); }
            set { messageListbox.SelectedItem = value; }
        }


This is in EditForm.cs
private void saveButton_Click(object sender, EventArgs e)
        {
            mainForm.messageText = editTextbox.Text;
            this.Close();
        }


Can someone please tell me why this isn't working?


You're actually doing it backwards from the way I suggested. If I had a ListBox strings, and I wanted to use a modal dialog to edit one of them, here's what I would do:

  • Create a dialog form, called EditForm (or whatever), that takes a string parameter in it's constructor.
  • Expose a public property on this form that is the result of the edit. In my example, I called it EditResult. (note that best practices suggest that you use upper case for properties)
  • Make sure to set the DialogResult property based on the buttons you have on the form (this.DialogResult = DialogResult.OK for the OK button, DialogResult.Cancel for the Cancel button, etc...)
  • In your MainForm, create an instance of the EditForm in some event, and pass it the SelectedItem (as a string) in the constructor.
  • Display it using ShowDialog, making sure to store the DialogResult.
  • If the DialogResult is good, grab the value from EditResult (or whatever you named it) and apply the change to your listbox.


Note that in this example, the Dialog is only for editing a string and providing a result. It doesn't access any data on the MainForm, it just exposes some of its own to be retrieved.
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4436
  • View blog
  • Posts: 7,709
  • Joined: 08-June 10

Re: How to access controls of another form

Posted 15 July 2010 - 12:15 PM

Here's an actual example of what I am talking about:

Form1.cs
using System;
using System.Windows.Forms;

namespace DynamicTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            listBox1.Items.Add("string 1");
            listBox1.Items.Add("string 2");
            listBox1.Items.Add("string 3");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string text = listBox1.SelectedItem as string;
            EditForm ef = new EditForm(text);
            if (ef.ShowDialog() == DialogResult.OK)
                listBox1.Items[listBox1.SelectedIndex] = ef.EditResult;
        }
    }
}


EditForm.cs
using System;
using System.Windows.Forms;

namespace DynamicTest
{
    public partial class EditForm : Form
    {
        public string EditResult
        {
            get { return textBox1.Text; }
        }

        public EditForm(string text)
        {
            InitializeComponent();
            textBox1.Text = text;
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}

Was This Post Helpful? 1
  • +
  • -

#11 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: How to access controls of another form

Posted 15 July 2010 - 02:47 PM

Thank you. This is exactly what I needed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1