I am using Form2 to update the default printer and send the string to Form3. I typically have no problem operating from Form1 and passing data to Form2 or Form3. But having trouble using Form2 to update Form3!
The real names are: Form1 = Form1, Form2 = formUserSettings, Form3 = formViewDwg
Here is the code in Form1, calling Form2 (formUserSettings):
private void configureStartupSettingsToolStripMenuItem_Click(object sender, EventArgs e)
{
formUserSettings frmUsr = new formUserSettings(prnNameString, prnDriverString, prnPortString,
Settings.Default.DefaultPrinter.ToString(), Settings.Default.ViewStyle, Settings.Default.ReCenterEVafterDwgClose,
Settings.Default.SyncListDwgNum, listMain);
frmUsr.ValueUpdated += new ValueUpdatedEventHandler(frmUsr_ValueUpdated); //---added 3-22-12
//frmUsr.ValueUpdated2 += new ValueUpdatedEventHandler(newPrn_ValueUpdated); //---added 4-12-12
frmUsr.ShowDialog();
frmUsr.Close();
}
Here's the code inside Form2 (formUserSettings) that tries to send the printer name to Form3 (formViewDwg).
if (Application.OpenForms.OfType<formViewDwg>().Count() > 0)
{
newEntry = comboPrinters.Items[index].ToString();
formViewDwg frmVd = this.Owner as formViewDwg;
delPassData del = new delPassData(frmVD.passedNewVal);
del(newEntry);
}
else
{
frmVD = new formViewDwg(EViewMethods.currentPartPath, EViewMethods.currentPartNum, EViewMethods.currentDwgNum,
Settings.Default.DefaultPrinter, Settings.Default.DefaultPrinterDriver, Settings.Default.DefaultPrinterPort,
EViewMethods.defaultPrn[0], EViewMethods.defaultPrn[1], EViewMethods.defaultPrn[2], lBox, false, false);
newEntry = comboPrinters.Items[index].ToString();
delPassData del = new delPassData(frmVD.passedNewVal);
del(newEntry);
}
Inside Form3 (formViewDwg) is:
public void passedNewVal(string newPrn) // using the delegate "delPassData" declared in formUserSettings
{
try
{
comboPrinter.Text = newPrn;
}
catch
{
}
}
The error is "Delegate to an instance method cannot have null 'this'".
Try this:
In Form1
Form2 vForm2=new Form2();
vForm2.vForm1=this; //initialize the vForm1 variable of Form2 to this form
vForm2.Show();
and in Form2 define a global public variable of type Form1.
public Form1 vForm1;
you may now be able to play around with any of the property of Form1.
Well I never found out how to send the string from Form2 to Form3, but I found a good solution: When Form2 closes and sends its string to Form1 from "frmUsr_ValueUpdated", it checks to see if Form3 is open. If it is then a public method within Form3 is used to update its comboBox.text as follows. (Form1 = Form1, Form2 = formUserSettings, Form3 = formViewDwg {instance = frmVD})
private void frmUsr_ValueUpdated(object sender, ValueUpdatedEventArgs e) //---added 3-22-12
{
// Update the printer name on Form1 with the new value from formUserSettings
string prnStr = e.NewValue;
string[] parts = prnStr.Split('^'); //the printer name, driver and port were passed by e.NewValue, being separated by a "^"
//---added 5-7-12
EViewMethods.defaultPrn[0] = parts[0]; //printer name
EViewMethods.defaultPrn[1] = parts[1]; //printer driver
EViewMethods.defaultPrn[2] = parts[2]; //printer port
toolStripStatusLabel3.Text = parts[0];
//---added 5-7-12
if (frmVD != null && !frmVD.IsDisposed) //want to send the new printer name now if formViewDwg is already open. If it is not open, then when it is called to open, the formViewDwg constructor will pass the new printer to it.
{
frmVD.ProcessPrinterName(parts[0]); //ProcessPrinterName is a public method inside formViewDwg. Can call here because formViewDwg is already open!
}
}
Inside formViewDwg (Form3) is the public ProcessPrinterName method:
public void ProcessPrinterName(string message)
{
comboPrinter.Text = message;
}
If Form3 (formViewDwg) is not open then the updated printer name will be passed to it whenever an instance is invoked through its constructor parameter list. Printer name will be passed as "string prnName" in the constructor:
public formViewDwg(string currentPath, string currentPartNum, string currentDwgNum,
string prnNameList, string prnDriverList, string prnPortList,
string prnName, string prnDriver, string prnPort, ListBox lstBox, bool usingEngCode, bool engCodeIsEnabled) //---added 3-12-12
{
InitializeComponent();
Related
I'm having a strange problem ... I have two forms (Form1 & Form2). Form1 calls with an old name (string) and the user enters a new name (textbox1) in Form2 which is returned to Form1. Everything works fine if they enter a value or cancel ... however I want to put an error check to insure they enter a value, among other things. The error check works fine, but after the error, when a correct value is entered, form2 closes but nothing happens.
I put in some breakpoints and Form1 seems to hold on the using(form2 ...) statement, waiting for Form2 to finish, but after firing the error message, nothing happens.
If I remove the ... Form2 F2 = new Form2 ... Form2 just closes and returns to Fomr1. Ideally I'd like to stay on Form2 until a value gets entered or the user cancels.
What am I missing?
// Form1
using(Form2 F5 = new Form2(SelNm))
{
if(F5.ShowDialog()== DialogResult.OK)
{
//Do stuff
}
}
// Form2
public string newName { get; set; }
public string oldName { get; set; }
public Form2(string oldNm)
{
InitializeComponent();
oldName = oldNm;
}
private void btnOK_Click(object sender, EventArgs e)
{
if (textbox1.Text.Length > 0)
{
newName = textbox1.Text;
DialogResult = DialogResult.OK;
Close();
}
else
{
MessageBox.Show("ERROR: Must enter a new name.");
DialogResult = DialogResult.Cancel;
Form2 f2 = new Form2(oldName);
f2.Show();
Close();
}
}
The reason for this is that you called a new Form2 after the error dialog is shown. This is not the instance of the Form2 which Form1 is waiting for. Instead of calling a new Form2 why not re-use the current Form2?
Instead of this:
MessageBox.Show("ERROR: Must enter a new name.");
DialogResult = DialogResult.Cancel;
Form2 f2 = new Form2(oldName);
f2.Show();
Close();
Why not this?
MessageBox.Show("ERROR: Must enter a new name.");
// Do not close the form so the user can
// input again
Update:
As suggested on the comments..
private void textbox1_TextChanged(object sender, RoutedEventArgs e)
{
btnOK.Enabled = !string.IsNullOrWhiteSpace(textbox1.Text);
}
So I need to be able to pass a property(name) into another windows form.
In the first form, the user is prompted to key in their name, while in the second one, their name is shown.
My problem is that although the value keyed in in the first form is saved (I have a Message box to show me) when the new form runs, the value of the property is reset to the placeholder name from the constructor class. Here are the codes(Form1 being the second form)
Both of them have initialised the reference to the contructor class at the start.
else if (select > 0 || txtName.Text != "")
{
p.Name = txtName.Text; // Save Name as property
MessageBox.Show("" + p.Name);
this.Hide();
Form1 form = new Form1();
form.ShowDialog();
}
For Form1:
private void Form1_Load(object sender, EventArgs e)
{
setName();
MessageBox.Show("" + p.Name);
timer1.Start();
label3.Text = "Player: " + p.Name;
}
Create a property in Form1 to accept the name:
public class Form1 : Form
{
//other stuff
public string Name {get;set;}
}
Then set that property when creating the form:
else if (select > 0 || txtName.Text != "")
{
this.Hide();
Form1 form = new Form1();
form.Name = txtName.Text;
form.ShowDialog();
}
I'm trying to call a method from a different form on buttonclick.
If I debug it it does go to the method that I'm trying to call, but as soon as I try to change a textbox inside this function it doesn't work.
Here is my function
public void addedtram(string tramno, string rail, string lineno, string sect)
{
String tbx = "tbx_sect" + 1 +"L"+ 2;
TextBox tb = (TextBox)this.FindControl(tbx) as TextBox;
if (tb != null)
{
tb.Text = tramno;
tbx_sect10L1.Text = tramno;
}
}
I just put this in as a test, because I'm sure that the tbx exists but and with the debug it does say tb.Text = "1234"(example) but it doesn't show on my form.
Does anyone have a clue what the problem could be here?
If I understand you correctly, you should expose the contents of the textbox using a property:
class Form1 {
public string txtbox {
get { return textBox1.Text; }
}
}
Then in Form2 do this:
var frm = new Form1();
textBox1.Text = frm1.txtbox;
You can make frm a class var of Form2 and call .Show() in the constructor of Form2.
I open an additional form through a Toolstrip to enter a Username that will be needed in the Mainform (and is declared as String in the Mainform)
Code of Mainform:
private void toolStripButton6_Click(object sender, EventArgs e)
{
using (Form frm = new Form3())
{
frm.FormBorderStyle = FormBorderStyle.FixedDialog;
frm.StartPosition = FormStartPosition.CenterParent;
if (frm.ShowDialog() == DialogResult.OK)
{
Username = frm.ReturnValue1;
}
}
}
Code of Form3:
public string ReturnValue1 {
get
{
return textBox1.Text;
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
C# tells me that there is no frm.ReturnValue1 :(
You have declared your form as type Form not Form3:
using (Form frm = new Form3())
and as the class Form doesn't have a property ReturnValue1 you are getting the error. This compiles because Form3 is a subclass of Form so you can assign it to a variable of type Form without any casting being required. If you had it the other way round the compiler would have told you you needed a cast.
Your code should be:
using (Form3 frm = new Form3())
or perhaps even (my preference):
using (var frm = new Form3())
Then it will always be of the right type and you don't have to remember to change the class name in two places should you decide to use a different form in future.
In (Form1) i have a setting button, when i click on it a new form ( Form2 ) is shown, using these lines of code :
private void b7_Click(object sender, EventArgs e)
{
Form3 frm = new Form3();
frm.Show();
}
In form3, i have 6 text boxes, and two button, Save and Cancel.
What i'm trying to do is to provide this form to the user so he types the neccessary data into the form, then he click the Save Settings button. In Form1, i want to access to these text boxes to get their current values ( when user clicked save settings ). I tried to add a Form4 and named it ( MiddleForm), i added 6 text boxes to it, and in Form3 (The form in the image above) i wrote these line :
private void button2_Click(object sender, EventArgs e)
{
MiddleForm mf = new MiddleForm();
mf.textBox1.Text = keywrd1.Text;
mf.textBox2.Text = keywrd2.Text;
mf.textBox3.Text = keywrd3.Text;
mf.textBox4.Text = keywrd4.Text;
mf.textBox5.Text = keywrd5.Text;
mf.textBox1.Text = thelink.Text;
Close();
}
then i tried to access the values passed to the MiddleForm from Form1 (The form where i need to use the textboxes values), in Form1, i wrote these lines (for debug purposes only)
MiddleForm mf = new MiddleForm();
MessageBox.Show(mf.textBox1.Text); // to see whether there is something
Unfortunately, it seems that nothing is passed to mf.TextBox1
How can i call the current values (Saved using save settings button) of Form3 From Form1 in order to use them in the rest of code.
Any help please on getting this to work ?
Make 6 public properties in your Form3 like that:
public partial class Form3 : Form
{
public string Value1
{
get { return this.keywrd1.Text; }
}
public string Value2
{
get { return this.keywrd2.Text; }
}
...
}
After your Form3 is closed (but before disposed) you can access text values via properties. As pointed in another answer, use ShowDialog instead of Show and close Form3 inside it's own code.
private void b7_Click(object sender, EventArgs e)
{
Form3 frm = new Form3();
frm.ShowDialog();
string value1 = frm.Value1;
...
}
Make public properties in Form3 like this
public string[] Keys
{
get
{
return new string[] { tbKey1.Text, tbKey2.Text, tbKey3.Text,
tbKey4.Text, tbKey5.Text};
}
}
public string Link { get { return tbLink.Text; } }
From Form1 you can access these properties like this
Form3 frm = new Form3();
if (frm.ShowDialog() == DialogResult.OK) {
string[] keys = frm.Keys;
string link = frm.Link;
}
Note: It is important that you use ShowDialog and not Show, since Show does not wait for the other form to close. Also, when "Save settings" is clicked in Form3, set the dialog result
DialogResult = DialogResult.OK;
Close();
so that you can check it in Form1 as shown above.
You need to do this:
var form = Form.ActiveForm as Form3;
String myText = form.txtBoxName.Text;
You should make a public field that provides the values you want to get out of the form. If you go to the source of Form1, you should add in something like this:
public string TextValue1 {
get {return TextBox1.Text;}
}
Now, you can use the Form1.TextBox1 to retrieve a string value from your Textbox.
You could try using ShowDialog it will create your Form as a Model Dialog box, you can then check the DialogResult to know wether the data was saved or the Form was canceled.
i.e.
private void button2_Click(object sender, EventArgs e)
{
Form3 frm = new Form3();
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
savedSettings = frm.getTextBoxValues();
}
}
Form3
public partial class Form3 : Form
{
string[] textValues = new string[6];
public Form3()
{
InitializeComponent();
}
public string[] getTextBoxValues()
{
return textValues;
}
private void saveSettings_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.OK;
textValues[0] = textBox1.Text;
textValues[1] = textBox2.Text;
textValues[2] = textBox3.Text;
textValues[3] = textBox4.Text;
textValues[4] = textBox5.Text;
textValues[5] = textBox6.Text;
this.Close();
}
private void cancelSettings_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.Close();
}
}