I've a WPF program which it's MainWindows.xaml.cs has many functions and events. I want to make it more readable so I moved some of the functions and events in a separate class named "partialClassMainWindow.cs". I moved all button events to it. now when I click on a button it must go to button_Click function in partialClassMainWindow but a new funciton is created in MainWindows.xaml.cs.
how can I prevent it?
for example consider this simple program:
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//moved to partialClassMainWindow:
//private void button1_Click(object sender, RoutedEventArgs e)
//{
//}
}
}
partialClassMainWindow.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace WpfApplication3
{
public partial class MainWindow:Window
{
public void button1_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "Hello World";
}
}
}
Don't think that you can avoid it. It looks like Visual studio just checks the code behind file and not any other files with more partial code. But the other guys are right MVVM is the way to go and normally you have little or none code behind. Only typical view stuff you would handle there, all the business logic goes into the viewmodel.
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
Is a good place to start reading about MVVM.
Related
overflow, im having trouble trying to add a ui framework , for exmple metro framework, to my c# form.
I know that i have to include the reference and include it with
using MetroFramework.Forms;
but im not sure what else to do here and when ever i try and google it, i just get websites that try and sell me a framework package.
Here is my code:
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;
using MetroFramework.Forms;
namespace FrameworkTest
{
public partial class Form1
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
I know this may be a silly question but any help would be much appreciated.
Thanks to Crowcoder I found out that I was supposed to add
MetroForm after Form1 like:
public partial class Form1 : MetroForm {
//Code goes here
}
In the code below, the line 'QuickToolBar.AddChild(SaveBtn)' gives the following error:
System.Windows.Controls.ItemsControl.AddChild(object) is inaccessible
due to its protection level.
I don't understand the reason for this error message. Please help
using System;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Ribbon;
namespace Ribbon3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CreateRibbon();
}
public void CreateRibbon()
{
Ribbon ribbon = new Ribbon();
ribbon.SelectedIndex = 0;
RibbonQuickAccessToolBar QuickToolBar = new RibbonQuickAccessToolBar();
RibbonButton SaveBtn = new RibbonButton();
QuickToolBar.AddChild(SaveBtn);
}
}
}
The method is protected:
https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.addchild(v=vs.110).aspx
A protected member of a base class is accessible in a derived class only if the access occurs through the derived class type.
https://msdn.microsoft.com/en-us/library/bcd5672a.aspx
As said by Derek you cannot use QuickToolBar.AddChild(SaveBtn) due to protection level. But you still can add using Items.Add like this
QuickToolBar.Items.Add(SaveBtn);
I'm trying to create a new form (child form) that inherits from another form (parent form), and the problem is that, when I try to view the child form design view (in order to modify some controls), Visual Studio throws the following error: "Unable to load DLL 'fmodex': the specified module could not be found inherited form. (Exception from HRESULT: 0x8007007E)". The strange thing is that it actually works fine when I run the program, the error is only shown when I try to enter to the design view of the child form. I've checked the dll path and it's correct. Here's part of the the parent's code:
using System;
using System.Collections;
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
{
protected EventList eventList;
protected FMOD.System system;
protected int count;
public Form1()
{
InitializeComponent();
//Create a FMOD System object and initialize.
FMOD.RESULT result;
result = FMOD.Factory.System_Create(ref system);
result = system.init(32, FMOD.INITFLAGS.NORMAL, (IntPtr)null);
//Event list
eventList = new EventList(0,system);
count = 0;
}
}
}
And the child's code:
using System;
using System.Collections;
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 : WindowsFormsApplication1.Form1
{
public Form2()
{
InitializeComponent();
}
}
}
Does anybody knows what could be happening? I can't understand why I can see the design view of the parent form and not the child form one.
Thanks in advance.
I just want to set the background image of another form to a png file picture resource I added
I have the following in my MAstrategyBldrForm.cs file:
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 MAStrategyBuilder
{
public partial class MAstrategyBldrForm : Form
{
private CrossPicsForm crossPic;
public MAstrategyBldrForm()
{
InitializeComponent();
crossPic = new CrossPicsForm();
}
....
....
.... later in same file:
private void MAcrossPicButton_Click(object sender, EventArgs e)
{
crossPic.BackgroundImage = (Bitmap) (Properties.Resources.ResourceManager.GetObject("Moving Avgs Cross.png"));
crossPic.Show();
}
....
....
In my CrossPicsForm.cs file I have:
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 MAStrategyBuilder
{
public partial class CrossPicsForm : Form
{
public CrossPicsForm()
{
InitializeComponent();
}
}
}
Everything compiles but when I click my button control in the mastrategy... form, a CrossPicsForm window is displayed that is blank. Darn!
You won't get an exception when you mis-spell the resource name, ResourceManager simply returns null and that leaves the BackgroundImage property unchanged. You did, it would normally be named "Moving_Avgs_Cross" without the .png extension.
Do favor using the property that was added by the resource designer instead. That ought to resemble:
crossPic.BackgroundImage = Properties.Resources.Moving_Avgs_Cross;
Use IntelliSense to help you fall in the pit of success here.
I have a simple window application I am starting and I was given another file I would like to bring into the new project. How do I bring them both together to work as one file?
New Project
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Other
using System;
using System.Text;
using System.IO;
using System.Net;
namespace CaptchaClient
{
public static class SampleUse
{
public static void SampleSolve()
{
}
}
public class CaptchaSolver
{
}
public enum ResponseState
{
}
}
This screenshots shows the how yo do he procedure
Right click on your project in the Solution Explorer and click Add Existing Item. Then browse to the other class file. Once imported, you might want to change its namespace to the one you are using in your project.