Put text from TextBox with a Button into a ListView - c#

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.

Related

C# designer form not updating methods of form objects?

I've been following this tutorial to name the elements on a windows form page. From what I gathered, the form will auto generate methods based on the name of the elements on the designer form.
However, while most of the textboxes, labels do update their method name when I click them and hit ENTER, some of them still reference to the default name.
In the attached image the label named "Option" remains as "label3_clicked".
Much appreciated.
Attached image of problem.
I never experience the behavior where the name of the event (which can be anything) is automatically updated to match the name of the control.
This is because the name of the event can be just anything (label3.Clicked += Whatever_Click_Whatever or even without the click Part). There is no naming rules that the compiler can check. There is just the default for auto generated ones.
What if you have a custom name that you don't want to change when changing the control name? So there is no automatic behavior here.
So what you can do is go to Form1.Designer.cs find the event like this:
this.Load += new System.EventHandler(this.Form1_Load);
Then Right click => Rename or Ctrl + R, Ctrl + R and change the name. This changes it is your form's code also.
You created the click event handler method while the Label was called label3. To fix this highlight the method name in your code behind and F2. That will enable you to change (refactor) the name of the method which in turn, will change the form designer as well.
In future just name your objects first and then create your event handler methods and all should be good.
It is a simple issue. You can fix this by creating a function when form is loaded. Set the default value for the form object in Form1_Load() function to change it on RunTime. You can get that function from form events. For example, If I want to change the name of the form, then approach is like
private void Form1_Load(object sender, EventArgs e) {
this.Text = "Initial Text"
}
After that, On your event which you want to change the Form Name. For example, I'm taking some function name
private void updateFormName_click(object sender, EventArgs e) {
this.Text = "Updated Text"
}

Add Items in ComboBox using Textbox Value from another form in C#

I have 2 Window Forms, Form1 and Form2. Form1 has a ComboBox and Form2 has a Textbox and a Button.
I hope you can help me with this one. What I would like to happen is, if I input a string value in the TextBox in Form2 and hit the Button1 that is also located in my Form2, the value of that TextBox will be an item for my ComboBox that is located in my Form1.
I just would like to ask if is there any way that we can do this? Could you provide an example for me? I'm looking forward to your help.
It would help a lot if you'd include what attempt code you have so far, and/or what obstacles you're encountering, but nonetheless this is a simple problem that's a bit difficult to search for, so hopefully this will help.
First, I'm assuming you've instantiated and displayed both forms. Next you'll want to find the Designer.cs (formNameHere.Designer.cs) file in your Solution Explorer, then locate the variable declaration for your combo box (should be near the bottom). Change its access modifier from 'private' to something more appropriate.
Now go back to your Form1 code file and add this to your button click event handler:
form2.comboBox1.Items.Add(textBox1.Text);
... where 'form2', 'comboBox1', and 'textBox1' are your child form, combo box, and text box from which you are sending the new combo box item.
Let me know if you have any further questions.
I think best option is to communicate between the forms through events. So on button click event trigger an event and subscribe to it from the Form1. When the event is raised, add the text to the combobox
In Form2:
internal event EventHandler<string> NewItemToAdd;
void button1_clicked(object sender, EventArgs e)
{
if(NewItemToAdd != null)
NewItemToAdd(textbox.Text);
}
In Form1 subscribe to the event NewItemToAdd and add the text to your combobox when the event is raised

how to access a user define controls iner controls in parent form

I'm new to windows forms programming so my question may sound little strange.
I have created a user define control (countdown timer) now I'm creating n no of it dynamically in a form by Click of a button (Add new timer) its working well and good.
Here is the Creation Code
private void Addnew_Click(object sender, EventArgs e)
{
UserControl1.userControl11 = new UserControl1();
flowLayoutPanel1.Controls.Add(userControl11);
}
My user control has a Reset button that reset all the content inside the user define control.
it is also working, but What I want Allow user to reset all the Created timers using the “Reset All” button on the form.
Okay one way to do this.
Create a List<UserControl1> private member on your form called say _myUserControls
In your Addnew Handler add it to the list.
If you have a remove button, don't forget to remove from _myUserControls as well.
Add a Reset method to your UserControl1, that does what it needs to do.
Then in your Reset all button click handler
foreach(UserControl1 ctrl in _myUserControls)
{
ctrl.Reset();
}
Jobs a good 'un
The answer I referred you to in comments, would be a way of finding all instances of your UserControl1 class, so you wouldn't need an internal list.

How to use same textbox with other tabs on the tabcontrol?

Sorry for the simple question and also sorry if there is an answer on the site and I couldn't find.
I want to use same textbox in every tab that I use on my form. How can I do that?
Add this to the TabControl's SelectedIndexChanged event handler.
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox1);
}
Adding a control to one tab page's Controls collection automatically removes it from the others.
Note: I have tested it by adding two lables on the form above the tab control and added these two lines to the method shown above:
label1.Text = tabControl1.TabPages[0].Controls.Count.ToString();
label2.Text = tabControl1.TabPages[1].Controls.Count.ToString();
Put the TextBox in the parent control of the TabControl. It can hover over all the rest. You might need to rework the focus traversal though.

passing value to unspecified object using delegate C#

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.

Categories

Resources