I want my items list in a listbox but when i try
listbox1.Items.Add("Item1");
nothing is being added but
if i place the code in the forms load metod it works but when I call it from the
separate code module it does not. I think this is because another instance
of the form is being updated.
but how do i get the active form and then add the items.
I got a Abstract class and tree subclasses and i want my subclasses in a list and then showed on the form.
this was my first attempt but this dosent work.
private void button1_Click(object sender, EventArgs e)
{
Subclass o = new Subclass();
List<BaseClass> l = new List<BaseClass>();
l.Add(o);
Form1 f = new Form1();
f.AddObjectToListbox(l);
}
And then in my From1 i got
public void AddObjectToListbox(List<BaseClass> l)
{
foreach (Subclass objectname in l.OfType<Subclass>())
{
l.Items.Insert(0, "text" + O.getMetod);
}
}
but like it is now it just add to another instance of form1.
thanks for alle the help.
Your question is a litle bit vague...where are you calling listbox1.Items.Add("Item1"); from
e.g.
From a method in the Forms class file
From code in another file,
From code in a different assembly,
However, you can get the Active Form via the Form class static method:
System.Windows.Forms.Form.ActiveForm
May this helps
One problem might be the use of "Object" as both a class and variable name in the AddObjectToListBox method.
Also if you are casting obj to Subclass, but then assign it to a variable of type Object, that is not making much sense. You could solve both of these at once, like this.
foreach (Subclass obj in l.OfType<Subclass>())
{
lbVehicles.Items.Insert(0, "text" + obj.getMetod);
}
As a third, it looks like you're creating a new form every time the button is clicked, is that intentional?
suddenly you find what you was looking for yourself.
this did it.
Form currentForm = Form.ActiveForm;
ListBox lb = (ListBox)currentForm.Controls.Find("ListboxName", true)[0];
but still thanks for looking at my question.
First try to clear items and then try to add
Related
I've been working on making a project of mine more modular. Something I've wanted to do is have multiple buttons use the same function when they perform a similar action, but with different values. I've been stuck on trying to apply this to the following situation:
"When this button is clicked, have the user select an image, and then have a PictureBox display the selected image". Each button has its own PictureBox. All Controls have been created before runtime.
Hope that makes sense!
My last attempt can be seen in the code below- I have tried assigning the Controls(Button and PictureBox) to variables to be stored together in a class. There's 6 of these classes all included within a single List.
I've also tried to store only the Control Names and then using this.Controls.Find to retrieve the Controls.
I've tried quite a few smaller changes such as passing by reference, making the List static, and things such as that would (somehow)magically do the trick- I've gotten desperate.
public class score_control
{
public Button score_button;
public PictureBox score_picture;
public int picture_index;
}
public List<string> score_boxes_names = new List<string>();
public List<score_control> score_boxes = new List<score_control>();
public void add_score_control(Button button, PictureBox pictureBox)
{
score_control new_score = new score_control();
new_score.score_button = button;
new_score.score_picture = pictureBox;
new_score.picture_index = score_boxes.Count();
score_boxes.Add(new_score);
score_boxes_names.Add(button.Name);
}
public score_control find_score_control(string name)
{
int index = score_boxes_names.IndexOf(name);
return score_boxes[index];
}
public frm_settings()
{
InitializeComponent();
add_score_control(btn_score1_image1, pic_score1_image1);
add_score_control(btn_score1_image2, pic_score1_image2);
add_score_control(btn_score1_image3, pic_score1_image3);
add_score_control(btn_score2_image1, pic_score2_image1);
add_score_control(btn_score2_image2, pic_score2_image2);
add_score_control(btn_score2_image3, pic_score2_image3);
}
private void score_button_Click(object sender, EventArgs e)
{
Button image_button = (Button)sender;
if (ofd_png.ShowDialog() == DialogResult.OK)
{
score_control clicked_control = find_score_control(image_button.Name);
score_image[clicked_control.picture_index] = ofd_png.FileName;
clicked_control.score_picture.Image = Image.FromFile(ofd_png.FileName);
}
}
The problem seems centered around this line:
clicked_control.score_picture.Image = Image.FromFile(ofd_png.FileName);
The program throws a NullReferenceException , but clickedcontrol is being recognized in the Local Watch, as well as score_image being noted to be a PictureBox(as it should be).
When I instead held the Control Names in the class, I had broke this line down into multiple lines, but the following line produced a NullReferenceException:
Control[] find_control = this.Controls.Find(clicked_control.score_picture, true);
In this case, clicked_control.score_picture would be a string containing the PictureBox Name. Again, the Local Watch showed that it clicked_control was not null, and neither was score_picture.
Any help figuring out how to properly store a Control within a variable to later be used to modify that Control's properties would be greatly appreciated.
dontpanic was able to help me out with this one. The issue was actually outside of this code - it had to do with the line score_image[clicked_control.picture_index] = ofd_png.FileName;. The way score_image was initialized as an array was incorrect. Fixing that made everything work fine.
If I have the following code:
Button[] _buttonarray = new Button[40]; // it is outside any function (situated in the public partial class MainWindow : Window)
And a new created button in a function called
private void createbutton()
{
_buttonarray[b]=new Button();
_buttonarray[b].Content = "Content";
...
}
How Can I edit _buttonarray[b] content from another function like,
private void editbutton()
{
_buttonarray[b].Content = "New Content";
}
Note: variable b is outside so it can be change from any function.
Make it Static:
public static Button[] ButtonArray = new ..
and use MainWindow.ButtonArray to access it.
Worth pointing out that by doing that, it is shared throughout every instance of your MainWindow.
Edit:
Just to point out - rather than saying 'outside' it is more common to use the definition of Scope , simply put - if you can access something A from somewhere B,then A is in B's Scope.
Also - read more regarding static here: Static and instance fields
If you want to edit a specifically created button, you can keep that button in a designated field, which is in both the creation code's scope and the alteration code's scope:
var myButton = new Button(){Content="Content"};
_buttonarray[b]=myButton;
SomethingInCommon.SpecificButton = myButton;
and the access it elsewhere that have access to SomethingInCommon.
Keep SpecificButton value until you don't need it any longer.
You're editing a button with the code you have, it's just that it's probably not the one you're expecting because the index of b has likely been iterated. Just access the right index, possibly by using Find and a predicate on Content to make sure you're editing the right button.
I have a query regarding maintaining a List in between two windows forms. It's for a project where I need to create an address book.
I have chosen to maintain the contact details in the form of a List. My first windows form (form1) contains a master copy of a list AddressBook, which contains the address book.
I hardcoded 4 entries into the address book list in order to experiment and get the simple functions such as 'add' and 'edit' working.
I have a second windows form called Add, in which I can add new entries to the list. This works fine. I can add a new contact in the ADD form and this shows up in the initial form1, master form.
My problem arises in the EDIT form. I pass the AddressBook (master) list to the EDIT form. The EDIT form takes the master list and I am able to manipulate the records in that list. However when it comes to sending back the new list to the master page (form1), it does not pick it up. I am using the same code as I do in the ADD form which successfully sends back the new list. However this code does not work when sending back an edited list.
Here is my AddressBook property within form1
public List<Contact> addressBook;
public List<Contact> AddressBook
{
get { return addressBook;}
set {addressBook = value;}
}
Within EDIT:
public Edit()
{
InitializeComponent();
temp = Master.AddressBook; // temp is the temporary List I update within EDIT
}
** I then have my algorithm which successfully lets me EDIT the list temp. the list temp now has the edited list**
then when I hit the save button, I use the following code;
Master.AddressBook = temp;
All I need is for the list temp to be sent back to form1.
the code Master.AddressBook = temp; WORKS for when I add values to the list through the ADD form.
ADD FORM:
public Add()
{
InitializeComponent();
temp = Master.AddressBook;
}
**** code to add a new record into the list temp. the new record is called newRecord**********
private void btnAddClose_Click(object sender, EventArgs e)
{
stor.AddressBook = temp; // when I hit close on the form, it updates the master list AddressBook
this.Close();
}
This is all probably very poorly worded but in essence the only bit where my code fails is when I want to change my master Addressbook within form1 by replacing it with the list temp, which is the edited list from my EDIT form.
I think it's something to do with my AddressBook property. But this doesn't explain why I can replace AddressBook with a list containing new records but I can't replace it with a list containing edited records.
One way to accomplish this would be to make the list in Master static.
Master:
public static List<Contact> AddressBook { get; set; }
Note: You do not need the backing variable, and if you do want to use it, best practices would suggest that it be private. If you do decide to use it, it will also need to be static.
In the Add form, you would then gather the data to create a new Contact object and temp should, in fact, be just a Contact object.
Add Form:
private Contact newRecord = null;
public Add()
{
InitializeComponent();
newRecord = new Contact();
}
/**** code to add the user-input to the new Contact object ****/
private void btnAddClose_Click(object sender, EventArgs e)
{
Master.AddressBook.Add(newRecord);
this.Close();
}
Hope this helps.
This is where the Singleton pattern comes in handy: Implementing Singleton in C#
You will notice Application Settings uses this same patttern to allow you to globally access it without having to pass it around.
When I use a Singleton I typically make the class name like (TypeName)Manager (ex: AddressBookManager).
So the class might be something like this:
public static class AddressBookManager
{
#region Singleton
static readonly AddressBookManager instance = new AddressBookManager();
private AddressBookManager(); // prevent creating instances of this
public static AddressBookManager Current { get { return instance; } }
#endregion
AddressBook master = new AddressBook(); // the master address book
public AddressBook Master
{
get { return master; } // get the master address book
set { master = value; } // set the master address book
}
}
Then in each form you would access it like so:
var addressBook = AddressBookManager.Current.Master;
addressBook.Add(newRecord);
The problem you are experiencing with the Edit functionality probably has something to do with the way you are using temporary lists. By using a static, global list and merely adding/editing items inside of it, you don't run that problem. Since your Contact items are a class (not struct), their changes will be reflected in the list automatically since they are reference types.
The great part about Singleton classes is the ability to access them from anywhere in the project. The only caveat is that you need to be extra cautious when working with multi-threaded applications and Singleton classes.
I have a MyListView class that is inheriting from ListView, and is overriding OnDragDrop() (and the other necessary events to implement drag and drop). When I place two of these MyListviews on a form I am able to drag an item from one of them and drop it to the other one. This part works.
Now I want to override OnDoubleClick() to that class such that again if I place two of these MyListViews on a form and double clicked on one of them, the item gets removed from that and gets added to the other one. But I can't get my head around how to do this one.
Could you please give me some ideas? Thanks.
Don't know if you manage the sleection of the item in a particular way, but you can
or after handling double-click look for SelectedItems and act on it
or you can add a code like this using ListViewHitTestInfo class:
private override OnDoubleClick(...)
{
ListViewHitTestInfo hit = this.HitTest(e.Location);
if (hit.Item != null)
{
ListViewItem doubleClickedItem = hit.Item;
}
}
Put the logic in your host form by:
Handle double-click of first ListView
Remove from first ListView
Add to second ListView
Unless you are doing this in many different forms - it's not worth complicating it more than that.
EDIT:
If justified, centralizing can be as easy as adding a method which does the same thing (pseudocode)
public void MyForm_OnListViewDoubleClick(object sender, EventArgs e)
{
MoveListItem(firstListView, secondListView);
}
// ...
public static void MoveListItem(ListView source, ListView destination)
{
var listItem = source.SelectedItem;
source.Remove( listItem );
destination.Add( listItem );
}
Here's the answer to your title
protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick(e);
}
And here is the answer to your question
Using DoubleClick event on a inherited class from ListView
This just links back to your other, very similar question.
I have a winforms application.
I have a textbox on one form (call F1) and when a button is clicked on this form (call F2), it launches another form.
On F2, I want to set a string via a textbox (and save it to a variable in the class), and then when I close this form, the string will appear in a label in F1.
So I am basically sharing variables between both forms. However, I can't get this to work correctly. How would this code look?
I would add a new property to form2. Say it's for a phone number. Then I'd add a friend property m_phone() as string to form 2. After showing an instance of form2 but before closing it, you can refer to the property m_phone in form1's code.
It's an additional level of indirection from Matthew Abbott's solution. It doesn't expose form2 UI controls to form1.
EDIT
e.g.:
public string StoredText
{
get;
private set;
}
inside the set you can refer to your UI control, like return textBox1.text. Use the get to set the textbox value from an earlier load.
And:
public string GetSomeValue()
{
var form = new F2();
form.ShowDialog();
return form.StoredText;
}
Just ensure that StoredText is populated (or not, if appropriate) before the form is closed.
Are you showing the second form as a dialog, this is probably the best way to do it. If you can avoid doing shared variables, you could do the following:
public string GetSomeValue()
{
var form = new F2();
form.ShowDialog();
return form.TextBox1.Text;
}
And called in code:
Label1.Text = GetSomeValue();
This might not be the most efficient way of approaching, but you could create a class called DB (database). Inside this class, create variables like
public static bool test or public static bool[] test = new bool[5];
In your other forms, you can just create an instance. DB db = new DB(); then grab the information using db.test = true/false. This is what I've been doing and it works great.
Sorry, I'm only like a year late.