I have a FlowLayoutPanel containing an unspecified number of Labels, when I double click in one of them, a new Form containing a TextBox and a Button will appear, here is the code:
foreach (Label lb in FlowLayoutPanel1.Controls)
{
lb.MouseDoubleClick+=new MouseEventHandler(lb_MouseDoubleClick);
}
private void lb_MouseDoubleClick(object sender, MouseEventArgs e)
{
NewForm form = new NewForm();
form.ShowDialog();
((Label)sender).Text = ...;//I want get text from TextBox of the NewForm here
}
I want get Text from TextBox of the NewForm and assign Text to the object that invoke the Form when user click the Button of the Form, I don't know how to use delegate to do this, please help! Thanks for reading this!
To be honest, I think your best bet might be to store the TextBox value in a static variable and simply take the value from there. It'll avoid a lot of complicated work.
That said, I'm not sure of how you've implemented the NewForm class. If you've set that up to have the textbox, or the textbox's value, publicly accessible, you could set it a lot more simply.
Related
it's just a simple stuff that i wanna test
i have two forms (Form1 and Form2)
In Form1 have:
textbox and button
In Form2 have only TextBox that use for displayData when I click button in Form1
the problems is when i clicked buttton the data send to Form2 but it's created a new form everytime I clicked it. Is there any way to updateData in Form2 without create a new one.
Here the code:
In Form1:
private void sendBt1_Click(object sender, EventArgs e) //sendBt1 is button in Form1
{
Form2 f2 = new Form2(typeBox1.Text);//typeBox1 is TextBox in Form1 use to type
f2.Show();
}
In Form2:
public Form2(string data)
{
InitializeComponent();`enter code here`
this.ds2.Text = data; //ds2 is textBox in Form2 that i wanna display
}
Can anyone help? Im kinda stuck rn :)
As commented already, there are numerous ways to get this done. The current code, as you describe, is creating multiple Form2 objects since the code is creating a new Form2 each time the button is clicked on Form1.
Form2 f2 = new Form2(typeBox1.Text);
In addition, you want the “TEXT” in typeBox1 TextBox to be passed to Form2's TextBox ds2. The current code does this however, if the text in typeBox1 TextBox in Form1 changes, the TextBox in Form2 (ds2) is NOT updated. Again, there are numerous ways to fix this, however, I suggest to create a DataBinding for Form2's TextBox such that IF the typeBox1 TextBox changes in Form1, then the TextBox ds2 in Form2 will automatically be updated at the same time and the user does not have to do anything.
The example below uses this DataBinding approach and works as described….
I may be missing something here, however, in my tests, one approach is to simply “check” to see if Form2 is already open. If Form2 IS OPEN… then, we do not have to do anything since our code sets up the data binding for the text box in Form2’s constructor. Since Form2 IS OPEN it may be minimized, therefore, if the user clicks the button on Form1 and Form2 IS OPEN then we will set the forms state back to normal which will basically restore the form. Also the code will call the forms Show() method in case the Form has somehow been hidden.
IF Form2 IS NOT OPEN, then obviously we need to create it as your current code does, however, instead of passing in JUST the TEXT from the TextBox, I suggest you send over the WHOLE TextBox from From1 to Form2. This way, we can set up the DataBinding for Form2’s TextBox and Bind Form2’s TextBox to the passed in TextBox from Form1. This will automatically update Form2’s TextBox ds2 when Form1’s TextBox changes.
Therefore I suggest you change the code in Form1’s sendBt1_Click event to something like…
private void sendBt1_Click_Click(object sender, EventArgs e) {
var form = Application.OpenForms["Form2"];
if (form == null) {
form = new Form2(typeBox1);
}
form.Show(); // <- in case the form is somehow hidden
form.WindowState = FormWindowState.Normal; // <- in case the form is minimized
}
NOTE: in the above code we are passing the WHOLE TextBox to From2, not just the text in the TextBox.
Then Form2’s constructor may look something like….
public Form2(TextBox f1TextBox) {
InitializeComponent();
ds2.DataBindings.Add("Text", f1TextBox, "Text");
}
This should be all you need. When the text is changed in Form1’s text box, the text box in Form2 is automatically updated.
I have two controls
a FormBox with a Button and a TextBox
a FormView with a ListView
I want to send the text from the textbox from FormBox to the FormView when I click the button.
There are several solutions to this request...
Maybe the simplest one is to pass to FormBox a reference to the existing instance of FormView (e.g. in FormBox constructor)... then you have to set Public as Modifiers in your ListView object (you can do it through form designer in Visual Studio, selecting your ListView object and then editing its properties).
Finally write something like:
myFormView.myListView.Items.Add(new ListViewItem(myTextBox.Text));
in your button click event handler.
This is not the best solution from the stylistic point of view, but maybe it's the simplest.
Accept this answer if it answers your question.
Forms are classes. Easy way is to define constructor for the second form that takes string as input. Now in the first form (where you have the button), instantiate the second form in button click event:
private void button1_Click(object sender, System.EventArgs e)
{
Form2 frm=new Form2(textBox1.Text);
//...
}
Now in the second form you get this text value as a string in the constructor. Hold this value in a string variable (for example: listVal) and add this value to the list:
var listViewItem = new ListViewItem(listVal);
listView1.Items.Add(listViewItem);
This is a way you can solve your problem. Please provide your own code work while asking any question.
I am having a problem with having the eventhandler textchanged method to work with an array of textboxes. The textboxes are generated through C# and not through ASP.NET.
here is the TextBox code in C#:
int i = 1;
foreach(string a in data)
{
i++;
TextBox text = new TextBox();
text.TextChanged += new EventHandler(updateone);
text.AutoPostBack = true;
text.ID = Convert.ToString(i);
}
I tried out the Text.AutoPostBack false and true and I had the same result. The updateone method is not even touched when I change the text of the textbox. When I do change the text
of the textbox it does update the website, but again the updateone method is not even touched in the code. Here is the updateone code:
protected void updateone(object sender, EventArgs e)
{
TextBox text = (TextBox)sender;
}
I thank everyone for their help! I am just confused why this is not working... and also I have to use the C# method and not the ASP.NET way.
have you tried storing references to your TextBoxes in an instance member so that they don't get garbage collected?
something like:
List<TextBox> textBoxes = ...
//in a loop
text.ID = Convert.ToString(i);
textBoxes.Add(text);
-- edit
also, as a rule of thumb, put as much logging in your application as possible.
Whether it is NLog, any other logging tool or even a simple Console.WriteLine(), seeing what your code is actually doing is very helpful.
Since you've confirmed that you're posting your actual code and don't seem to get what I'm trying to say, let me try to explain in an answer.
First problem: Your array of TextBoxes does not exist, which the other answer has already addressed and you've apparently fixed but haven't updated the code in your question to show your fix.
Second problem: The TextBoxes you create are not being added to your form in any way. I'm not sure how you're testing your event handler without doing that.
Third problem: Your event handler updateone doesn't do anything. Imagine you walk into a grocery store, pick up an orange, put it back down, and then leave. That's what your event handler is doing. Instead of just instantiating a temporary TextBox and then doing nothing, try making a message box pop up, or changing the text of another control that exists on the form.
Maybe something like this will work:
List<TextBox> textboxes = new List<TextBox>();
int i=1;
foreach(string a in data) // I assume data is a list or array of strings
{
// I'm not sure why you iterate over data if you don't use it at all inside the loop...
++i;
TextBox text = new TextBox();
text.TextChanged += new EventHandler(updateone);
text.AutoPostBack = true;
text.ID = Convert.ToString(i);
// Add the TextBox to form here, not sure what the call is
}
Label info = new Label;
Label.Text = "Hello!";
// Add Label to form here, again not sure what the call is
And then your event handler:
protected void updateone(object sender, EventArgs e)
{
info.Text = ((TextBox)sender).Text;
}
You are dynamically creating ASP.NET controls. This means that they will not be automatically re-created on the postback of the form. Also, the controls have to be created in the Page_Init event, not the Page_Load event.
So the question is, where and when are you creating the textboxes. Make sure they are created at the Page_Init stage, and you are creating them in the request and in the postback.
I have a Form (Form1) and a Rich Text Box (RichBox1) inside of it.
I have many objects that can append text in RichBox1
and Two Other Forms (Form2, Form3)
When that happens, if the application does not have focus (say user is using a calculator)
and when the user sets focus back to the application thru selecting Form2,
Form1 gets focus first if the RichBox1 got new text while the user wasn't using it.
I've looked at all properties and can't find it. a .CanFocus for the textbox cannot be set to false as it can only be read. Any ideas?
Thanks
Edit:
I've Added:
this.GotFocus += new EventHandler(Form1_GotFocus);
This event does not fire at all.
I have a whole bunch of other controls in Form1, a few labels, buttons and menu strips. I do not understand what is causing this behaviour.
EDIT 2:
Perhaps can someone suggest an event to raise in Form1 to start tracking what is causing this. I am assuming it has to do with RichBox1 but not so sure now.
EDIT 3.
Added:
this.Activated += new EventHandler(Form1_GotFocus);
This event does not fire when I do the above. Perhaps this means that Form2 loses focus, instead of Form1 getting activated? Also worth noting this behaviour appears when I right click in Form2
Try using the Form Activated event. Though I made a small test case and I can not duplicate your problem, if the RichTextBox's Text Changes I can still select Form2, make sure you do not have a timer or event setting focus to the RichTextBox
public Form1()
{
InitializeComponent();
this.Activated += new EventHandler(Form1_Activated);
}
void Form1_Activated(object sender, EventArgs e)
{
//Set the Focus to the Control that you want
button1.Focus();
}
I created a form with a label, textbox and a button. In the form load event I called the focus() function for the textbox. But when I run my code the cursor is not coming to textbox. I need the cursor to go to text box as soon as the form is loaded. How to do it?
If you simply need to make sure a certain control gets focus when you first load a form, then change the TabOrder properties of all your controls (in the Designer) so that the control in question is '0', and the other elements are going up from there, '1', '2', etc.
If you need to dynamically select a different control when you show a form depending on some condition, then use the following code:
private void Form1_Load(object sender, EventArgs e) {
// You need to show the form otherwise setting focus does nothing
// (there are no controls to set focus to yet!)
this.Show()
if (someCondition == true)
control.Focus();
else
control2.Focus();
}
Handle the Shown event instead. This code should work.
private void Form1_Shown(object sender, EventArgs e)
{
textBox2.Focus();
}
Don't call Focus in the Load event. Call it in the Activate event. That would work
You can set the TabIndex property of textbox to 0 if you always want the focus on textbox when form loads. (This property is always eventually set in the form.designer.cs. And you won't have to write any extra code in your form.cs.)