I'm trying to pass value from one form to another in winforms.
On my main form I have btnAddNewRecord and dataOptions combobox.
User should first select from combobox(dataOptions) and than click on btnAddNewRecord.
I want to pass this user selected value from dataoptions combobox to new form, so I tried like this
MainForm
private void btnAddNewRecord_Click(object sender, EventArgs e)
{
var formAddRecord = new FormNewRecord();
formAddRecord.ShowDialog();
}
private void Form1_Load()
{ populating combobox...}
private void dataOptions_SelectedIndexChanged(object sender, EventArgs e)
{
IMyCustomData data = (IMyCustomData)dataOptions.SelectedItem;
var formAddRecord = new FormNewRecord();
formAddRecord.SelectedDataOptions = data.ToString();
}
FormNewRecord.cs
public string SelectedDataOptions {get; set;}
private void FormNewRecord_Load(,,,,,)
{
txtSelectedDataOptions.Text = SelectedDataOptions;
}
no error on build, but on debugging txtSelectedDataOptions is not populated with passed value. What I'm doing wrong here?
Thanks
You are creating two different instances of FormNewRecord. Make formAddRecord as private field and show it on button click.
FormNewRecord formAddRecord = new FormNewRecord();
private void btnAddNewRecord_Click(object sender, EventArgs e)
{
formAddRecord.ShowDialog();
}
private void dataOptions_SelectedIndexChanged(object sender, EventArgs e)
{
IMyCustomData data = (IMyCustomData)dataOptions.SelectedItem;
formAddRecord.SelectedDataOptions = data.ToString();
}
Well, formAddRecord should be a private field of your class, not a var redeclared in each method !
(Method btnAddNewRecord_Click has no ideas of variables declared in Method dataOptions_SelectedIndexChanged, by the way you create different instances).
So
private FormNewRecord formNewRecord_ = new FormNewRecord();
private void btnAddNewRecord_Click(object sender, EventArgs e)
{
formNewRecord_ .ShowDialog();
}
private void Form1_Load()
{ populating combobox...}
private void dataOptions_SelectedIndexChanged(object sender, EventArgs e)
{
IMyCustomData data = (IMyCustomData)dataOptions.SelectedItem;
formNewRecord_.SelectedDataOptions = data.ToString();
}
I don't think new a form instance in another form is a good way, a better way you can set the data you want to pass as public in the parent form, and when you show the child form, set the parent form as child's owner, then you can get and use the data in the child form.
set the data as a public property in the parent form, like this: Main Form:
public string passData = "";
private void btnAddNewRecord_Click(object sender, EventArgs e)
{
var formAddRecord = new FormNewRecord();
formAddRecord.ShowDialog(this); //important
}
private void Form1_Load()
{ populating combobox...}
private void dataOptions_SelectedIndexChanged(object sender, EventArgs e)
{
IMyCustomData data = (IMyCustomData)dataOptions.SelectedItem;
passData = data.ToString(); //store the selected value to passData
}
2.get passed data from child's owner:
FormNewRecord.cs
private void FormNewRecord_Load(,,,,,)
{
if(this.Owner != null)
{
MainForm mf = (MainForm)this.Owner;
txtSelectedDataOptions.Text = mf.passData;
}
}
Related
I am trying to fill the class with data and use this data anywhere else on the program, where I will need it.
I created this class:
public class id
{
private string name; // field
public string Name // property
{
get { return name; } // get method
set { name = value; } // set method
}
}
And in form_name I tried to fill the class this way:
private void Button_Click(object sender, RoutedEventArgs e)
{
id IDOBJE = new id();
{
IDOBJE.Name = txtshenimi.Text;
}
this.Close();
}
But I don't get any results. Could someone help me to clarify this?
What I have tried:
And in another form I tried to retrieve data like this:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
id idobje = new id();
txtrez.Text = idobje.Name;
}
You can use some global area to store and access a common variable.
For example, create a class as a central repository.
public static class Globals {
public object myObj;
}
Then assign your created object to this one on first form.
private void Button_Click(object sender, RoutedEventArgs e)
{
id IDOBJE = new id();
{
IDOBJE.Name = txtshenimi.Text;
}
Globals.myObj = IDOBJE;
this.Close();
}
Access that on your second form this way.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
id idobje = (id)Globals.myObj;
txtrez.Text = idobje.Name;
}
I declared my array like this string[] dateArray = new string[10]; then placing a value to it like this:
public void btnAdd_Click(object sender, EventArgs e)
{
dateArray[i] = date.Text.ToString();
}
So far, after a few inputs and want to check if it the dateArray has a value I did this.
private void button1_Click(object sender, EventArgs e)
{
string toDisplay = string.Join(Environment.NewLine, dateArray);
MessageBox.Show(toDisplay);
}
And it is really have a value and displaying it.
So I've created a button that may pass the value of dateArray to another form for it to display and print. I'm using iTextSharp. Here is my button:
public static string finalDate;
private void printBtn_Click(object sender, EventArgs e)
{
string finalDate = string.Join(Environment.NewLine, dateArray);
printForm table_form = new printForm();
table_form.Show();
}
And passing the string finalDate(Second form) to here
table2.AddCell(new Phrase(tableITS.finalDate, data));
But no luck, I tried putting a message box at the end of doc.Close(); but it really doesn't have a value
MessageBox.Show(tableITS.finalDate);
What am I getting wrong?
EDIT:
I'm passing it here
private void printForm_Load(object sender, EventArgs e)
{
string finalDate2 = tableITS.finalDate;
.
.
.
//Document printing stuff
}
Assuming that You want to pass data from Form1 to Form2:
Create a public property in Form2like:
public string[] dateArray { get; set;}
and then in your `Form1:
Form2 frm = new Form2();
frm.dateArray = dateArray;
frm.ShowDialog();
Now you can use the dateArray in Form2
Here is my code.
private void PlaceOrder_Click(object sender, EventArgs e)
{
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
I want to add what is in the menu box to a another list box on another form.
Update
(added by jp2code)
Form1 (Main):
namespace WindowsFormsApplication1 {
public partial class RESTAURANT : Form
{
double soup = 2.49;
double ordertotal;
public RESTAURANT()
{
InitializeComponent();
}
private void RESTAURANT_Load(object sender, EventArgs e)
{
}
private void Add_Click(object sender, EventArgs e)
{
MenuBox.Items.Add("Soup");
TotalBox.Items.Add(String.Format("{0:C}", soup));
ordertotal += soup;
total.Text = Convert.ToString(String.Format("{0:C}", ordertotal));
}
private void TotalBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void PlaceOrder_Click(object sender, EventArgs e)
{
new AreYouSure().Show();
this.Show();
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
}
}
Form2 (Confirmation)
namespace WindowsFormsApplication1 {
public partial class Confirmation : Form
{
public Confirmation()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Confirmation_Load(object sender, EventArgs e)
{
}
private void MenuBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
When clicking the 'Send Order' button the items from 'MenuBox' in form 1 need to be sent to the 'MenuBox' in form 2
OtherForm.OtherListbox.Items.Clear();
foreach(var itm in MenuBox.Items)
OtherForm.OtherListbox.Items.Add(itm);
Form1 (Main):
namespace WindowsFormsApplication1 {
public partial class RESTAURANT : Form
{
double soup = 2.49;
double ordertotal;
public RESTAURANT()
{
InitializeComponent();
}
private void RESTAURANT_Load(object sender, EventArgs e)
{
}
private void Add_Click(object sender, EventArgs e)
{
MenuBox.Items.Add("Soup");
TotalBox.Items.Add(String.Format("{0:C}", soup));
ordertotal += soup;
total.Text = Convert.ToString(String.Format("{0:C}", ordertotal));
}
private void TotalBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void PlaceOrder_Click(object sender, EventArgs e)
{
new AreYouSure().Show();
this.Show();
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
}
}
Form2 (Confirmation)
namespace WindowsFormsApplication1 {
public partial class Confirmation : Form
{
public Confirmation()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Confirmation_Load(object sender, EventArgs e)
{
}
private void MenuBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
When clicking the 'Send Order' button the items from 'MenuBox' in form 1 need to be sent to the 'MenuBox' in form 2
It is better that the controls on your other form (ListBox, in this case) are set to Private by default.
In that case, you would either need to set the control's visibility to Public (bad form, in my opinion) or create a method in your other form to accept the parameters from your form.
Consider something like this:
public void ListBoxData(object[] array)
{
listBox1.Clear();
listBox1.AddRange(array);
}
To get the data or selected item information back to your main form, you would likewise create another public object that you could check, like the property below:
public object SelectedItem { get { return listBox1.SelectedItem; } }
I hope that is what you were looking for.
UPDATE:
Using the code you supplied in the post below, I can see you do not have anything in your Confirmation form to send data to, much less a way to pass that information.
If you had a ComboBox, you could do something like follows:
public partial class Confirmation : Form
{
private ComboBox comboBox1;
public void AddRange(object[] array)
{
comboBox1.Items.AddRange(array);
}
}
That does not place the ComboBox anywhere on your form. You would need to work that out.
With that done, I'm guessing you need to edit your PlaceOrder_Click routine:
private void PlaceOrder_Click(object sender, EventArgs e)
{
//new AreYouSure().Show();
//this.Show();
using (var obj = new Confirmation())
{
var list = new List<object>(MenuBox.Items.Count);
foreach (var o in MenuBox.Items)
{
list.Add(o);
}
obj.AddRange(list.ToArray());
if (obj.ShowDialog(this) == DialogResult.OK)
{
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Items.Clear();
ordertotal = 0;
}
}
}
If you are struggling with this, you might need to look into some C# Windows "multiple forms" tutorials.
Here is a YouTube (that I did not sit all the way through): https://www.youtube.com/watch?v=qVVtCPDu9ZU
I am trying to create a notepad like application in C# by using a Textbox.
I want to implement find function in it. I want an ability to search the text entered in textbox of Find form in textbox of Form1 and then highlight it.
Please help i am unable to do it
Form1.cs
private void findToolStripMenuItem_Click(object sender, EventArgs e)
{
Find f = new Find();
f.Show();
}
public void find()
{
int idx = 0;
while((idx=textBox1.Text.IndexOf(text))!=1)
{
textBox1.Select();//Select the text which are found
}
}
Find.cs
public partial class Find : Form
{
Form1 f = new Form1();
public Find()
{
InitializeComponent();
}
private void Cancelbutton2_Click(object sender, EventArgs e)
{
this.Close();
}
private void Findbutton1_Click(object sender, EventArgs e)
{
f.text =textBox1.Text;
f.find();
}
You need to specify the start and length parameter in the "Select" method. For Example:
textBox1.Select(idx, text.Length);
You can only highlight one section of data at a time with the standard TextBox. Try FastColourTextbox if you want better support.
private void textBox1_Enter(object sender, System.EventArgs e){
if (!String.IsNullOrEmpty(textBox1.Text))
{
textBox1.SelectionStart = 0;
textBox1.SelectionLength = textBox1.Text.Length;
}
}
I have an app that show a form call System Parameters and i want the form to only pop one time so that the user cant open the same window million times. I tried
private void SystemParametersClick(object sender, EventArgs e)
{
Xpan sp = new Xpan();
sp.CurrentItem = this.GetCaller(sender);
if (sp.Visible==false)
{
sp.Show();
}
}
It doesnt work because it is not the same instance. :(
How do i make it only pop once?
Why do you instantiate the form within the method? Simply instantiate it within the parent class and only call the Show() method within the click event.
public class MainForm : Form
{
private Xpan _Xpan;
public MainForm()
{
InitializeComponent();
_Xpan = new Xpan();
}
private void SystemParametersClick(object sender, EventArgs e)
{
_Xpan.Show();
}
}
Maybe this simple approach would suffice?
private bool has_been_shown = false;
private void SystemParametersClick(object sender, EventArgs e)
{
if(!has_been_shown)
{
has_been_shown = true;
Xpan sp = new Xpan();
}
}
First disable closing for Xpan form. You can do it by defining OnFormClosing event handler.
private void Xpan_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
}
Then define your Xpan form as a class member of the parent form, e.g.:
private readonly Xpan _sp = new Xpan();
And finally defile your Click handler this way:
private void SystemParametersClick(object sender, EventArgs e)
{
if (!_sp.Visible)
{
_sp.Show();
}
else
{
_sp.Activate();
}
}
That's it.