I tried to make a simple document using FastReport. So I started with placing a button on the form and writing the following code in order to run it when the button is clicked:
private void button1_Click(object sender, EventArgs e)
{
Report report = new Report();
ReportPage page1 = new ReportPage();
page1.Name = "Page1";
report.Pages.Add(page1);
page1.ReportTitle = new ReportTitleBand();
page1.ReportTitle.Name = "ReportTitle1";
TextObject text1 = new TextObject();
text1.Name = "Text1";
text1.Text = "REPORT TITLE TEXT";
text1.HorzAlign = HorzAlign.Center;
text1.Font = new Font("Tahoma", 14, FontStyle.Bold);
page1.ReportTitle.Objects.Add(text1);
report.Show();
}
Unfortunately, when I ran the application and I pressed the button, a blank page was shown, without any text. What is wrong with the code? Does it lack some element?
You should set width and height for text object:
text1.AutoWidth = true;
text1.Height = 100;
Related
Okay so basically I have a calendar display and when you click on anyone of the dates on it, it creates a new panel with a label displaying the date selected. I also made it so when you click on a date and a new panel is made, a label, textbox and button is created and placed onto that new panel as well.
So what I want and have been struggling with is for me to enter something into that textbox then to press the button to submit it and then for it to show on the label.
I think I know what the issue is but I've been stuck at this for hours.
Here is my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void monthCalendar1_DateSelected_1(object sender, DateRangeEventArgs e)
{
Panel newPanel = new Panel();
this.Controls.Add(newPanel);
newPanel.Visible = true;
newPanel.Size = new Size(564, 831);
newPanel.Location = new Point(0, 190);
newPanel.BringToFront();
Label textLabel = new Label();
textLabel.Size = new Size(500, 500);
textLabel.Font = new Font(textLabel.Font.Name, 25, textLabel.Font.Style);
textLabel.Location = new Point(3, 3);
Label dateLabel = new Label();
dateLabel.Size = new Size(500, 500);
dateLabel.Font = new Font(dateLabel.Font.Name, 25, dateLabel.Font.Style);
dateLabel.Location = new Point(128, 3);
Button Submitbutton = new Button();
Submitbutton.Location = new Point(100, 500);
Submitbutton.Text = "Add Food";
Submitbutton.Size = new Size(400, 100);
Submitbutton.BackColor = Color.Aqua;
Submitbutton.BringToFront();
Submitbutton.Click += Button_Click;
TextBox textBox = new TextBox();
textBox.Location = new Point(100, 650);
textBox.Size = new Size(500, 500);
textBox.BackColor = Color.Aqua;
textBox.Visible = true;
textBox.Text = "Enter food here...";
textBox.BringToFront();
Label inputtedFood = new Label();
inputtedFood.Size = new Size(500, 500);
inputtedFood.Font = new Font(inputtedFood.Font.Name, 25, inputtedFood.Font.Style);
inputtedFood.Location = new Point(100, 600);
inputtedFood.Text = "placeholder";
newPanel.Controls.Add(dateLabel);
newPanel.Controls.Add(textLabel);
newPanel.Controls.Add(Submitbutton);
newPanel.Controls.Add(textBox);
newPanel.Controls.Add(inputtedFood);
String myCalendar = monthCalendar1.SelectionRange.Start.ToShortDateString();
textLabel.Text = "Date:";
dateLabel.Text = myCalendar;
}
private void Button_Click(object sender, EventArgs e)
{
inputtedFood.Text = textBox.Text;
}
private void monthCalendar1_DateChanged_1(object sender, DateRangeEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
I tried the above code and was met with errors that are shown in the post.
Totally agree with both LarsTech and Ňɏssa Pøngjǣrdenlarp, you should be building a UserControl in place of the Panel and placing the TextBox, Button, and Label inside of that.
Your immediate question, though:
So what I want and have been struggling with is for me to enter
something into that textbox then to press the button to submit it and
then for it to show on the label.
Can be accomplished with this simple code:
Button Submitbutton = new Button();
// ... more code ...
Submitbutton.Click += (s2, e2) =>
{
inputtedFood.Text = textBox.Text;
};
Here's a little example showing it in action:
private void button1_Click(object sender, EventArgs e)
{
FlowLayoutPanel flp = new FlowLayoutPanel();
TextBox textBox = new TextBox();
// ... more code ...
Label inputtedFood = new Label();
inputtedFood.Text = "placeholder";
// ... more code ...
Button Submitbutton = new Button();
// ... more code ...
Submitbutton.Click += (s2, e2) =>
{
inputtedFood.Text = textBox.Text;
};
flp.Controls.Add(textBox);
flp.Controls.Add(Submitbutton);
flp.Controls.Add(inputtedFood);
flowLayoutPanel1.Controls.Add(flp);
}
The output:
I would like a label to be generated every time a button is clicked on my UserControl - below is the method called whenever the button is clicked:
private void button1_Click(object sender, EventArgs e)
{
}
I would be grateful for a code snippet inside the method that will generate a label with my choice of text, location and tooltip.
You can do it easily in windows forms application. Following code snippet only generates a label at specific location with hardcoded text and some basic properties settings. You can modify it according to your requirement e.g. you can change label name, location, text accordingly. Hope this will be helpful for you.
private void button1_Click(object sender, EventArgs e)
{
var lblnew = new Label
{
Location = new Point(50, 50),
Text = "My Label", //Text can be dynamically assigned e.g From some text box
AutoSize = true,
BackColor = Color.LightGray,
Font = new Font("Microsoft JhengHei UI", 9.75F, FontStyle.Regular, GraphicsUnit.Point, (byte)0)
};
//this refers to current form you can use your container according to requirement
Controls.Add(lblnew);
}
Or you can also use simplified initialization as follows;
private void button1_Click(object sender, EventArgs e)
{
var lblnew = new Label
{
Location = new Point(50, 50),
Text = "My Label", //Text can be dynamically assigned e.g From some text box
AutoSize = true,
BackColor = Color.LightGray,
Font = new Font("Microsoft JhengHei UI", 9.75F, FontStyle.Regular, GraphicsUnit.Point, (byte)0)
};
//this refers to current form you can use your container according to requirement
Controls.Add(lblnew);
}
I'd like to use a stackpanel with two textblock that have two uris, inside a ContentDialog. The problem is that despite I set the property to Vertical, it has no effect and that's the visual result
private async void AboutButton_Click(object sender, RoutedEventArgs e)
{
TextBlock gHRepoTB = new TextBlock();
Hyperlink hyperlink1 = new Hyperlink();
Run run1 = new Run();
run1.Text = "View GitHub repository";
hyperlink1.NavigateUri = new Uri("https://www.google.com/");
hyperlink1.Inlines.Add(run1);
gHRepoTB.Inlines.Add(hyperlink1);
TextBlock privacyPolicyTB = new TextBlock();
Hyperlink hyperlink2 = new Hyperlink();
Run run2 = new Run();
run2.Text = "Privacy Policy";
hyperlink2.NavigateUri = new Uri("https://www.bing.com/");
hyperlink2.Inlines.Add(run2);
gHRepoTB.Inlines.Add(hyperlink2);
StackPanel aboutPanel = new StackPanel();
aboutPanel.Orientation = Orientation.Vertical;
aboutPanel.Children.Add(gHRepoTB);
aboutPanel.Children.Add(privacyPolicyTB);
ContentDialog aboutDialog = new ContentDialog();
aboutDialog.Title = "About";
aboutDialog.Content = aboutPanel;
aboutDialog.PrimaryButtonText = "Report a bug";
aboutDialog.PrimaryButtonClick += ReportBug_Click;
aboutDialog.PrimaryButtonStyle = App.Current.Resources["AccentButtonStyle"] as Style;
aboutDialog.CloseButtonText = "Close";
await aboutDialog.ShowAsync();
}
#mm8 showed how to fix your problem, however this is the result of unnecessary and unclear code.
Here are the things I changed:
I've changed a button with hyperlink layout to a hyperlinkbutton.
I've set the contents to just a string since in this case no more is needed.
Instead of manually setting a style to the primarybutton, I've specified the DefaultButton
By creating the dialog this way, you are less prone to making mistakes, others and yourself later on will be able to tell faster what's going on, and less temporary variables are introduced.
The code (You should be able to copy this code directly over your current eventhandler):
private async void AboutButton_Click(object sender, RoutedEventArgs e)
{
StackPanel aboutPanel = new StackPanel();
aboutPanel.Children.Add(
new HyperlinkButton
{
Content = "View GitHub repository",
NavigateUri = new Uri("https://www.google.com/")
});
aboutPanel.Children.Add(
new HyperlinkButton
{
Content = "Privacy Policy",
NavigateUri = new Uri("https://www.bing.com/")
});
var dlg = new ContentDialog
{
Title = "About",
Content = aboutPanel,
PrimaryButtonText = "Report a bug",
DefaultButton = ContentDialogButton.Primary,
CloseButtonText = "Close"
};
dlg.PrimaryButtonClick += ReportBug_Click;
await dlg.ShowAsync();
}
See the result below:
You add the second Hyperlink to the wrong TextBlock. It should be privacyPolicyTB.Inlines.Add(hyperlink2);:
TextBlock privacyPolicyTB = new TextBlock();
Hyperlink hyperlink2 = new Hyperlink();
Run run2 = new Run();
run2.Text = "Privacy Policy";
hyperlink2.NavigateUri = new Uri("https://www.bing.com/");
hyperlink2.Inlines.Add(run2);
privacyPolicyTB.Inlines.Add(hyperlink2); //<-- here
I created some Hyperlinks using the code below:
public Class_List()
{
InitializeComponent();
StackPanel myStackPanel = new StackPanel();
TextBlock txt = new TextBlock();
txt.Foreground = Brushes.Black;
txt.FontFamily = new FontFamily("03SmartFontUI");
txt.FontSize = 25;
txt.Margin = new Thickness(0, 5, 0, 5);
Run run = new Run(className);
Hyperlink link = new Hyperlink(run);
link.Click += Link_Click;
txt.Inlines.Add(link);
}
Now, I want to get the text of the hyperlink and store it on string s:
private void Link_Click(object sender, RoutedEventArgs e)
{
string s = (sender as Hyperlink).Inlines.ToString();
Class_Page class_page = new Class_Page();
NavigationService.Navigate(class_page);
}
However instead of the hyperlink text, I got
System.Windows.Documents.InlineCollection
You're getting that type because you are actually accessing the entire collection of Inlines rather than the Inline you're looking for. The fastest way to access the Run's text you're using as the first Inline in the Hyperlink's InlineCollection is to do this:
((sender as Hyperlink).Inlines.FirstInline as Run).Text;
Im trying to write this simple winform menu and I need to add the contents of the NBox text box to a string so I can display it when a button is pressed, however I keep getting the error that NBox does not exist in the current context. So, how would i got about making the contents of the text box available at the press of a button?
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
//namespace game{
class MainM : Form{
public MainM(){
Text = "Adventures Main Menu";
Size = new Size(400,400);
//NameBox
TextBox NBox = new TextBox();
NBox.Location = new Point(145, 100);
NBox.Size = new Size(200, 30);
//Title Label
Label title = new Label();
title.Text = "ADVENTURE THE GAME";
title.Location = new Point(145, 30);
title.Size = new Size(200,60);
title.Font = new Font(defaultFont.FontFamily, defaultFont.Size, FontStyle.Bold);
//The main menu Buttons and all that jazz
Button credits = new Button();
Button start = new Button();
//Credits Button
credits.Text = "Credits";
credits.Size = new Size(75,20);
credits.Location = new Point(145,275);
credits.Click += new EventHandler(this.credits_button_click);
//Start Button
start.Text = "Start";
start.Size = new Size(75,20);
start.Location = new Point(145,200);
start.Click += new EventHandler(this.start_button_click);
//Control addition
this.Controls.Add(title);
this.Controls.Add(credits);
this.Controls.Add(start);
this.Controls.Add(NBox);
}
public void test(){
//The Main Window
}
private void credits_button_click(object sender, EventArgs e){
MessageBox.Show("Created by: Me");
}
private void start_button_click(object sender, EventArgs e){
this.Hide();
string name = NBox.Text;
MessageBox.Show(name);
//Process.Start("TextGame.exe");
}
public static void Main(){
Application.Run(new MainM());
}
}
//}
First, you need to name the control, that name will be its key in container's Controls collection :
//NameBox
TextBox NBox = new TextBox();
NBox.Location = new Point(145, 100);
NBox.Size = new Size(200, 30);
NBox.Name = "NBox"; //Naming the control
Then you will be able to retrieve it from the container:
private void start_button_click(object sender, EventArgs e){
this.Hide();
TextBox NBox= (TextBox)Controls.Find("NBox", true)[0];//Retrieve controls by name
string name = NBox.Text;
MessageBox.Show(name);
//Process.Start("TextGame.exe");
}
You declared NBox in consturctor and it is visible only inside constructor. You need to move it outside of constructor.
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
//namespace game{
class MainM : Form{
TextBox NBox;
public MainM(){
Text = "Adventures Main Menu";
Size = new Size(400,400);
//NameBox
NBox = new TextBox();
...