I am trying to print a variable with a specified font, but the string is null so nothing is visible in the output. please go through the code and help me to find the error
class BasicClass
{
public string str;
public Font fnt;
}
class BasicMethod:BasicClass
{
public void changevalues(string newstr,Font newfnt)
{
str = newstr;
fnt = newfnt;
}
}
class PrintClass:BasicClass
{
public void print()
{
PrintDialog pd = new PrintDialog();
PrintDocument pdoc = new PrintDocument();
PrinterSettings ps = new PrinterSettings();
PaperSize psize = new PaperSize();
pdoc.DefaultPageSettings.Landscape = true;
pd.Document = pdoc;
pd.Document.DefaultPageSettings.PaperSize = psize;
pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);
DialogResult result = pd.ShowDialog();
if (result == DialogResult.OK)
{
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = pdoc;
ppd.PrintPreviewControl.Zoom = 1.5;
((Form)ppd).WindowState = FormWindowState.Maximized;
DialogResult ppdResult = ppd.ShowDialog();
}
}
void pdoc_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
//string str1 = "XYZ";
//Font fnt1 = new Font("Arial", 12.5f);
g.DrawString(str, fnt, new SolidBrush(Color.Black), 10, 10);
}
}
Button Click event
private void button1_Click(object sender, EventArgs e)
{
BasicMethod bm = new BasicMethod();
PrintClass pc = new PrintClass();
Font ft = new System.Drawing.Font("Arial", 12.5f);
bm.changevalues("Hello", ft);
pc.print();
}
i need to get the output Hello
You are setting the values you want in a completely different object from the one where you try to use them.
One way to fix this:
Change the PrintClass so that it inherits the BasicMethod class instead of BasicClass
class PrintClass : BasicMethod
Then change your click handler:
private void button1_Click(object sender, EventArgs e)
{
PrintClass pc = new PrintClass();
Font ft = new System.Drawing.Font("Arial", 12.5f);
pc.changevalues("Hello", ft);
pc.print();
}
Related
Сannot update form butReset_Click does not work RestoreLayoutFromRegistry from another form. How to do it right? I created one form from another and it is necessary to reset the pivotGridControl settings from 2 forms. I can't reset the settings.
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
butReset.Click += new System.EventHandler(butReset_Click);
pivotGridControl1.SaveLayoutToRegistry(regKey);
}
string regKey = "DevExpress\\XtraPivotGrid\\Layouts\\PivotGridLayout";
public Button butLoad = new Button();
public Button butReset = new Button();
public void LoadBtn_Click(object sender, EventArgs e)
{
Form1 fr = new Form1();
Form form = new Form();
form.Show();
ListBox listBox1 = new ListBox();
listBox1.Size = new System.Drawing.Size(200, 100);
listBox1.Location = new System.Drawing.Point(10, 10);
form.Controls.Add(listBox1);
listBox1.MultiColumn = true;
listBox1.SelectionMode = SelectionMode.MultiExtended;
DirectoryInfo dir = new DirectoryInfo(#"E:\");
FileInfo[] files = dir.GetFiles("*.txt");
butReset.Text = "Сбросить настройки";
butReset.Location = new Point(140, 160);
form.Controls.Add(butReset);
butLoad.Text = "Принять";
butLoad.Location = new Point(30, 160);
form.Controls.Add(butLoad);
var fileNamesWithoutExtension = files
.Select(fi => Path.GetFileNameWithoutExtension(fi.Name));
foreach (string fn in fileNamesWithoutExtension)
{
listBox1.Items.Add(fn.ToString());
}
}
private void SaveBtn_Click(object sender, EventArgs e)
{
pivotGridControl1.RestoreLayoutFromRegistry(regKey);
}
static public void butReset_Click(object sender, EventArgs e)
{
Form1 er = new Form1();
er.pivotGridControl1.RestoreLayoutFromRegistry(er.regKey);
//MessageBox.Show("dsdsdfsdf");
}
}
}
Use PivotGridOptionsDataField.Reset() method to Resets all options to their default values. In your case
static public void butReset_Click(object sender, EventArgs e)
{
pivotGridControl1.Reset();
}
Reference document: https://docs.devexpress.com/CoreLibraries/DevExpress.XtraPivotGrid.PivotGridOptionsDataField.Reset
I am trying to make an application launcher and in the end i am trying to pass and extra argument to RoutedEventHandler method call but its giving me an error, i tried looking it up on google but i didn't understand a damn thing. (i am noob) so here is the code please have a look and it.Thanks
namespace Button_Launcher
{
public partial class MainWindow : Window
{
private string line;
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog fileName = new OpenFileDialog();
fileName.Filter = "executables (.exe)|*.exe";
fileName.ShowDialog();
StreamWriter writer = new StreamWriter("E:/Config.txt",true);
writer.WriteLine(fileName.FileName);
writer.Close();
textBox.Text = fileName.FileName;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
string file = ("E:/Config.txt");
StreamReader Reader = new StreamReader(file, Encoding.ASCII);
List<string> appList = new List<string>();
List<string> Items = new List<string>();
int count = 1;
while ((line = Reader.ReadLine()) != null)
{
makeButton(line,count);
count++;
}
Reader.Close();
}
public void makeButton(string link,int count)
{
string[] mySplit = link.Split(new char[] { '\\' });
string[] name = mySplit.Last().Split(new char[] { '.' });
string fName = name.First();
string buttonName = fName;
Button newBtn = new Button();
newBtn.Content = buttonName;
newBtn.Height = 30;
newBtn.Width = 70;
newBtn.Margin = new Thickness(-60, 90*count/1.5, 180,80);
StackPanel sp = new StackPanel();
sp.Children.Add(newBtn);
mainGrid.Children.Add(sp);
newBtn.Click += new RoutedEventHandler(launchApp(link));
//launchApp(link);
}
private void launchApp(string link,object sender, RoutedEventArgs e)
{
Process.Start(link);
}
}
}
You cannot change the method signature of the RoutedEventHandler. Instead, try storing the link data in the Tag property of your button. Then in the method you could cast the sender object to a button and access the tag property.
public void makeButton(string link,int count)
{
string[] mySplit = link.Split(new char[] { '\\' });
string[] name = mySplit.Last().Split(new char[] { '.' });
string fName = name.First();
string buttonName = fName;
Button newBtn = new Button();
newBtn.Content = buttonName;
newBtn.Height = 30;
newBtn.Width = 70;
newBtn.Tag = link;
newBtn.Margin = new Thickness(-60, 90*count/1.5, 180,80);
StackPanel sp = new StackPanel();
sp.Children.Add(newBtn);
mainGrid.Children.Add(sp);
newBtn.Click += launchApp;
//launchApp(link);
}
private void launchApp(object sender, RoutedEventArgs e)
{
var btn = sender as Button;
if( btn == null )
return;
Process.Start(btn.Tag.ToString( ));
}
I seem to have a problem with two lines of code in my program.
A ThreadStateException occurs at the lines if (o.ShowDialog() == DialogResult.OK) and if(s.ShowDialog() == DialogResult.OK)
The program is supposed to interpret a made up language, but that part of code has not been made yet. Please help, I have no idea what to do!
public class meow : Form
{
TextBox meowbox = new TextBox();
private string titile;
public meow()
{
titile="Tiger goes Meow";
Size = new Size(500, 600);
Text =titile ;
meowbox.Size = new Size(450, 520);
meowbox.Multiline = true;
meowbox.ScrollBars = ScrollBars.Horizontal;
meowbox.WordWrap = true;
meowbox.Location = new Point(25, 10);
//file
MenuItem feow = new MenuItem("File Meow");
MenuItem oeow = new MenuItem("open Meow");
MenuItem seow = new MenuItem("Save Meow");
feow.MenuItems.Add(oeow);
feow.MenuItems.Add(seow);
//run
MenuItem leow = new MenuItem("Meow");
MenuItem ceow = new MenuItem("Check Meow");
MenuItem reow = new MenuItem("Run Meow");
leow.MenuItems.Add(ceow);
leow.MenuItems.Add(reow);
//menu
MainMenu beow = new MainMenu();
Menu = beow;
beow.MenuItems.Add(feow);
beow.MenuItems.Add(leow);
//put it all meow
Controls.Add(meowbox);
//handlers
oeow.Click += new EventHandler(oeow_Click);
seow.Click += new EventHandler(seow_Click);
/*ceow.Click += new EventHandler(ceow_Click);
reow.Click += new EventHandler(reow_Click);*/
}
protected void oeow_Click( object sender, EventArgs e){
Text="Oeow";
OpenFileDialog o = new OpenFileDialog();
if (o.ShowDialog() == DialogResult.OK)
{
Stream file = o.OpenFile();
StreamReader reader = new StreamReader(file);
char[] data = new char[file.Length];
reader.ReadBlock(data, 0, (int)file.Length);
meowbox.Text = new String(data);
reader.Close();
}
Text = titile;
}
protected void seow_Click(object sender, EventArgs e)
{
Text="seow";
SaveFileDialog s = new SaveFileDialog();
if(s.ShowDialog() == DialogResult.OK)
{
StreamWriter writer = new StreamWriter(s.OpenFile());
writer.Write(meowbox.Text);
writer.Close();
}
Text=titile;
}
public static void Main()
{
Application.Run(new meow());
}
}
If the code you posted is your real program, then the problem is that you aren't setting the thread's apartment state correctly. The main Winforms UI thread must be a single-threaded apartment thread.
Try:
[STAThread]
public static void Main()
{
Application.Run(new meow());
}
Note that ideally, you should just create your Winforms project using the template built into Visual Studio. It will configure the thread correctly for you.
I am trying to make a button that when clicked pops up a new form with a yes and no button and then make an if statement based on what button is pressed. Here is my current code:
private YesNoMessageBoxResized newBoxResized;
private string buttonClickResult;
public void YesNoNewMessageBox(string title, string message,string buttonYes, string buttonNo)
{
YesNoMessageBoxResized msgResized = new YesNoMessageBoxResized(title, message, buttonYes, buttonNo);
msgResized.StartPosition = FormStartPosition.CenterScreen;
msgResized.TopMost = true;
Button yesButtonResize = new Button();
Button noButtonResize = new Button();
//yes button
yesButtonResize.Text = buttonYes;
yesButtonResize.Size = new Size(150, 80);
yesButtonResize.Font = new Font("Arial", 26);
yesButtonResize.Location = new Point(100, 150);
//no button
noButtonResize.Text = buttonNo;
noButtonResize.Size = new Size(150, 80);
noButtonResize.Font = new Font("Arial", 26);
noButtonResize.Location = new Point(300, 150);
//make a copy of the current form
newBoxResized = msgResized;
//eventhandlers
yesButtonResize.Click += YesButtonResizeClicked;
noButtonResize.Click += noButtonResizeClicked;
newBoxResized.Controls.Add(yesButtonResize);
newBoxResized.Controls.Add(noButtonResize);
msgResized.Show();
}
private void YesButtonResizeClicked(object o, EventArgs sEA)
{
this.buttonClickResult = "true";
this.newBoxResized.Close();
}
private void noButtonResizeClicked(object o, EventArgs sEA)
{
this.buttonClickResult = "false";
this.newBoxResized.Close();
}
private void buttonRestoreDefaults_Click(object sender, EventArgs e)
{
YesNoNewMessageBox("Restore Defaults?", "Restore Defaults?", "Yes", "No");
if (this.buttonClickResult == "true")
this.restoreDefaults();
}
My problem is that after hitting yes and closing the form that pops up, buttonClickResult is not seen as true and therefore the restore default function I am calling is not called. Only when clicking on the "RestoreDefaults" button again is the function called. So, it seems that the onclick event for buttonRestoreDefaults_Click isn't reconizing the onclick for the yes or no buttons in the form that popups until clicking on it again. Is there a way around this or some sort of implementation to fix this? Thank you.
Also, here is the code for the class. I was thinking about using delegates and event handlers, but I am not sure if I actually need that since what I have works, but just doesn't update the variable on closing correctly:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class YesNoMessageBoxResized : Form
{
private Label labelMessage;
//no default button specified
public YesNoMessageBoxResized(string title, string message, string buttonYes, string buttonNo)
{
InitializeComponent();
this.Text = title;
this.labelMessage.Text = message;
this.Deactivate += MyDeactivateHandler;
}
public YesNoMessageBoxResized()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.labelMessage = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// labelMessage
//
this.labelMessage.AutoSize = true;
this.labelMessage.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.labelMessage.Location = new System.Drawing.Point(12, 31);
this.labelMessage.Name = "labelMessage";
this.labelMessage.Size = new System.Drawing.Size(165, 29);
this.labelMessage.TabIndex = 3;
this.labelMessage.Text = "labelMessage";
//
// YesNoMessageBoxResized
//
this.ClientSize = new System.Drawing.Size(572, 268);
this.Controls.Add(this.labelMessage);
this.Name = "YesNoMessageBoxResized";
this.ResumeLayout(false);
this.PerformLayout();
}
protected void MyDeactivateHandler(object sender, EventArgs e)
{
this.Close();
}
public delegate void buttonYes_ClickResultEvent(object o, EventArgs sEA);
public event buttonYes_ClickResultEvent choiceResult;
}
public class buttonYes_ClickResultEvent : EventArgs
{
public buttonYes_ClickResultEvent(bool choice)
{
this.buttonResult = choice;
}
public bool buttonResult;
}
**I posted this on codereview, but then they told me to post it here since it deals with solving a problem.
just for a test, try using a global variable and set that to true within your yesbuttonresizedclicked event.
something like
public static bool yestest = false;
private void YesButtonResizeClicked(object o, EventArgs sEA)
{
yestest = true;
}
I sort of fixed it by adding a function that had the if statement withing the other button events. I am open to other solutions though:
private YesNoMessageBoxResized newBoxResized;
private string buttonClickResult;
public void YesNoNewMessageBox(string title, string message,string buttonYes, string buttonNo)
{
YesNoMessageBoxResized msgResized = new YesNoMessageBoxResized(title, message, buttonYes, buttonNo);
msgResized.StartPosition = FormStartPosition.CenterScreen;
msgResized.TopMost = true;
Button yesButtonResize = new Button();
Button noButtonResize = new Button();
//yes button
yesButtonResize.Text = buttonYes;
yesButtonResize.Size = new Size(150, 80);
yesButtonResize.Font = new Font("Arial", 26);
yesButtonResize.Location = new Point(100, 150);
//no button
noButtonResize.Text = buttonNo;
noButtonResize.Size = new Size(150, 80);
noButtonResize.Font = new Font("Arial", 26);
noButtonResize.Location = new Point(300, 150);
//make a copy of the current form
newBoxResized = msgResized;
//eventhandlers
yesButtonResize.Click += YesButtonResizeClicked;
noButtonResize.Click += noButtonResizeClicked;
newBoxResized.Controls.Add(yesButtonResize);
newBoxResized.Controls.Add(noButtonResize);
newBoxResized.Show();
}
private void YesButtonResizeClicked(object o, EventArgs sEA)
{
this.buttonClickResult = "true";
this.DoIfStatement();
this.newBoxResized.Close();
}
private void noButtonResizeClicked(object o, EventArgs sEA)
{
this.buttonClickResult = "false";
this.DoIfStatement();
this.newBoxResized.Close();
}
private void DoIfStatement()
{
if (buttonClickResult == "true")
this.restoreDefaults();
}
private void buttonRestoreDefaults_Click(object sender, EventArgs e)
{
YesNoNewMessageBox("Restore Defaults?", "Restore Defaults?", "Yes", "No");
}
I think I need it to be more modular though. I don't always want the same if statement there. So, I don't always want to be a yes and no with regards to restore defaults. I may want it to do something else with another function call and have yes and no buttons do something else.
You can just use MessageBox to do this for you:
if (MessageBox.Show("Yes or no?", "", MessageBoxButtons.YesNo)
== DialogResult.Yes)
You could change the form to a modal dialog form. Make the Accept and Cancel buttons properties equal the appropriate button. Then change the dialog result of each button to the appropriate value. Now when you use the ShowDialog the form will return the appropriate DialogResult.
I have this class,
public class ImageBox : Grid
{
Image imgclose; public String path;
List<ImageBox> ImageBoxes;
public ImageBox(string label,List<ImageBox> ImBox)
{
this.ImageBoxes = ImBox;
imgclose = new Image();
imgclose.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("pack://application:,,,/Close.ico"));
imgclose.Width = 20; imgclose.Height = 20; imgclose.Cursor = Cursors.Hand; imgclose.HorizontalAlignment = System.Windows.HorizontalAlignment.Right; imgclose.VerticalAlignment = System.Windows.VerticalAlignment.Top;
imgclose.Visibility = System.Windows.Visibility.Hidden;
imgclose.MouseLeftButtonDown += new MouseButtonEventHandler(imgclose_MouseLeftButtonDown);
this.MouseEnter += new MouseEventHandler(Blank_MouseEnter);
this.MouseLeave += new MouseEventHandler(Blank_MouseLeave);
this.Height = 100; this.Width = 100;
try
{
System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog();
path = open.FileName.Replace(Directory.GetCurrentDirectory(), "");
this.Background = new System.Windows.Media.ImageBrush(new System.Windows.Media.Imaging.BitmapImage(new Uri(label)));
path = label;
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
Grid.SetColumn(imgclose, 0); Grid.SetRow(imgclose, 1);
this.Children.Add(imgclose);
ContextMenu contextMenu1 = new ContextMenu();
MenuItem conitem1 = new MenuItem() { Header = "Send to back" }; conitem1.Click += new System.Windows.RoutedEventHandler(conitem1_Click);
MenuItem conitem2 = new MenuItem() { Header = "Bring to Front" }; conitem2.Click += new System.Windows.RoutedEventHandler(conitem2_Click);
contextMenu1.Items.Add(conitem1); contextMenu1.Items.Add(conitem2);
this.ContextMenu = contextMenu1;
}
void conitem1_Click(object sender, EventArgs e)
{
Canvas.SetZIndex(this, (Canvas.GetZIndex(this) - 1));
}
void conitem2_Click(object sender, EventArgs e)
{
Canvas.SetZIndex(this, (Canvas.GetZIndex(this) + 1));
}
void Blank_MouseEnter(object sender, MouseEventArgs e)
{
imgclose.Visibility = System.Windows.Visibility.Visible;
}
void Blank_MouseLeave(object sender, MouseEventArgs e)
{
imgclose.Visibility = System.Windows.Visibility.Hidden;
}
void imgclose_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (System.Windows.MessageBox.Show("Are you sure?", "Confirm", System.Windows.MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.Question) == System.Windows.MessageBoxResult.OK)
{
ImageBoxes.Remove(this);
(this.Parent as System.Windows.Controls.Canvas).Children.Remove(this);
}
else
{
}
}
}
This class displays an image (chosen from a dialog box).
How can I modify it to make it play a video file ?
More precisely,
How should I modify the line
this.Background = new System.Windows.Media.ImageBrush(new System.Windows.Media.Imaging.BitmapImage(new Uri(label)));
path = label;
so that it plays a video file.
The Background property has to have some sort of Brush in it. A quick Google search came up with this: VideoBrush. I hope this is what you were looking for.