Windows Develop Bookmark and Share   
 index > Windows Forms General > calling windows form from a class
 

calling windows form from a class

dear all

its seems simple question but something went wrong when i try this

i have a class library project within this class i have 2 classes

1) winform class that inherits form (frmMSMQ) - that contains textbox1, textbox2 andApply button- during this form constructor i set 2 default values to the textboxes

2) class MSMQ

i wonder how can i show the frmMSMQ form from MSMQ class - and when i press on apply button - i want to read the textboxes values (the user can change the default values in the textboxes)

in shortage i have form and class - how can i read values from textboxes in the form within class running after clicking Apply button on the form

Thanks alot

ronlahav  Sunday, April 15, 2007 2:11 PM
to show a form, do this;

frmMSMQ myForm = new frmMSMQ(which ever constructor you want to call);
myForm.Show(); // for modaless form
OR
myForm.ShowDialog(); // modal form

i guess you want a modaless form.

now, for getting values of textboxes in MSMQ class whenever Apply button is clicked, do something like this ...

add a public event (and delegate) in frmMSMQ class. the delegate should take the two string values (for 2 textboxes values) as arguments. call the event in Apply button's click event and pass it the textbox text values.

In MSMQ class, add event handler for the event you defined in frmMSMQ class !

so whenever the Apply button is clicked, this event handler is called and you have textbox values as argument!


it should'nt b the only solution to this problem, but it should be good enought to solve your problem!..


sample code Smile

frmMSMQ class
----------------
public delegate void myDelegate(string textbox1value , string textbox2value);
public event
myDelegate myEventOfTypeMyDelegate;

// Apply button click event
private void btnApply_Click(object sender, EventArgs e)
{
// do something

if (myEventOfTypeMyDelegate != null)
myEventOfTypeMyDelegate (textBox1.Text , textBox2.Text);

// do something
}


MSMQ class
-----------

frmMSMQ
myForm;

public MSMQ() // constructor
{
myForm = new myForm ( /* overloaded constructor parameters here */);
myForm.
myEventOfTypeMyDelegate += new frmMSMQ.myDelegate(this.EventHandlerForApplyButton);
}

// same signature as myDelegate
private void
EventHandlerForApplyButton (string val1 , string val2)
{
MessageBox.Show ("textbox 1 values is " + val1);
MessageBox.Show ("textbox 2 values is " + val2);
}

cutealien  Monday, April 16, 2007 1:07 AM
Figo Fei  Tuesday, April 17, 2007 4:56 AM
to show a form, do this;

frmMSMQ myForm = new frmMSMQ(which ever constructor you want to call);
myForm.Show(); // for modaless form
OR
myForm.ShowDialog(); // modal form

i guess you want a modaless form.

now, for getting values of textboxes in MSMQ class whenever Apply button is clicked, do something like this ...

add a public event (and delegate) in frmMSMQ class. the delegate should take the two string values (for 2 textboxes values) as arguments. call the event in Apply button's click event and pass it the textbox text values.

In MSMQ class, add event handler for the event you defined in frmMSMQ class !

so whenever the Apply button is clicked, this event handler is called and you have textbox values as argument!


it should'nt b the only solution to this problem, but it should be good enought to solve your problem!..


sample code Smile

frmMSMQ class
----------------
public delegate void myDelegate(string textbox1value , string textbox2value);
public event
myDelegate myEventOfTypeMyDelegate;

// Apply button click event
private void btnApply_Click(object sender, EventArgs e)
{
// do something

if (myEventOfTypeMyDelegate != null)
myEventOfTypeMyDelegate (textBox1.Text , textBox2.Text);

// do something
}


MSMQ class
-----------

frmMSMQ
myForm;

public MSMQ() // constructor
{
myForm = new myForm ( /* overloaded constructor parameters here */);
myForm.
myEventOfTypeMyDelegate += new frmMSMQ.myDelegate(this.EventHandlerForApplyButton);
}

// same signature as myDelegate
private void
EventHandlerForApplyButton (string val1 , string val2)
{
MessageBox.Show ("textbox 1 values is " + val1);
MessageBox.Show ("textbox 2 values is " + val2);
}

cutealien  Monday, April 16, 2007 1:07 AM
Figo Fei  Tuesday, April 17, 2007 4:56 AM

You can use google to search for other answers

Custom Search

More Threads

• non rectengular form flickers at moving
• textbox with listbox
• Unable to set InterpolationMode in Graphics
• Multi-select of textboxes in C# windows form
• Borders.
• changing focus from one control to another
• How to catch mouse wheel in the WebBrowser?
• Tooltip create crashes the application
• LayoutMdi method doesn’t work with Sizeable ToolWindow
• DataGridView SelectionBackColor and Transparency Problem