Invalidate UserControl Error in C# - c#

I created an UserControl and everything works great except for the properties. The problem is I lost all the Design Property in my UserControl.. to be more clear, i made a screenshot.
This is my UserControl
..and a ToolStrip sample
this is my sample code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using System.ComponentModel.Design;
namespace _Sample__User_Control
{
[Designer("Sytem.Windows.Forms.Design.ParentControlDesigner,System.Design", typeof(IDesigner))]
public partial class ClientsCollectionListBox : UserControl
{
public ClientsCollectionListBox()
{
InitializeComponent();
}
[Browsable(true)]
[DefaultValue("Hello")]
public string MyString { get; set; }
}
}
my User Control doesn't have the Design Property. Upon dragging the User Control to my form, there's still the Design Property but upon running or adding another instance to this control, i lost the display. Any help for this problem would be appreciated. Do i need to Invalidate on load?
SOLUTION:
Spelling error.. sorry.. please close this thread..
[Designer("Sytem.Windows.Forms.Design.ParentControlDesigner,System.Design", typeof(IDesigner))]
[Designer("System.Windows.Forms.Design.ParentControlDesigner,System.Design", typeof(IDesigner))]

Related

How can I easily replace one control for another with the same name in Windows Forms?

The first problem is that in form1 top I have:
ListViewNF lvnf;
The ListViewNF is a class in form1:
class ListViewNF : System.Windows.Forms.ListView
{
public ListViewNF()
{
//Activate double buffering
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
//Enable the OnNotifyMessage event so we get a chance to filter out
// Windows messages before they get to the form's WndProc
this.SetStyle(ControlStyles.EnableNotifyMessage, true);
}
protected override void OnNotifyMessage(System.Windows.Forms.Message m)
{
if (m.Msg != 0x14)
{
base.OnNotifyMessage(m);
}
}
}
Then in the form1 constructor I'm making instance for the lvnf variable set the size, location and add this control lvnf to the form1 controls.
Then in my program I'm using the lvnf in some places.
But now I want to make a new UserControl call it lvnf so when I drag the UserControl from the toolbox to the form1 designer it will replace the lvnf I created.
The first problem is once I delete the line:
ListViewNF lvnf;
I'm getting many errors and can't see the form1 designer I'm getting an error since lvnf not exist.
If I don't delete it and drag the new UserControl it will give the new control another name and I want it to be lvnf. I don't want to change anything in my run time code related to the lvnf only the control in the designer.
This is the code of the UserControl:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Pop3_Emails
{
public partial class ListViewNF : UserControl
{
public ListViewNF()
{
InitializeComponent();
}
}
}
Should I make all the events of the lvnf to move them to the UserControl code? Or leave them all on form1?
The main problem is how to use the UserControl as lvnf.
Also I need to use the code in the class ListViewNF.
What a mess. In general what I want to do is to make the lvnf the ListViewNF a control in the form1 designer instead a control in the run time code.
Drag the new UserControl onto the form, under a different name (x for example)
Rename the lvnf to x, asking Visual Studio to update all references. Ignore the warning that the name already exists, and the project won't compile.
Remove the ListViewNF x declaration (now the project should compile again).
Rename x back to lvnf, also updating all references.

How to communicate between ViewModels in WPF and how to control Views lifecycle

There are three windows MainWindow, FirstWindow and SecondWindow. MainWindow can open FirstWindow and SecondWindow.
Now my question is:
How to open SecondWindow from FirstWindow, and close FirstWindow when the SecondWindow open. At this time, I can control SecondWindow but can't control MainWindow, just like using SecondWindow.ShowDialog() from MainWindow.
After I click the "save" button on SecondWindow, the SecondWindow shall be closed and the DataGrid of MainWindow shall be updated. How to update data from another ViewModel or how to return data when event was handled?
You are asking multiple for multiple things here.
Basically you need 2 things. An event aggregator (also called messenger) to pass messages between view models. There are different frameworks that implement it or they come as part of MVVM Frameworks.
Second you need is a navigation service to decouple navigation from your view models, as navigation requires knowledge of the view related technology (WPF, UWP, Silverlight etc.)
I'm agree with Tseng's answer and will try to extend his answer.
First part
For low-coupled communication between modules (not only ViewModels) we can try to implement EventAggregator pattern. Event aggregator helps to implement subscriber/publisher pattern in low-coupled app. I know few different implementations.
First one based on CodeProject post and uses WeakReference that will help you to prevent memory leak. I will not publish whole code because you can just download source code and use it. In this implementation you must to implement ISubscriber interface for your subscribers.
The second is Microsoft Prism implementation. This is an open source project then you can see interface, implementation and base event class. In this implementation you must unsubscribe from the event manually.
The third and the last is MVVMLight library and its Messenger class.
As you can see all of this implementations uses Singleton pattern for saving subscribers.
Second part
Second part is about navigation. The simpliest way is to use Page navigation infrastructure. But in MVVM-world we have many different concepts for navigation.
The main purpose to use navigation abstractions is to separate navigation logic from concrete view rendering technology (WPF, Silverlight, WinRT, Xamarin).
For example, in Microsoft Prism we can use regions and RegionManager for navigation between views and windows. It's very ponderous navigation framework and it can be difficult to understand the concept after only one article.
MVVM Light also have their own navigation mechanism.
For my projects I use my own realization of navigation via Workspaces. It is a hybrid mechanism that combines Page navigation principes from .net and Regions concept from Prism.
Conclusions
This post is not an answer to your questions. But I hope that it will be helpful to you to understanding MVVM concepts.
As you can read above there are many MVVM-frameworks which contains infrastructure (not only Messenger and NavigationService, but also base command realisations, PopupService, converters, INotifyPropertyChanged-helpers and base ViewModel implementations) to implement typical scenarios in your application.
You need to use an instance of the form class to pass data. See my simple two form project below
Form 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form2 form2;
public Form1()
{
InitializeComponent();
form2 = new Form2(this);
}
private void button1_Click(object sender, EventArgs e)
{
form2.Show();
string results = form2.GetData();
}
}
}
​
Form 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
Form1 form1;
public Form2(Form1 nform1)
{
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
form1 = nform1;
form1.Hide();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//stops form from closing
e.Cancel = true;
this.Hide();
}
public string GetData()
{
return "The quick brown fox jumped over the lazy dog";
}
}
}
​

Strange browser issue

I have a strange browser issue regarding my windows forms app.
I have placed a web browser in my form and I'm simply trying to load google.
The problem is that the browser is auto refreshing it every few seconds and it makes it useless.
I have used:
webBrowser1.Url = new Uri("http://google.com");
and
webBrowser1.Navigate("http://google.com");
And the result is the same. The page is still auto refreshing. It's the first time I'm facing this issue. Is there anyone who faced it and can help me?
I'm running the code on Visual Studio 2012 - windows 7 x64
EDIT:
Here is the code of the form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Navigate("http://google.com");
}
}
}
I can't see how you're wiring things up, but to me it looks like you're wiring WebBrowser.DocumentCompleted to navigate to Google. The problem is;
The WebBrowser.DocumentCompleted event occurs when the WebBrowser control finishes loading a document.
In other words, every time you get an event that the page has finished loading, you're calling webBrowser1.Navigate("http://google.com"); which reloads it again.

Inherited control will not display in designer

Trying to create a simple custom control in C# Winforms that inherits from Panel, but as soon as I change it to inherit from "Panel" instead of "UserControl" I get this error:
Here's the code for the entire class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SETPaint
{
public partial class Canvas : Panel
{
public Canvas()
{
InitializeComponent();
}
private void Canvas_Load(object sender, EventArgs e)
{
}
}
}
Delete the line 'this.AutoScaleDimensions = ...' in your Designer.cs file (line 35 according to the exception). There's probably another similar to 'this.AutoScaleMode = .Font' too.
This problem arises because you've used the Designer when the Control derived from UserControl, and it set some default properties in the InitializeComponent() method. Those properties are part of the UserControl base type, but not the Panel base type.
Since the IDE Designer can't load the Designer.cs file to fix this problem, you need to do it manually.
Your code should be like this
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SETPaint
{
public class Canvas : Panel
{
}
}
And on the toolbox there should be an item called Canvas after you build the solution

MessageBox.Show() fonts

Is there a way I can change the font types in a MessageBox.Show() to get bigger size, bold, italic styles?
You can always make your own MessageBox creating a new Windows.Forms class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MessageBoxFont
{
public partial class Message : Form
{
public Message(String text)
{
InitializeComponent();
tbxMessage.Text = text;
btnOK.Focus();
}
private void btnOK_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Then you can control the properties (like the font, size, color and the like) shown under the solution explorer. You initialize this form like this:
private void OpenMessageBox()
{
String text = "This is a sample error message";
Message message = new Message(text);
message.Show();
}
Its a work-around, however, easier to implement :)
I believe that those fonts are controlled by the operating system.
You could (however) make a custom dialog and put anything you want in there including custom fonts.
Here is the MSDN resource for custom dialogs.
http://msdn.microsoft.com/en-us/library/2chz8edb(VS.90).aspx
Have you thought of something like a customized message box (www.html-messagebox.com)?
For more customization such as building an irregular shaped message box (Homer Simpson's head), you are better off creating your own MessageBox-like implementation for your project.
Check this http://www.windowsdevelop.com/windows-forms-general/change-font-size-for-messageboxshow-dialogs-62092.shtml

Categories