I'm having a very strange problem with statuicon.
I'm doing on a simple project to save and show some data in table, I have a mainwindow(MainWindow) where the user insert the data and then there is another window where are the data shown(SumList). Also there is a status icon which I have created by sublassing the Gtk.StatusIcon. The problem is that when I start the application and show the window that should show the data(everything works) and then close the window (no matter how) the statusIcon disappear from the panel.
Also I have found that it is cause be the length of constructor of the class SumList. If I delete some lines from there (random order) the statusicon works fine.
How can I fix this strange behavior?
EDIT #1
I try not to subclass the StatusIcon instead i have declared as static member of MainClass and now it works as it should, weird. Anyway the question is why it isn't working if the statusIcon is not declared static?
The MainClass (StatusIcon)
class MainClass : StatusIcon
{
MainWindow window;
private MainClass()
{
window = new MainWindow();
window.Show();
Stock = Gtk.Stock.Home;
PopupMenu += rightClick;
Activate += leftClick;
}
private void rightClick (object sender, Gtk.PopupMenuArgs evt){
window.Hide();
}
private void leftClick (object sender, EventArgs e){
window.Show();
}
public static void Main (string[] args)
{
Application.Init ();
new MainClass();
Application.Run ();
}
}
The SumList class
public partial class SumList : Gtk.Window
{
public SumList () : base(Gtk.WindowType.Toplevel)
{
this.Build ();
// create the "title" column ------------ //
TreeViewColumn title = new TreeViewColumn();
CellRendererText titleR = new CellRendererText();
title.PackStart(titleR, true);
title.AddAttribute(titleR, "text", 0);
// create the "detial" column ----------- //
TreeViewColumn detail = new TreeViewColumn();
CellRendererText detailR = new CellRendererText();
detail.PackStart(detailR, true);
detail.AddAttribute(detailR, "text", 1);
// create the "price" column ------------ //
TreeViewColumn price = new TreeViewColumn();
CellRendererText priceR = new CellRendererText();
price.PackStart(priceR, true);
price.AddAttribute(priceR, "text", 2);
// create the "date" column ------------- //
TreeViewColumn date = new TreeViewColumn();
CellRendererText dateR = new CellRendererText();
date.PackStart(dateR, true);
date.AddAttribute(dateR, "text", 3);
// set the columns names
title.Title = "Title";
detail.Title = "Detail";
price.Title = "Price";
date.Title = "Date";
// append columns to the treeview
this.treeview.AppendColumn(title);
this.treeview.AppendColumn(detail);
this.treeview.AppendColumn(price);
this.treeview.AppendColumn(date);
// set the model
this.treeview.Model = Singleton.Model.Instance.Data;
}
}
The MainWindow class
public partial class MainWindow: Gtk.Window{
public MainWindow (): base (Gtk.WindowType.Toplevel){
Build ();
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a){
Application.Quit ();
a.RetVal = true;
}
protected void OnButtonOKClicked (object sender, System.EventArgs e){
SumList list = new SumList();
list.Show();
}
protected void onButtonHideClicked (object sender, System.EventArgs e){
entrySum.Text = "";
entryTitle.Text = "";
this.Hide();
}
}
Simple, your GTK control is getting garbage collected.
public static void Main (string[] args)
{
Application.Init ();
new MainClass();
Application.Run ();
}
You now no longer have any live references to your MainClass instance. IMO you are lucky that the program even does this.
Related
I am making an application that loads a separate form, the user puts in information, and then when done, it will show up on the primary form the application loaded with first.
The issue is that I tried multiple solutions to get this to load in, but it will not load in after the information is put in. I have tried this.Controls.Add(Label); which is what I have seen the most, but it has not worked. Another way I tried was doing Label.Show();, but the same result, with nothing showing. The AddContacts(string Name) method below is how I add the contact
The AddContact_Click(object sender, EventArgs e) method is a button that, when pressed, opens another form that allows information to be inserted.
public partial class Phonebook : Form
{
public Phonebook()
{
InitializeComponent();
MaximumSize = new Size(633, 306);
}
private void AddContact_Click(object sender, EventArgs e)
{
MakeContact MC = new MakeContact();
MC.Show();
}
public void AddContacts(string Name)
{
Label name = new Label();
//Added Style and Location of Label...
name.Text = Name;
name.Location = new Point(98, 13);
name.Font = new Font("Microsoft Sans Serif", 13, FontStyle.Bold);
this.Controls.Add(name);
Refresh();
}
}
Below is the Method I used when the Finish button is pressed, for when the user is done with the information, and then the AddContacts() method is called
public partial class MakeContact : Form
{
public MakeContact()
{
InitializeComponent();
MaximumSize = new Size(394, 377);
}
private void FinishContact_Click(object sender, EventArgs e)
{
//FullName is the name of the TextField when asking for a name
string Name = FullName.Text;
Phonebook PB = new Phonebook();
PB.AddContacts(Name);
//Closes Separate Form and goes back to the
Close();
}
}
Expectation:
It should load the label into the form after the information is put in.
Actual:
It will not show what so ever.
EDIT: Added More to the Code and to the Question since I didn't do too good of asking the question, sorry about that :/
An example of what I described in the comments:
When you do this:
Phonebook PB = new Phonebook();
you create a new instance of the PhoneBook class (your form): this is not the same Form instance (the same object) that created the MakeContact Form and the one you're trying to update. It's a different object.
Whatever change you make to this new object, it will not be reflected in the original, existing, one.
How to solve:
Add a Constructor to the MakeContact Form that a accepts an argument of type PhoneBook and a private object of type Phonebook:
private PhoneBook pBook = null;
public MakeContact() : this(null) { }
public MakeContact(PhoneBook phoneBook)
{
InitializeComponent();
this.pBook = phoneBook;
}
Assign the argument passed in the constructor to the private field of the same type. This Field will then used to call Public methods of the PhoneBook class (a Form is a class, similar in behaviour to other class).
It's not the only possible method. You can see other examples here.
Full sample code:
public partial class Phonebook : Form
{
private void AddContact_Click(object sender, EventArgs e)
{
MakeContact MC = new MakeContact(this);
MC.Show();
}
public void AddContacts(string Name)
{
Label name = new Label();
// (...)
this.Controls.Add(name);
}
}
public partial class MakeContact : Form
{
private PhoneBook pBook = null;
public MakeContact() : this(null) { }
public MakeContact(PhoneBook phoneBook)
{
InitializeComponent();
this.pBook = phoneBook;
}
private void FinishContact_Click(object sender, EventArgs e)
{
string Name = FullName.Text;
this.pBook?.AddContacts(Name);
this.Close();
}
}
When I go to run the program it is empty, no data displayed
namespace _0000003
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
soulpets = new ListView();
ListViewItem lvi6001 = soulpets.Items.Add("6001");
lvi6001.SubItems.Add("Tough Ent, Rare");
ListViewItem lvi6004 = soulpets.Items.Add("6004");
lvi6004.SubItems.Add("Stone Fist Ent, Rare");
ListViewItem lvi6007 = soulpets.Items.Add("6007");
lvi6007.SubItems.Add("Healing Ent,Rare");
Controls.Add(soulpets);
soulpets = new ListView();
should be soulpets.Items.Clear();
You probably want:
add a listview via the designer with the name soulpets
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
fillListViewSoulpets();
}
private void fillListViewSoulpets()
{
soulpets.Items.Clear(); //it should already be empty
soulpets.Items.Add(
new ListViewItem({ "Tough", "Ent", "Rare" }),
new ListViewItem({ "Stone", "Fist", "Ent", "Rare" }),
new ListViewItem({ "Healing", "Ent", "Rare" })
);
}
}
I come to ask because i'm new using windows forms and i need some help.
I have a Windows Forms project with 2 forms. The MainForm and the InnerForm.
I'm trying to access to a TableLayoutPanel in MainForm from InnerForm to add new rows in this tablelayoutpanel with some actions that happen in the InnerForm.
I have the next code:
MainForm:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
TableLayoutPanel panel = tableLayoutPanel1;
panel.ColumnCount = 4;
panel.RowCount = 1;
panel.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
panel.Controls.Add(new Label() { Text = "Tag/ID" }, 0, 0);
panel.Controls.Add(new Label() { Text = "Tipo" }, 1, 0);
panel.Controls.Add(new Label() { Text = "Acción" }, 2, 0);
panel.Controls.Add(new Label() { Text = "Ejecutar" }, 3, 0);
}
private void AddInnerForm(string url)
{
var inner = new InnerForm(url)
// more code
}
public void agregarRow(ConsoleMessageEvents args){
// some action with tableLayoutPanel1
}
}
InnerForm:
public partial class InnerForm : UserControl
{
MainForm theMain;
public InnerForm(MainForm main)
{
theMain = main;
}
public InnerForm(string url)
{
InitializeComponent();
// more code
}
private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
{
theMain.agregaRow(args);
}
}
but when I'm debugging the program I get this error:
An unhandled exception of type 'System.NullReferenceException' occurred in Project.exe
Additional information: Object reference not set to an instance of an object.
in this line in InnerForm:
private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
{
**theMain.agregaRow(args)**;
}
What is the problem here?
That should fix it. The field theMain was not initialized, also you need to call InitializeComponenets in every constructos to create the child controls.
public partial class InnerForm : UserControl
{
MainForm theMain;
public InnerForm(MainForm main)
{
InitializeComponent();
theMain = main;
}
public InnerForm(string url, MainForm mainForm)
{
this.theMain = mainForm;
InitializeComponent();
// more code
}
private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
{
theMain.agregaRow(args);
}
}
And in MainForm:
private void AddInnerForm(string url)
{
var inner = new InnerForm(url, this)
// more code
}
From your code it easy to see what you're initializing theMain variable in this constructor:
public InnerForm(MainForm main)
{
theMain = main;
}
But in your code var inner = new InnerForm(url) you're using another constructor which is not initializing this variable - so theMain is left null and you're getting that exception when trying to access its method.
public InnerForm(string url)
{
InitializeComponent();
// more code
}
I need help passing data from one WPF form to another. I have a main window with two other windows that will prompt the user for information. I want to end up with all the information in the first form so that I can store the data later on. The second form must return the Reservation and Room information when you click the OK button on the second form. The third form must return the Person information when you click OK.
public partial class MainWindow : Window
{
private string message;
public MainWindow()
{
InitializeComponent();
}
protected void Exit_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
protected void Create_Reservation_Click(object sender, RoutedEventArgs e)
{
Reservation PersonReservation = new Reservation();//Create a reservation instance
Room PersonRoom = new Room(); //Create an instance of a room
Person myPerson = new Person();//Create an instance of a person
CreateResRoom createReservationRoom = new CreateResRoom();//Create a instance of the CreateReservation WPF Form
createReservationRoom.Show();
Here it is supposed to set the room, reservation and person instance that I created equil to their corresponding instances in the CreateResRoom class.
I think the problem lies here, because it keeps continuing before it opens the CreateResRoom form.
PersonRoom = createReservationRoom.myRoom;
PersonReservation = createReservationRoom.myReservation;
}
}
That was my first class, the second and third will follow.
public partial class CreateResRoom : Window
{
Person myPerson;
public CreateResRoom()
{
InitializeComponent();
myReservation = new Reservation();
myRoom = new Room();
myPerson = new Person();
}
public Room myRoom
{
get;
set;
}
public Reservation myReservation
{
get;
set;
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
myRoom.RoomBeds = txtHeadCount.Text;
myRoom.RoomNumber = 1;
myRoom.RoomPrice = 20;
myRoom.RoomType = cboRoomType.Text;
myReservation.ResEndDate = dpEnd.ToString();
myReservation.ResStartDate = dpStart.ToString();
CreateRes createReservation = new CreateRes();
createReservation.Show();
//I think the same problem lies here that is in the MainWindow.
myPerson = createReservation.myPerson;
this.Close();
}
}
And the last class follows:
public partial class CreateRes : Window
{
public Person myPerson
{
get;
set;
}
public CreateRes()
{
InitializeComponent();
myPerson = new Person();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
myPerson.FirstName = txtFName.Text;
myPerson.LastName = txtLName.Text;
myPerson.IdNumber = Convert.ToInt32(txtIdNumber.Text);
myPerson.PhoneNumber = Convert.ToInt32(txtPhoneNumber.Text);
myPerson.AddressCity = txtAddressCity.Text;
myPerson.AddressStreet = txtAddressStreet.Text;
myPerson.AddressProvince = txtAddressProvince.Text;
myPerson.AddressPostalCode = txtAddressPostalCode.Text;
this.Close();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
Just make a overload constructor which takes parameters of the window in which you want to retrieve.
Example:
Suppose we want a user to login from our MainWindow( i.e Login Window ) and we want to pass an int ID / string Email to our second form to retrieve data of logging user.
Than We have to first overload our second wpf form constructor. You can either make default constructor to do this or make an overload constructor for this work.
SecondForm:
public secondForm()
{
//Your Default Constructor Logic
}
public secondForm(string email_ )
{
//Your Overload Constructor Logic
}
Now in MainWindow from where we are logging and passing our EMail
MainWindow:
public void btnLogin()
{
//On Success
SecondWindow sw = new SecondWindow(txtBoxEMail.Content);
sw.Show();
}
A pattern you can use for this sort of thing is to have each form be responsible for creating the instance on ok click and then provide the object via a property get.
public partial class SomeForm: Window
{
public SomeClass MyProperty { get; private set; }
private void btnOk_Click(object sender, RoutedEventArgs e)
{
this.MyProperty = new SomeClass();
//additional setter logic here
this.Close();
}
}
Then you would access it from a parent form like this (notice the use of ShowDialog() http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog(v=vs.110).aspx for easy checking of whether ok was clicked or not).
protected void Create_Reservation_Click(object sender, RoutedEventArgs e)
{
SomeClass myObj;
SomeOtherClass myOtherObj;
SomeForm myForm = new SomeForm();
if(myForm.Show().Value)
{
myObj = myForm.MyProperty;
}
SomeOtherForm myOtherForm = new SomeOtherForm();
if(myOtherForm.ShowDialog().Value)
{
myOtherObj = myOtherForm.MyOtherProp;
}
//save myObj & myOtherObj or whatever you need to do with them
Use the "normal way", here is a short overview.
First create a Data Context:
public class DC_Reservation() : INotifyPropertyChanged {
protected Reservation _PersonReservation ;
public Reservation PersonReservation {
get { return _PersonReservation ; }
set {
_PersonReservation = value;
NotifyPropertyChanged("PersonReservation ");
}
}
protected Room _PersonRoom ;
public Room PersonRoom {
get { return _PersonRoom ; }
set {
_PersonRoom = value;
NotifyPropertyChanged("PersonRoom");
}
}
protected Person _myPerson ;
public Person myPerson {
get { return _myPerson ; }
set {
_myPerson = value;
NotifyPropertyChanged("myPerson ");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged( string PropertyName ) {
if ( PropertyChanged != null ) {
PropertyChanged( this, new PropertyChangedEventArgs( PropertyName ) );
}
}
}
In the MainWindows you can assign and use the dataContext :
public partial class MainWindow : Window {
DC_Reservation dataContext {
get { return DataContext as DC_Reservation; }
}
private string message;
public MainWindow() {
InitializeComponent();
DataContext = new DC_Reservation();
}
protected void Create_Reservation_Click(object sender, RoutedEventArgs e) {
dataContext.PersonReservation = new Reservation();//Create a reservation instance
dataContext.PersonRoom = new Room(); //Create an instance of a room
dataContext.myPerson = new Person();//Create an instance of a person
CreateResRoom createReservationRoom = new CreateResRoom();//Create a instance of the CreateReservation WPF Form
// I'm not sure whether the next line is required.
createReservationRoom.DataContext = DataContext;
createReservationRoom.Show();
}
}
You can assign the DataContext in the constructor, but I think the better way is to define the DataContext in the MainWindow, in the other windows you can use the DesignContext:
<Window.DataContext>
<local:DC_Reservation />
</Window.DataContext>
So you can use the same DataContext over all forms ...
With DataBindings you can bind the input to the field:
<TextBox Text="{Binding FirstName, Path=myPerson, Mode=TwoWay}" />
I found another answer that Zarathos posted Jan 16 '13 at 21:43
for a different question
Use a public static class and access it from anywhere.
public static class Globals
{
public static String s_Name = "Mike"; //Modifiable in Code
public const int32 VALUE = 10; // unmodifiable
}
Then you can use it anywhere, provided you are working on the same namespace
string name = Globals.s_Name;
Please, you can you help me, how can i change label text in diferent class?
Basic winform script:
public partial class buildEditor : Form
{
public buildEditor()
{
InitializeComponent();
Label maxSkillPoint = new Label();
maxSkillPoint.AutoSize = true;
maxSkillPoint.BackColor = System.Drawing.Color.Transparent;
maxSkillPoint.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
maxSkillPoint.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(193)))), ((int)(((byte)(196)))), ((int)(((byte)(181)))));
maxSkillPoint.Location = new System.Drawing.Point(528, 687);
maxSkillPoint.Name = "maxSkillPoint";
maxSkillPoint.Text = UniqueValue.spentSkillPoints.ToString();
maxSkillPoint.Size = new System.Drawing.Size(0, 20);
this.Controls.Add(maxSkillPoint);
}
public void maxSkillPoint_TextChanged(Form formInstance, string labelName)
{
// Get reference to the label
var label = formInstance.Controls.Find(labelName, true).FirstOrDefault();
if (null != label && label is Label)
{
(label as Label).Text = "test";
}
}
}
I created next class which will be change text for maxSkill text.
public class ChangeTextForMaxSkill()
{
Button button = new Button();
public ChangeTextForMaxSkill()
{
button.Click += new EventHandler(changeText);
}
private void changeText(object sender, EventArgs e)
{
// Get reference to the label
var buildEditor = new buildEditor();
buildEditor.maxSkillPoint_TextChanged(buildEditor, "maxSkillPoint");
}
}
I really thx for all answers.
I got it very simple:
Hand over the Label control in the constructor of your external class:
using System.Windows.Forms;
public class Yourclass{
private Label UpdateLabel;
public Yourclass (Label yourLabel)
{
this.UpdateLabel = yourlabel;
}
private void action()
{
//here is your update of the label
UpdateLabel.Text = "Your text";
}
}
In the form class, create an instance of "yourclass" and hand over the Label:
Yourclass cls = new Yourclass(Label1);
First of all your naming conventions do not follow standard practices. Both class and method names should use uppercase first letters of words, not camel case as you have done. I have used proper naming conventions in my answer.
You have to pass an instance of your BuildEditor* form to your ChangeTextForMaxSkill.ChangeText() function.
Next, the label object maxSkill is not a property of your BuildEditor class. Therefore, you'd need to actually find a reference to that control within the form since you're dynamically adding it.
public partial class BuildEditor : Form
{
public BuildEditor()
{
InitializeComponent();
Label maxSkill = new Label();
maxSkill.Name = "MaxSkil"; // need the ID to find it later (makes it easier)
maxSkill.Location = new Point(1, 1);
this.Controls.Add(maxSkill);
}
// This is just for testing; assumes you dragged a button from toolbox onto your
// BuildEditor form in the Form Designer
private void button1_Click(object sender, EventArgs e)
{
var changeTextForMaxSkill = new ChangeTextForMaxSkill();
changeTextForMaxSkill.ChangeText(this, "MaxSkil");
}
}
public class ChangeTextForMaxSkill
{
public void ChangeText(Form formInstance, string labelName)
{
// Get reference to the label
var label = formInstance.Controls.Find(labelName, true).FirstOrDefault();
if (null != label && label is Label)
{
(label as Label) .Text = "test";
}
}
}
If you want to test it, just add a button on your form and make the test in the button click handler:
private void button1_Click(object sender, EventArgs e)
{
var changeTextForMaxSkill = new ChangeTextForMaxSkill();
changeTextForMaxSkill.ChangeText(this, "MaxSkil");
}
I've tested and this works :)