C# Set Checkbox to Static - c#

I have a checkbox that I created to windows forms, how can I set it to static?
public static CheckBox checkthis;
This code creates a new one as static, what I want to do is set one that I have created in the designer to static.
Update:
I tried below answer and it worked, though the checkbox disappeared from the form and various other issues kicked in. Instead I did this create a new one and did this:
public static CheckBox checkthisnew;
...
checkthisnew = checkthis;
Either way, I have now realised that I am fail and that I just can use the state changed on the events list, so all is well...
Sorry for not making my reasoning behind this more clear, I do appreciate your answers though, thank you.

Edit the MyForm.Designer.cs file, right where the declaration for your checkbox is. Note that your changes will be reverted if you use the designer to modify the UI again so you'll have to do this again.

Nothing good can come from making a dependent UI control static. It's one thing to make a component static and that could be ok, but for something like a CheckBox, you're just asking for trouble. For starters, a single control can only have one parent. So you can't just make a single instance of your control and expect to be able to add it to multiple forms and everything will magically appear to be in sync. If you need to share some values, do it the right way and bind to them, register some events, share the value and not the control that holds the value, or other similar methods.
I also cannot recommend you modify generated files (especially if it's generated from a tool you're using all the time). If you must insist on making the control static, declare it in your source file for the class, not the designer-generated file, the classes are declared partial for a reason.
You're probably trying to share some bool value that the CheckBox represents. Make that a static property.
public partial class MyForm : Form
{
public static bool IsToggled { get; set; }
}
If somewhere down the line you want to tie that to an event or whatever, you could always change the implementation of the accessors.

Hmm... without more context, I'd suggest to use a public boolean property for this.
Having a UI item be static is not a good idea.
Try something like this:
public class MyForm : Form
{
public static bool IsWhateverSet {get; private set;}
}

Related

Using ref in (whole) partial class

I got some confusion about "the right way" to use a reference in a partial class.
Basically i wrote a WPF program which has different Menus. Every Menu got the same Viewmodel and some data-related object class. In my case i call the Object "DataModel" which i want to use as reference in every menu. I just came across a problem when ich switched my DataModel from a static object to the desired instance for every Menu as input ref. (i still want to use one and the same DataModel for every menu though...)
But in the "lower" methods it says that _dm is simply not defined.
Code shortly summarized as:
public partial class FormatWPF : UserControl
{
public FormatWPF(DataModel _dm)
{
InitializeComponent();
if (this.DataContext == null)
{
this.DataContext = _dm.g1.MVM;
}
}
// here come several Methods with which i want to calculate stuff and "manipulate" the DataModel
private void Steinformat_berechnen()
{
_dm.g1.FormatNr = _dm.g1.FormatAnzahl + 1;
}
//....
}
Shortly said i want to use the _dm which is given as input ref in the Constructor of the class object for every other method in the whole partial class as well (is it really necessary to define this ref for every method ?) Using the DataModel as static seemed so easy for me.... but basically it is "wrong" ?
Thanks in advance for some help and tips about doing it the right way.
Maybe i was a little bit unclear. The thing is i want to use just one DataModel for all the menu and my whole project. Nevertheless i dont want to make it as static ( there occured some other confusion in later parts of my code... ) So basically i have to give in the DataModel as ref for all the Menus...
Concerning your answer: I know the possibility to define another
private Datamodel _dm;
in the namespace..
But im not quite sure about:
1)won't i got here some additional "memory" usage by defining another DataModel for every menu ? becuase it is somehow "big"
2)when i now calculate data in the _dm, will it change for the "complete" program ? like in the former static Model ?
I hope to make the DataModel static then is not the "right answer" to my problem because i just wanted to get away from this somehow ... hm
Best regards
Knally
Yes, static is very wrong if the DataModel is a per-instance thing (static means all the instances would be using the same value); but it can still be an instance field:
private DataModel _dm;
public FormatWPF(DataModel dataModel)
{
_dm = dataModel;
// the rest of your constructor code here
}
Now you can use _dm in all of your other instance methods, and everything should be fine. If you only ever need _dm.g1, you could perhaps store that value as the field, instead of the model itself.

How to fix "inaccessible due to its protection level" error in winform?

I have a Form class
partial class ProgressMainForm : Form
{
public ProgressMainForm()
{
InitializeComponent();
}
}
And then a class that uses that class and contains all functionality for the user
public class ProgressForm
{
public ProgressMainForm myProgressForm;
public ProgressForm(string title)
{
myProgressForm = new ProgressMainForm();
myProgressForm.Text = title;
}
public void SetProgressBar(int min, int max)
{
....
}
I then use this ProgressForm class in my project like this
progresswindow = new ProgressForm("Replacing All Strings");
This way progresswindow only contains members that are related to the functionality of the ProgressForm and all those Form members are hidden from the user.
But sometimes I need to access those Form members, for example when I need Invoke method.
Is there a way to make myProgressForm in ProgressForm accessible to users without making ProgressMainForm public?
Or is this approach wrong?
In my opinion you should not work with the form directly. If I read your setup correctly, you want to show progress indicator while some job is being done. ProgressForm should expose methods to set the counters and increment them; as you run it on another thread, form manipulation should be done from inside the methods of ProgressForm. Your Invokes belong there, wrapped in suitable methods. If you want to change some visual properties of ProgressMainForm relay those properties to ProgressForm.
To resume, calling code should have no clue what ProgressForm does other than setting progress boundaries, starting, setting current percentage and stopping. This way, if you are asked to port the application to another UI system the amount of code you will need to change will be drastically reduced.
Is there a way to make myProgressForm in ProgressForm accessible to users without making ProgressMainForm public?
Yes, you can create some public properties on ProgressForm that expose specific properties of ProgressMainForm.
private ProgressMainForm myProgressForm;
public int SomeProperty
{
get { return myProgressForm.IntProp; }
set { myProgressForm.IntProp = value; }
}
For readonly properties, omit the set, and for any types that are reference types, you may want to return a clone or copy (to ensure the client can't change it).
Wrap or Expose the Methods you need. But somehow i don't like the approach, restricting the access is not a bad idea but should not be the whole purpose of this kind of abstraction. Try to make the acess easier, not restrictive.
You can declare the methods as internal , This will allow you to call the methods from within the assembly.

Changing a form object's property from a separate class

I'm very new to using C#. If you have ever heard of the Karol the robot program that is written in Java then that's what I'm aiming to build.
But I am stumbling at almost the first hurdle, I want to make a class for Karol (It is just a picture) that can move around the screen in blocks of 32x32 squares.
Trouble is when you create a class you can't access the properties of form objects because they are separate things.
I would like to be able to manipulate form objects from my class but without having to pass the object through the method each time I use it.
Any help is much appreciated.
Do not need to pass a reference of the Form every time. Should be enough to do something like this:
`public class Karol
{
private Form _form=null;
public Karol(Form frm) {
_form = frm;
}
// after use _form inside the functions and properties of the class, where needed
}`
edit
to acces a control data inside a Form it needs to expose or controls itself, like
public Label MyFormLabel {....}
Or expose a functions/properties that sets or gets the data from the control.
public string MyFormLabelText { get{ return label.Text;} set{label.Text = value;}}
If it's just one form object that "Karol" is manipulating, you should be able to pass it to the constructor and save it for use in later member calls. That way you only pass it once at construction time.

Creating a helper class for ballontip that can be accessed from any wpf page

I have a wpf application that is using a NavigationWindow that allows me to load pages within the MasterWindow.
I am trying to add the notifyicon feature http://www.codeproject.com/KB/WPF/wpf_notifyicon.aspx to my application that will allow me to display a notification window when I want.
I have added the taskbaritem to my navigationwindow
<tb:TaskbarIcon x:Name="MyNotifyIcon" ToolTipText="My Application" IconSource="/Images/Icons/TB.ico"/>
I want to be able to create a helper class that can access MyNotifyIcon from any of the pages loaded. I was thinking something like
public static void DisplayMessageArea(string messageToDisplay)
{
var balloon = new StandardNotification {BalloonMessage = messageToDisplay };
//TaskbarIcon tb = (TaskbarIcon)MasterWindow.Resources.FindName("MyNotifyIcon");
//tb.ShowCustomBalloon(balloon,PopupAnimation.Slide,4000);
}
However I don't know the correct way to find the controller "MyNotifyIcon" from my helper class.
Can someone point me in the right direction?
I've never seen that NotifyIcon before (kinda cool, by the way), but I took a look at its API. What I would suggest is that you not bother naming that control and trying to find it in your code. Instead, in your main navigation window, set its datacontext to a helper class that you define, and bind to its iconsource, tooltiptext, etc:
<tb:TaskbarIcon DataContext="{Binding SomeViewModel}"
IconSource="..."
ToolTipText="{Binding Text}"
Visibility="{Binding IsVisible, Converter=...}" />
(More about value converters here).
Now, the "SomeViewModel" class will implement INotifyPropertyChanged and expose properties like:
bool IsVisible { get; set; }
string Text { get; set; }
...except that these properties will have actual getters/setters and raise the property changed event.
With this approach, you don't need to tell your presentation layer to go looking in your View layer, which isn't really ideal. It's better if your view (XAML) knows about your presentation (code), but not vice-versa, as this loosens coupling. In this case, it loosens coupling by virtue of the fact that your actual code does not depend on the existence of some named control declared in XAML.
As to how to get at this helper view model, you can pass a reference to it around to the various classes that can set it, or you can have the sub-controls of the navigation window raise events that the nav window listens for, or you can go with the approach that you had in mind, which is to define a static method on the view model (I'm not an advocate of this approach, personally, but it's the closest to what you're looking to do).
If you're set on the approach that you're taking here, however, keep in mind that you do have it named in that control, so you can re-expose it as a static property on the control in question:
class MyControlWithTipIcon
{
public static TaskbarIcon TaskBarIcon { get { return MyNotifyIcon; } }
}
Now, you can access it from your helper class:
public static void DisplayMessageArea(string messageToDisplay)
{
MyControlWithTipIcon.TaskBarIcon.ToolTipText = messageToDisplay;
MyControlWithTipIcon.TaskBarIcon.Visibility = ... //i.e. show the message
}
I would not personally advocate that approach at all, but it is probably the easiest way to do, specifically, what you're asking. If you go this route, however, remember to check the TaskBarIcon static property for null before doing anything with it, and keep in mind that the static property will work return a value whether or not your control containing it has been loaded or even instantiated.

Accessing a listview box

I am pretty new to programming in c#. I have a listview (ListViewProjects) box in ProjectListForm but I want to be able to access the items in the listview box in AddProjectForm. How can I access them? I was going to try to do something like this
ProjectListForm.ListViewProjects.Items.Clear
but when I type in ProjectListForm., ListViewProjects isn't an option.
You can either set the list view's design time options to generate a public member (in control's properties, under Design set Modifiers to Public), or possibly better you could add a public property to ProjectListForm to expose either the list view or just the Items collection, eg:
public class ProjectListForm : Form
{
...
public ListView.ListViewItemCollection ProjectsListViewItems
{
get { return ListViewProjects.Items; }
}
}
The best way IMO is to write public methods in ProjectListForm that do what you're trying to achieve in AddProjectForm, such as ProjectListForm.ClearProjects(). That way you're not forcing AddProjectForm to depend on the implementation of ProjectListForm, say if you later want to change to a TreeView.
You cannot achieve this way in C#. you need to create the instance of the object.
To do this you have to create the method in ProjectListForm and in that methos you have to call this.ListViewProjects.Items.Clear.
Now from the AddProjectForm you have to call this method of the running instance of ProjectListForm.
If you want to know more about how to make the call, then please give the flow about how do you show the AddProjectForm from the ProjectListForm.

Categories

Resources