I have a datacontrol.cs(UserControl), which is contain textbox1 and codebehind window having a method with parameter of currentvalue
public void bindvalue(float currentvalue)
{
textbox1.Clear();
textbox1.Text = currentvalue.ToString();
}
I have a Form, Here added the usercontrol in this form and which contains a Button
So when clicking button it pass a currentvalue by the way of method to the datacontrol class like that .
private void button_click(object sender, EventArgs e)
{
float currentvalue = 1500.00f;
datacontrol obj = new datacontrol();
obj.bindvalue(currentvalue);
}
Everything working fine to me. It pass the current value to the usercontrol class and there current value assigned/added to the textbox1.Text = currentvalue.ToString();. It doesn't shows any error . But finally the textbox doesn't shows any value.
I used breakpoint to check the functionality. It gave current value to the textbox. But strange!!!..
I can't predict whats wrong in my code.
Helps appreciated.:)
Your instance of datacontrol with required value (1500.00f) does not exist on your form. You are only delcaring it, passing value and forgetting about it.
If you have already added user control to form and want to call bindvalue method of existing control, you should do the following:
private void button_click(object sender, EventArgs e)
{
float currentvalue = 1500.00f;
this.dataControl1.bindvalue(currentvalue);
}
Note that dataControl1 is the name of your user control on the form, it can be different from dataControl1.
If you want to create new user control and call bindvalue, you should add new instance on the form:
private void button_click(object sender, EventArgs e)
{
float currentvalue = 1500.00f;
datacontrol obj = new datacontrol();
obj.bindvalue(currentvalue);
this.Controls.Add(obj);
}
If it is already dynamically added control on the form, declare a field of Form class, assign new instance of it control, when you want it, and call to it as it shows in the first example.
You need add instance of datacontrol to the form
datacontrol obj = new datacontrol();
obj.bindvalue(currentvalue);
Controls.Add(obj);
You can simply make a call to bindvalue method by using the tagename of your user control in button click event handler.
Suppose the tag name is 'datacontrol', then you should use the following lines of code to achieve your task:
protected void Button1_Click(object sender, EventArgs e)
{
float myvalue = 150.50f;
datacontrol1.BindValue(myvalue);
}
Regards, Aamir ( .Net developer)
Please marks this thread as answer if it solved what were looking for... thanks
Related
I need to pass a value from Form to UserControl.
I have tried to:
1.(and i make basketBox public)
Form:
UserControlBasket us1 = new UserControlBasket();
public void button1_Click(object sender, EventArgs e)
{
us1.basketBox.Text = g1.Name;
}
2.
Form:
UserControlBasket us1 = new UserControlBasket();
public void button1_Click(object sender, EventArgs e)
{
us1.Txt = g1.Name;
}
UserControl:
public string Txt
{
get { return basketBox.Text; }
set { basketBox.Text = value; }
}
3. And i have tried to do like here:
Pass value from Usercontrol to Form
I expect that in basketBox(it is a textBox) will be value from a g1.Name;
I just add a Controls.Add(us1) and it solved my problem.
I am just summarizing the comments (from #Jimi) with my answer here. Both the number 2 and 3 Solutions are acceptable and correct by design principles. It seems that a different object of user control was accessed here instead of using the one that placed the from.
In your case, you should better use textbox, which is attached to page and dont use a UserControl.
My question is about dealing with multiple forms in C# Windows form application. I am developing a code for playing a movie and moving it frame by frame with buttons. I already have the code for moving the movie frame by frame with ctlcontrols in Windows Media Player.
The thing that I'm having an issue with is that I want to have a main form and a movie form, and when I click the button in the main form, I want to send a number to the other form and if the number was 2, I want the movie to go frame by frame in the movie form. And I want to do it without opening a new form every time I click the button. I have made a function in my second form and I called it in the button in the main form. It is expected to work but it doesn't.
The code for the button in the main form is:
private void button1_Click(object sender, EventArgs e)
{
value = txtSendNum.Text; // get the value from the textox and
// assign it to string variable
MovieForm movieform = new MovieForm(); //create an object for MovieForm
movieform.ConnectForms(value);
}
The code for the function(ConnectForms function) in the second form is:
public void ConnectForms(string value)
{
val = Convert.ToInt32(value);
if (val == 2)
{
axWindowsMediaPlayer1.Ctlcontrols.play();
axWindowsMediaPlayer1.Ctlcontrols.currentPosition += 0.5;
axWindowsMediaPlayer1.Ctlcontrols.stop();
}
}
You are creating a new MovieForm every time the user clicks the button, this is wrong. You need a reference to the MovieForm that was previously open.
This is the difference between the meaning of Object and Class. You need a reference to the object not a new object from the same class.
A simple way to make it work is like the following code:
MovieForm movieform = null;
private void button1_Click(object sender, EventArgs e)
{
value = txtSendNum.Text;
if(movieform == null || movieform.IsDisposed)
{
movieform = new MovieForm(); //create an object for MovieForm
movieform.Show();
movieform.ConnectForms(value);
}
else
{
movieform.ConnectForms(value);
movieform.Focus();
}
}
You must have a reference the the other form. Instead of declaring movieform as a local variable, declare it as a class level variable (i.e., a field)
private MovieForm _movieform = new MovieForm();
private void button1_Click(object sender, EventArgs e)
{
value = txtSendNum.Text; //get the value from the textox and assign it to string variable
_movieform.ConnectForms(value);
_movieform.Show();
}
A local variable, i.e., a variable declared in a method has a lifetime limited to one method call (I'm not talking about special cases like iterators and closures).
A class field has the same lifetime as the object (here the Form).
Instead of creating a method on each form that receives a value or passing the value as parameter on the constructor of each form, or creating a new property to set the value to search for it later you should use the Tag property of the Control that is already created for that. Here you is how it is used
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.tag?view=netframework-4.7.2
private void buttonNewCustomer_Click(object sender, EventArgs e)
{
/* Create a new customer form and assign a new
* Customer object to the Tag property. */
CustomerForm customerForm = new CustomerForm();
customerForm.Tag = new Customer();
customerForm.Show();
}
I want to pass a parameter to the textbox. I have the following code and it is passing the parameter but not the way I want.
My main form in already open and I want to pass the parameter from my search form. when I do with the code below it opens mt 1 more main form and the parameter is shown in there. I want to by able to show in the opened main form.
When I erase frmMain.Show(); nothing happens.
Main frmMain = new Main();
artikal = "TEST TEST";
frmMain.ed_artiakal.Text = artikal;
frmMain.Show();
any suggestions?
You have many variants to solve your problem.
Option 1
Define and use custom event.
Search form code:
public event EventHandler ArtikalTextChanged;
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (ArtikalTextChanged != null)
ArtikalTextChanged(this, EventArgs.Empty);
}
Main form code:
private void button1_Click(object sender, EventArgs e)
{
Search search = new Search();
search.ArtikalTextChanged += OnArtikalTextChanged;
search.Show();
}
private void OnArtikalTextChanged(object sender, EventArgs e)
{
this.ed_artiakal.Text = (sender as Search).textBox1.Text;
}
Don't forget to make textBox1 of Search form public.
Option 2
Get instance of your main form in search form:
Search form code:
private void textBox1_TextChanged(object sender, EventArgs e)
{
var mainForm = Application.OpenForms.OfType<Main>().FirstOrDefault();
mainForm.ed_artiakal.Text = textBox1.Text;
}
Main form code:
private void button1_Click(object sender, EventArgs e)
{
Search search = new Search();
search.Show();
}
Don't forget to make ed_artiakal control public in your Main form.
Option 3
Share data between forms (recommend)
But if you application is large and you want to make it scaleable and flexible I recommend you to use data-binding technique to share data between forms without coupling them. You can read more at articles: http://msdn.microsoft.com/en-us/library/h974h4y2(v=vs.90).aspx
I have solved my problem in the following way.
On my Search Form I created a public string and when I showed the form I referenced to that string in my case GetItemCode.
The key here was to use ShowDialog() and not to use Show().
SEARCH FORM
Search frmSearch = new Search();
frmSearch.ShowDialog();
ed_artiakal.Text = frmSearch.GetItemCode;
MAIN FORM
public string GetItemCode
{
get { return Artikal; }
}
Now when I close the search form the value is shown in the TextBox on my main form.
Thanks for your answers and comments!
I have a program in which the user inputs information into two separate text boxes and then clicks a button, at which time the program does some work and returns data to a third text box.
Then the user enters data into another textbox and clicks a second button. The program takes the text box return data from the first button click method along with the new input data, does some work and returns data to a mutli-line text box.
The problem I am having is that once the program leaves the first button click method, the return data disappears, so no data from the first button click method gets passed to the second button click method.
I am pretty sure it does this because the object is instantiated inside of the first button click method instead of being instantiated at the class level and therefore once the program leaves the method the object is gone. I attempted to instantiate the object at the class level but I cannot because I receive a "field initializer cannot reference the non-static field, method, or property" error.
I have looked at the MSDN specifications for this problem and understand what causes it, but the answer they give to fix it is to instantiate the object inside of the method. So I am right back to where I started from.
I am at a loss on what to do here. Here is my code with the object instantiated inside of the button click method:
private void btnOrderButton_Click(object sender, EventArgs e)
{
numberOfPizzas = int.Parse(this.txtNumberOfPizzaOrdered.Text);
numberOfCokes = int.Parse(this.txtNumberOfCokesOrdered.Text);
PizzaOrder orderObject = new PizzaOrder(numberOfPizzas, numberOfCokes);
this.txtAmountDue.Text = orderObject.TotalAmountDue.ToString("C");
}
private void btnPaymentButton_Click(object sender, EventArgs e)
{
amountDue = double.Parse(this.txtAmountDue.Text);
amountPaid = double.Parse(this.txtAmountPaid.Text);
Payment orderPaymentObject = new Payment(amountDue, amountPaid);
}
The data disappears because you're creating a temporary object at the method's scope, meaning it doesn't exist outside the method.
To fix that declare the data member at the class level and use it like so:
Payment m_orderPaymentObject = null;
private void btnPaymentButton_Click(object sender, EventArgs e)
{
amountDue = double.Parse(this.txtAmountDue.Text);
amountPaid = double.Parse(this.txtAmountPaid.Text);
m_orderPaymentObject = new Payment(amountDue, amountPaid);
}
Repeat the process for any other data object you need.
This is one way to do it.
class ClassName
{
// available anywhere within the class; remember to instantiate before referencing
private PizzaOrder _order;
private Payment _payment;
private void btnOrderButton_Click(object sender, EventArgs e)
{
numberOfPizzas = int.Parse(this.txtNumberOfPizzaOrdered.Text);
numberOfCokes = int.Parse(this.txtNumberOfCokesOrdered.Text);
_order = new PizzaOrder(numberOfPizzas, numberOfCokes);
this.txtAmountDue.Text = _order.TotalAmountDue.ToString("C");
}
private void btnPaymentButton_Click(object sender, EventArgs e)
{
amountDue = double.Parse(this.txtAmountDue.Text);
amountPaid = double.Parse(this.txtAmountPaid.Text);
_payment = new Payment(amountDue, amountPaid);
}
}
I had a DataGrid and a ContextMenuStrip in it. When I click SelectMenuStrip in a row I want the ClickEvent of the context menu to get all data in that row in a databean class and return that databean class, so that I can fill the data in another class-
All was fine I defined event as below
private CustomerDataBean toolStripMenuItem1_Click(object sender, EventArgs e)
{
CustomerDataBean custdatabean = null;
int rowno = tblcustomerdataview.CurrentCellAddress.Y;
custdatabean.Customerpk = int.Parse(tblcustomerdataview.Rows[rowno].Cells[0].Value.ToString());
custdatabean.Contactno = tblcustomerdataview.Rows[rowno].Cells[6].Value.ToString();
custdatabean.Emailid = tblcustomerdataview.Rows[rowno].Cells[7].Value.ToString();
custdatabean.Other = tblcustomerdataview.Rows[rowno].Cells[8].Value.ToString();
return custdatabean;
}
but in designer.cs I am getting an error in line:
this.toolStripMenuItem1.Click += new System.EventHandler(this.toolStripMenuItem1_Click);
The error is:
Error 1:
'WindowsFormsApplication3.CustomerDataBean WindowsFormsApplication3.CustomerSearch.toolStripMenuItem1_Click(object, System.EventArgs)' has the wrong return type D:\WindowsFormsApplication3\WindowsFormsApplication3\Merchandising\customerSearch.Designer.cs 83 46 NFTRANS
Where have I done something wrong?
Let me explain the situation
I had a jobcodeform where user should input the customercode in combobox if he forget customercode he can use a buton to go to another form called customersearch where there is a datagrid table with a context menustrip which when clicked gets the whole details of the selected row in a customerdatabean and return that back to the first jobcodeform
Your code doesn't make a lot of sense. Click events don't return anything (except void), they really just run a procedure.
Your quick fix is to match the signature of the handler:
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
// do something, don't return anything
}
What you need to define is what are you trying to do with your CustomerDataBean object. If you are just trying to add it to a list, then add it to a list:
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
CustomerDataBean custdatabean = new CustomerDataBean();
// set properties
myBeans.Add(custdatabean);
}
The code you currently have doesn't even create a CustomerDataBean object. It's null, and then you are trying to update a null object. That isn't going to work.
Ask yourself where should your click event return the object to?
What code will process that bean?
The others have explained what is wrong with your click event.
Here's one possible method:
Have your click event call a separate method to process the bean. Something like this, perhaps:
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
CustomerDataBean custdatabean = null;
int rowno = tblcustomerdataview.CurrentCellAddress.Y;
custdatabean.Customerpk = int.Parse(tblcustomerdataview.Rows[rowno].Cells[0].Value.ToString());
custdatabean.Contactno = tblcustomerdataview.Rows[rowno].Cells[6].Value.ToString();
custdatabean.Emailid = tblcustomerdataview.Rows[rowno].Cells[7].Value.ToString();
custdatabean.Other = tblcustomerdataview.Rows[rowno].Cells[8].Value.ToString();
processBean(custdatabean);
}
private void processBean(CustomerDataBean bean)
{
//Code to process the bean here.
}
ToolStripMenuItem click event handlers need to return void.