I have 2 projects in 1 solution first one to log into database second one have my main project my second project 'Class Library' Output.
I am trying send User Name from first project log in form to main project main form
I am using code
public string MyValue;
{
get { return txtUserName.Text; }
}
in log in window
and use
var frm1 = new DS4ERP .Core .Lanch .frmLogin ();
radLabel2.Text = frm1.MyValue;
i get nothing in second form
what is wrong ?
You are creating new Form instance. When you opening your second form use this:
Form2 f2 = new Form2(); // change Form2 with your second Form name
f2.Show(this);
Then change this code:
var frm1 = new DS4ERP.Core.Lanch.frmLogin ();
radLabel2.Text = frm1.MyValue;
To:
radLabel2.Text = ((frmLogin)Owner).MyValue;
Related
So I'm making a video editor and I want the program to open a new window when creating a file. So I created a new form and I wrote something like this:
Form fm = create_file(); fm.Show();
And I got an error, which said that create_file cannot be used as a method. I tried deleting the brackets, and it said that it's incorrect in the current context. How am I supposed to do this?
First you should write:
Form fm = new create_file();
then:
fm.Show();
Do not forget to write new when you create an Object from the Form fm!
public Form FormIsOpen(string name)
{
FormCollection forms = Application.OpenForms;
foreach (Form form in forms)
if (form.Name.Equals(name))
return form;
return null;
}
public void OpenForm(Form instance, string name)
{
Form form = FormIsOpen(name);
if (form == null)
instance.Show();
else
{
instance.Dispose();
form.Show();
form.WindowState = FormWindowState.Maximized;
form.Activate();
}
}
You can call by using below code
OpenForm(new MyForm(), nameof(MyForm));
I have a button on a completion form, which when pressed opens up a windows explorer.
This was ok, when i only had 1 directory.
I now have 2 directories, which are now set from my main form.
What i would like to do, is when the button is pressed, if a combobox on my main form is for example "Apples" then opens up a coded directory.
If the combobox on my main for is "Pears" then open up a different directory.
I have the following code to open up the original directory - just dont know how to include the second, as i cant seem to access the combobox from the completion form:
private void button1_Click(object sender, EventArgs e)
{
{
this.Hide();
FrmMain form3 = new FrmMain();
form3.Show();
}
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
{
FileName = "C:\\Directory 1\\",
UseShellExecute = true,
Verb = "open"
});
}
Hope you can help.
Thanks - Craig.
There are quite a few ways you could do this. I'm supposing your completion form is opened from the main form. The cleanest way would be to inject the current selection of the main form's combobox upon creation. Something in the line of:
var completionForm = new CompletionForm(directoryCombo.Text);
completionForm.ShowDialog(this);
Do note that ShowDialog is relevant. This ensures that no one will change the selected directory while the completion form is active. If this not the case, and the completion form isn't modal, then a better alternative is to implement a public property in your main form:
public string SelectedDirectory => directoryCombo.Text;
And then simply access it form your completion form:
var selectedDirectory = (Owner as mainForm).SelectedDirectory;
Let's say your mainform class is called MainForm and that the combobox instance inside it is called directoryComboBox.
Now, the first step consists in creating a property in your MainForm that exposes the currently selected value of the directoryComboBox instance:
public String CurrentDirectory
{
get { return directoryComboBox.Text; }
}
In your CompletionForm class, add the following field (used as a reference to the parent MainForm instance):
private MainForm m_Parent;
and modify the constructor as follows:
public CompletionForm(MainForm parent)
{
m_Parent = parent;
}
Finally, modify the code of your MainForm in which you create a new completion form and show it to the user as follows:
CompletionForm cf = new CompletionForm(this); // this is the current instance of `MainForm` showing the completion form
cf.ShowDialog(this);
In your CompletionForm you can now have access to the currently selected value of your directoryComboBox:
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
{
FileName = Path.Combine(#"C:\", m_Parent.CurrentDirectory),
UseShellExecute = true,
Verb = "open"
});
You could also construct your CompletionForm instance by directly passing it the current directoryComboBox value. The approach is very similar and it doesn't require you to modify your MainForm class adding a new property. All you have to do is to edit the right CompletionForm portions of code:
private String m_CurrentDirectory;
public CompletionForm(String currentDirectory)
{
m_CurrentDirectory = currentDirectory;
}
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
{
FileName = Path.Combine(#"C:\", m_CurrentDirectory),
UseShellExecute = true,
Verb = "open"
});
and the MainForm method that instantiates it:
CompletionForm cf = new CompletionForm(directoryComboBox.Text);
cf.ShowDialog(this);
So i am trying to store my main form and open a new one however i get this error, here is the code:
I have this at form level
public static frmAddBook frmkeepBooks = null;
public frmMain()
{
InitializeComponent();
frmkeepBooks = this;
}
The error underlines "this" saying it "Cannot inplicitly convert type Books.frmMain to Books.frmAddBook"
Change the first line into:
public static frmMain frmkeepBooks = null;
The types should be equal (or in herited) and probably it is not.
Are you trying to just show the new form on top of the old as a dialog keeping the old form up? I don't quite understand why you are trying to set your instance of frmMain to equal a null instance of frmAddBook.
if you are trying to open new form as a dialog you would do something like this:
public static frmAddBook frmkeepBooks;
public frmMain()
{
InitializeComponent();
frmKeepBooks = new frmAddBook();
/* if you want to display the 2nd form ontop of the first disallowing
user interaction on the first until the 2nd form closes */
frmKeepBooks.ShowDialog();
// If you want to allow interaction on either form
frmKeepBooks.Show();
/* maybe you don't want to display the first form
anymore after the 2nd form is displayed */
this.Visible = false;
}
I think this question needs some clarification on what you are trying to do exactly.
I am developing an application where i have to get data from 2 different forms to fill different fields of same form
My Work
I have called the constructor of form1 from party form and from itemform with parameters
code form Itemform
string units = dr.Cells[2].Value.ToString();
string rate = dr.Cells[3].Value.ToString();
Form1 f1 = new Form1( units, rate);
this.Hide();
constructor on form1 called while debugging
public Form1(string units, string rate)
{
InitializeComponent();
ItemId_LBL.Text = units;
ItemName_TXT.Text = rate;
}
same for another form but i no want to close my main form1 and only close other forms while clicking button constructor called but fields not populated why
NOTE:
I HAVE TO FILL TEXTBOXES OF ALREADY OPENED FORM FORM1
from another form get object of opened form
Form1 f = (Application.OpenForms[0] as Form1);
f.PName_TXT.Text = name.ToString();
f.PId_LBL.Text = id.ToString();
}
If Form1 is already open, it means the constructor had already run. Make a setter method and call the method from other form.
public void UpdateUnitAndRate(string units, string rate)
{
ItemId_LBL.Text = units;
ItemName_TXT.Text = rate;
}
//put it in form2 (or itemform) as a field
Form1 f1;
//add this in the method or event (in itemform) you want to update units and rates in form1
if (f1 != null)
f1.UpdateUnitAndRate(units, rate);
else
f1 = new Form1(units, rate);
I think what you are looking for is Application.OpenForms, use this to create an instance of the open form and access the textbox.
Hello I have datagridview in form1 and through form1 I open form2 and through form2 I open form3 and string named vyber_ID_K placed in Form1 needs to be accessed in Form3 (I need to get its value in Form3)
this is placed on button click in form1
form2 a = new form2 ("Novy");
string vyber_IDK = (string)dataGridView1.CurrentRow.Cells["ID_K"].Value.ToString();
a.vyber_ID_K = vyber_IDK;
a.Show();
a.Closed += klient_Closed;
I would like to access vyber_ID_K in form 3, how it can be done? I tried to set public string vyber_ID_K in form2 and pass it similary to form3 but I get null. Am I doing it right? Is there any other better solution please?
Thanks in advance.
My step-by-step according to Servy:
button click in Form 1
Func vyberIDKGetter = () => dataGridView1.CurrentRow.Cells["ID_K"].Value.ToString();
try
{
form2 = new form2 ("Novy");
a.vyberIDKGetter = () => dataGridView1.CurrentRow.Cells["ID_K"].Value.ToString();
a.Show();
}
button click in form2
public Func vyberIDKGetter;
private void button1_Click(object sender, EventArgs e)
{
nova_platba b = new nova_platba("novy");
b.vyberIDKGetter();
b.Show();
b.Closed += klient_Closed;
}
In form3
Func<string> vyberIDKGetter = veberIDK;
string vyberIDK = vyberIDKGetter();
SqlCommand sc = new SqlCommand(#"
INSERT INTO kliplat (datum,text,castka,akce,subkey,priznak,rocnik)
VALUES (#datum,#text,#castka,#akce,#subkey,#priznak,#rocnik);
SELECT scope_identity();
", spojeni);
sc.Parameters.AddWithValue("#subkey", vyberIDK);
So the issue here is that the value that you want doesn't exist yet when you're constructing Form2, or even Form3 for that matter. It needs to have some means of accessing the data at some point in the future. We can get this behavior by leveraging delegates.
Rather than passing a string to Form2, when that form is constructed (since we don't know what the string will be yet) pass a Func<string>. That object will be a method that, when invoked, will provide a string that represents the needed value. Form1 can define it like this:
Func<string> vyberIDKGetter =
() => dataGridView1.CurrentRow.Cells["ID_K"].Value.ToString();
Then in Form3 when it's holding onto the function that was passed it can get the string out by simply invoking that delegate:
Func<string> vyberIDKGetter = [...];
string vyberIDK = vyberIDKGetter();
This approach to solving the problem is particularly adventageous in that Form3 doesn't need to know anything about Form1 or Form2. If there is some other caller that wants to use it they can provide their own delegate instead. If there is a developer handling the coding of each form they don't need to communicate all of the internal details of each form to each other, they can just handle the passing of this delegate and then be able to treat the caller/callee as a black box.
You have to make a public getter/setter around the string:
public string Vyber_ID_K
get
{
return vyber_ID_K;
}
set
{
vyber_ID_K = value
}
That you need a reference from Form 1 in Form 2, and from Form 2 in Form 3. So you can access
each Form.
You can't use a string as Referenced Parameter, becuase it is an immutable class. String C#
That is a really odd that you pass a parameter via the constructor
form2 a = new form2 ("Novy");
and in the same time you pass another parameter via the property
a.vyber_ID_K = vyber_IDK;
Why don't you instead pass all parameters via the constructor?
string vyber_IDK = (string)dataGridView1.CurrentRow.Cells["ID_K"].Value.ToString();
form2 a = new form2 ("Novy", vyber_IDK);
and in Form2
public class form2
{
private string Name { get; set; }
private int vyber_IDK { get; set; }
public form2( string Name, int vyber )
{
this.Name = Name;
this.vyber_IDK = vyber_IDK;
}
Then, passing anything to form3 from form2 works in the same way
form3 f = new form3( this.vyber_IDK );