How to only show month on MonthCalendar title header? c# - c#

I need to show only the Month name on the MonthCalendar header title.
I looked at the MonthCalendar methods but there's no method for either customizing header or changing header title. Only the font colors could be changed.

From comments, a way by using System.Windows.Controls.Calendar
Screen copies (french)
Test code (C#/Winforms/VS 2015 on Windows 10)
(I added the references to be added in comments)
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Controls.Calendar calendar1;
private void Form1_Load(object sender, EventArgs e)
{
// Add reference to PresentationFramework
calendar1 = new System.Windows.Controls.Calendar();
// Test Background
// Add reference to PresentationCore
// Add reference to WindowsBase
System.Windows.Media.LinearGradientBrush linearGradientBrush = new System.Windows.Media.LinearGradientBrush(System.Windows.Media.Colors.Cyan, System.Windows.Media.Colors.Blue, new System.Windows.Point(0, 0), new System.Windows.Point(0, 1));
calendar1.Background = linearGradientBrush;
// Container for Calendar
System.Windows.Controls.Canvas canvas = new System.Windows.Controls.Canvas();
System.Windows.Controls.Viewbox viewbox1 = new System.Windows.Controls.Viewbox();
viewbox1.StretchDirection = System.Windows.Controls.StretchDirection.Both;
viewbox1.Stretch = System.Windows.Media.Stretch.Fill;
viewbox1.MaxWidth = 260;
viewbox1.MaxHeight = 260;
viewbox1.Child = calendar1;
canvas.Children.Add(viewbox1);
// Test Event
calendar1.SelectedDatesChanged += calendar1_SelectedDatesChanged;
textBox1 = new System.Windows.Forms.TextBox();
textBox1.Location = new System.Drawing.Point(10, 280);
textBox1.Name = "textBox1";
textBox1.Size = new System.Drawing.Size(149, 20);
Controls.Add(textBox1);
// Test new Culture
// using System.Globalization;
CultureInfo cultureinfo = (CultureInfo.CurrentCulture.Clone() as CultureInfo);
cultureinfo.DateTimeFormat.YearMonthPattern = #"yyyy - MMMM";
//cultureinfo.DateTimeFormat.YearMonthPattern = #"MMMM";
System.Threading.Thread.CurrentThread.CurrentCulture = cultureinfo;
// Host for controls
// Add reference to WindowsFormsIntegration
// using System.Windows.Forms.Integration;
var elementHost = new ElementHost();
elementHost.Child = canvas;
Controls.Add(elementHost);
elementHost.Location = new System.Drawing.Point(10, 10);
elementHost.Size = new System.Drawing.Size(300, 300);
ClientSize = new System.Drawing.Size(280, 340);
CenterToScreen();
}
private void calendar1_SelectedDatesChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
textBox1.Text = calendar1.SelectedDate.ToString();
}

Related

Validator new textbox c#

How to create a validator from the textbox class or create new textbox, rather than dragging the textbox from the toolbox
private void Form1_Load(object sender, EventArgs e)
{
createTextpass();
// Creating and setting the properties of TextBox1
TextBox textboxUsername = new TextBox();
textboxUsername.Location = new Point(420, 50);
textboxUsername.Size = new Size (300,30);
textboxUsername.Name = "text_user";
this.Controls.Add(textboxUsername);
TextBox textboxPassword = new TextBox();
textboxPassword.Location = new Point(420, 80);
textboxPassword.Size = new Size(300, 30);
textboxPassword.Name = "text_pass";
this.Controls.Add(textboxPassword);
TextBox textboxMail = new TextBox();
textboxMail.Location = new Point(420, 110);
textboxMail.Size = new Size(300, 30);
textboxMail.Name = "text_mail";
this.Controls.Add(textboxMail);
}
Add the texbox width and height
If I understand your question correctly. You want to create a textbox and check whether valid text is entered in this textbox or not. For example, the following code can be used for textboxUsername:
public TextBox textboxUsername;
private void Form1_Load(object sender, EventArgs e)
{
createTextpass();
// Creating and setting the properties of TextBox1
textboxUsername = new TextBox();
textboxUsername.Location = new Point(420, 50);
textboxUsername.Size = new Size(300, 30);
textboxUsername.Name = "text_user";
this.Controls.Add(textboxUsername);
textboxUsername.TextChanged += new EventHandler(usernameTextbox_TextChanged);
....
}
//If textboxUsername contains the # character, an alarm will be displayed
protected void usernameTextbox_TextChanged(object sender, EventArgs e)
{
if (textboxUsername.Text.Contains('#'))
MessageBox.Show("inavlid char in userName");
}

Generate a New Guid into a Textbox area using a button. (C#)

Looking for help.
I have a form which generates fields into a Class extends groupbox.
I am creating a button to clear and then generate a new GUID into a text box but I can seem to access my textbox which has been creating in the Initialize method.
I created a list to store input.
private List<InputSetItem> _inputSetItems = new List<InputSetItem>();
This is where the textbox is created:
public void initialize()
{
//balanceIdentifier
var balanceIdentifierSet = InputGenerator.GenerateInputControl(this, typeof(Guid), "BalanceIdentifier");
Controls.Add(balanceIdentifierSet.Label);
Controls.Add(balanceIdentifierSet.Input);
balanceIdentifierSet.Label.Left = 820;
balanceIdentifierSet.Input.Left = 1050;
balanceIdentifierSet.Input.Width = 400;
balanceIdentifierSet.Label.Top = 20;
balanceIdentifierSet.Input.Top = 20;
_inputSetItems.Add(balanceIdentifierSet);
// btn_Guid
this.btn_Guid.BackColor = System.Drawing.Color.Blue;
this.btn_Guid.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point);
this.btn_Guid.ForeColor = System.Drawing.Color.White;
this.btn_Guid.Location = new System.Drawing.Point(1455, 19);
this.btn_Guid.Name = "btn_Guid";
this.btn_Guid.Size = new System.Drawing.Size(75, 26);
this.btn_Guid.TabIndex = 3;
this.btn_Guid.Text = "GENERATE";
this.btn_Guid.UseVisualStyleBackColor = false;
this.btn_Guid.ImageAlign = ContentAlignment.MiddleRight;
this.btn_Guid.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
this.btn_Guid.FlatStyle = FlatStyle.Flat;
this.Controls.Add(btn_Guid);
this.btn_Guid.Click += new System.EventHandler(this.generateRandomGuid);
}
This is the UI
This is my Generate Method, I cant wrap my head around accesses and updating this textBox
private void generateRandomGuid(object sender, EventArgs e)
{
var delBal = new InputSetItem();
Guid randomGuid = Guid.NewGuid();
//delBal.btn_Guid.Input = randomGuid.ToString();
//delBal.Input.Text;
}
So figured it out.
I had to create a Control instances
private Control _idemp;
Add the textbox assigning it
var IdempotencyRef = InputGenerator.GenerateInputControl(this, typeof(Guid), "IdempotencyReference");
_idemp = IdempotencyRef.Input;
Controls.Add(IdempotencyRef.Label);
Controls.Add(_idemp);
And then updating the method
private void generateRandomGuid(object sender, EventArgs e)
{
Guid randomGuid = Guid.NewGuid();
_idemp.Text = randomGuid.ToString();
}

Use WPF expander in Winforms with more than one control

My problem is that I want to use WPF expander object to host some winforms control. And the position that I'm going to use this is in my application's setting form. But, what I couldn't find is to add more than one control to it.
After a lot of searching for solution to my problem I just found this simple code that only add one control to the WPF expander object (I require more than one control to be added):
private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Controls.Expander expander = new System.Windows.Controls.Expander();
expander.Header = "Sample";
WPFHost = new ElementHost();
WPFHost.Dock = DockStyle.Fill;
WindowsFormsHost host = new WindowsFormsHost();
host.Child = new DateTimePicker();
expander.Content = host;
WPFHost.Child = expander;
this.Controls.Add(WPFHost);
}
In this code the expander only hosts one control.
How should I customize it to host more than one control ?
Please help
Using a System.Windows.Forms.Panel as a container will help:
private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Controls.Expander expander = new System.Windows.Controls.Expander();
System.Windows.Controls.Grid grid = new System.Windows.Controls.Grid();
expander.Header = "Sample";
ElementHost WPFHost = new ElementHost();
WPFHost.Dock = DockStyle.Fill;
Panel panel1 = new Panel();
DateTimePicker dtPicker1 = new DateTimePicker();
Label label1 = new Label();
// Initialize the Label and TextBox controls.
label1.Location = new System.Drawing.Point(16, 16);
label1.Text = "Select a date:";
label1.Size = new System.Drawing.Size(104, 16);
dtPicker1.Location = new System.Drawing.Point(16, 32);
dtPicker1.Text = "";
dtPicker1.Size = new System.Drawing.Size(152, 20);
// Add the Panel control to the form.
this.Controls.Add(panel1);
// Add the Label and TextBox controls to the Panel.
panel1.Controls.Add(label1);
panel1.Controls.Add(dtPicker1);
WindowsFormsHost host = new WindowsFormsHost();
host.Child = panel1;
expander.Content = host;
WPFHost.Child = expander;
this.Controls.Add(WPFHost);
}

How do I write a winform-based activeX control for use on a web page?

I created an activeX control using C# language. Now I need to add UI (some text boxes, and labels) to my activeX control. How can I do that? I tried next code, but I haf no success with it (I got this code from msdn.microsoft.com).
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.CompilerServices;
using System.Data;
using System.Linq;
namespace AxControls
{
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("42BBA00A-515E-45b5-9EAF-3827F7AEB4FA")]
[ProgId("AxControls.HelloWorld")]
[ComDefaultInterface(typeof(IClip))]
public class HelloWorld : UserControl
{
// Create the controls.
private System.Windows.Forms.ErrorProvider errorProvider1;
private System.Windows.Forms.TextBox textName;
private System.Windows.Forms.TextBox textAddress;
private System.Windows.Forms.TextBox textCity;
private System.Windows.Forms.TextBox textStateProvince;
private System.Windows.Forms.TextBox textPostal;
private System.Windows.Forms.TextBox textCountryRegion;
private System.Windows.Forms.TextBox textEmail;
private System.Windows.Forms.Label labelName;
private System.Windows.Forms.Label labelAddress;
private System.Windows.Forms.Label labelCityStateProvincePostal;
private System.Windows.Forms.Label labelCountryRegion;
private System.Windows.Forms.Label labelEmail;
private System.ComponentModel.IContainer components;
// Define the constructor.
public HelloWorld()
{
InitializeComponent();
}
// Initialize the control elements.
public void InitializeComponent()
{
// Initialize the controls.
components = new System.ComponentModel.Container();
errorProvider1 = new System.Windows.Forms.ErrorProvider();
textName = new System.Windows.Forms.TextBox();
textAddress = new System.Windows.Forms.TextBox();
textCity = new System.Windows.Forms.TextBox();
textStateProvince = new System.Windows.Forms.TextBox();
textPostal = new System.Windows.Forms.TextBox();
textCountryRegion = new System.Windows.Forms.TextBox();
textEmail = new System.Windows.Forms.TextBox();
labelName = new System.Windows.Forms.Label();
labelAddress = new System.Windows.Forms.Label();
labelCityStateProvincePostal = new System.Windows.Forms.Label();
labelCountryRegion = new System.Windows.Forms.Label();
labelEmail = new System.Windows.Forms.Label();
// Set the tab order, text alignment, size, and location of the controls.
textName.Location = new System.Drawing.Point(120, 8);
textName.Size = new System.Drawing.Size(232, 20);
textName.TabIndex = 0;
textAddress.Location = new System.Drawing.Point(120, 32);
textAddress.Size = new System.Drawing.Size(232, 20);
textAddress.TabIndex = 1;
textCity.Location = new System.Drawing.Point(120, 56);
textCity.Size = new System.Drawing.Size(96, 20);
textCity.TabIndex = 2;
textStateProvince.Location = new System.Drawing.Point(216, 56);
textStateProvince.Size = new System.Drawing.Size(56, 20);
textStateProvince.TabIndex = 3;
textPostal.Location = new System.Drawing.Point(272, 56);
textPostal.Size = new System.Drawing.Size(80, 20);
textPostal.TabIndex = 4;
textCountryRegion.Location = new System.Drawing.Point(120, 80);
textCountryRegion.Size = new System.Drawing.Size(232, 20);
textCountryRegion.TabIndex = 5;
textEmail.Location = new System.Drawing.Point(120, 104);
textEmail.Size = new System.Drawing.Size(232, 20);
textEmail.TabIndex = 6;
labelName.Location = new System.Drawing.Point(8, 8);
labelName.Size = new System.Drawing.Size(112, 23);
labelName.Text = "Name:";
labelName.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
labelAddress.Location = new System.Drawing.Point(8, 32);
labelAddress.Size = new System.Drawing.Size(112, 23);
labelAddress.Text = "Address:";
labelAddress.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
labelCityStateProvincePostal.Location = new System.Drawing.Point(8, 56);
labelCityStateProvincePostal.Size = new System.Drawing.Size(112, 23);
labelCityStateProvincePostal.Text = "City, St/Prov. Postal:";
labelCityStateProvincePostal.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
labelCountryRegion.Location = new System.Drawing.Point(8, 80);
labelCountryRegion.Size = new System.Drawing.Size(112, 23);
labelCountryRegion.Text = "Country/Region:";
labelCountryRegion.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
labelEmail.Location = new System.Drawing.Point(8, 104);
labelEmail.Size = new System.Drawing.Size(112, 23);
labelEmail.Text = "email:";
labelEmail.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
// Add the Validating and Validated handlers for textEmail.
textEmail.Validating += new System.ComponentModel.CancelEventHandler(textEmail_Validating);
textEmail.Validated += new System.EventHandler(textEmail_Validated);
// Add the controls to the user control.
Controls.AddRange(new System.Windows.Forms.Control[]
{
labelName,
labelAddress,
labelCityStateProvincePostal,
labelCountryRegion,
labelEmail,
textName,
textAddress,
textCity,
textStateProvince,
textPostal,
textCountryRegion,
textEmail
});
// Size the user control.
Size = new System.Drawing.Size(375, 150);
}
private void MyValidatingCode()
{
// Confirm there is text in the control.
if (textEmail.Text.Length == 0)
{
throw new Exception("Email address is a required field.");
}
// Confirm that there is a "." and an "#" in the e-mail address.
else if(textEmail.Text.IndexOf(".") == -1 || textEmail.Text.IndexOf("#") == -1)
{
throw new Exception("Email address must be valid e-mail address format." +
"\nFor example: 'someone#example.com'");
}
}
// Validate the data input by the user into textEmail.
private void textEmail_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
MyValidatingCode();
}
catch(Exception ex)
{
// Cancel the event and select the text to be corrected by the user.
e.Cancel = true;
textEmail.Select(0, textEmail.Text.Length);
// Set the ErrorProvider error with the text to display.
this.errorProvider1.SetError(textEmail,ex.Message);
}
}
private void textEmail_Validated(Object sender, System.EventArgs e)
{
//If all conditions have been met, clear the error provider of errors.
errorProvider1.SetError(textEmail, "");
}
}
}
and this is how I use the activeX control in HTML page
<html>
<head>
</head>
<body>
<object name='Hello' style='display: none' id='Hello' classid='CLSID:42BBA00A-515E-45b5-9EAF-3827F7AEB4FA'
codebase='AxControls.cab#version=1,0,0,0' width="100%" height="100%">
</object>
</body>
</html>
I know for sure that the activeX is registered and executed well, because when I add something like
MessageBox.Show("HELLO");
to the code of my activeX control I can see this message while calling my html page. Anyway I can see no UI at all.
Any suggestions would be great.
It was a really bad idea to use style='display: none' in <object> tag :)

Iterating controls on button click in c# windows application

I am trying to create a windows application where I want to display a group of controls (Combo Box, Text Box and a button) on button click inside a panel.
I have created a code to create controls once but I want to create them again and again on button click one below another.
The code I am using is
public partial class Employee_PayHeads_add : Form
{
private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ComboBox combohead = new ComboBox();
public Employee_PayHeads_add()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.btnAdd.BackColor = Color.Gray;
this.btnAdd.Text = "Remove";
this.btnAdd.Location = new System.Drawing.Point(240, 25);
this.btnAdd.Size = new System.Drawing.Size(70, 25);
this.txtBox.Text = "";
this.txtBox.Location = new System.Drawing.Point(150, 25);
this.txtBox.Size = new System.Drawing.Size(70, 40);
this.combohead.Location = new System.Drawing.Point(10, 25);
panel1.Controls.Add(btnAdd);
panel1.Controls.Add(txtBox);
panel1.Controls.Add(combohead);
}
Also I want a vertical scroller in the panel if number controls overlap the space.
Thanks in advance
Inside the button click event create new objects, instead of using the one you declared before.
Try something like that:
public partial class Employee_PayHeads_add : Form
{
private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ComboBox combohead = new ComboBox();
private int txtBoxStartPosition = 150;
private int btnAddStartPosition = 240;
private int comboheadStartPosition = 10;
}
public Employee_PayHeads_add()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TextBox newTxtBox = new TextBox();
Button newBtnAdd = new Button();
ComboBox newCombohead = new ComboBox();
newBtnAdd.BackColor = Color.Gray;
newBtnAdd.Text = "Remove";
newBtnAdd.Location = new System.Drawing.Point(btnAddStartPosition, 25);
newBtnAdd.Size = new System.Drawing.Size(70, 25);
newTxtBox.Text = "";
newTxtBox.Location = new System.Drawing.Point(txtBoxStartPosition, 25);
newTxtBox.Size = new System.Drawing.Size(70, 40);
newCombohead.Location = new System.Drawing.Point(comboheadStartPosition, 25);
panel1.Controls.Add(newBtnAdd);
panel1.Controls.Add(newTxtBox);
panel1.Controls.Add(newCombohead);
txtBoxStartPosition += 50;
btnAddStartPosition += 50;
comboheadStartPosition += 50;
}
I havent tried your code yet, but it shows that it is always creating the new controls on every click event, but as youo have specified the hardcoaded location for buttons, so it must be creating new controls overlapping each other. so you can change the location dynamically and hopefully it will work
If you want to add the controls again and again you have to create new ones. So rather than defining them in your form like that you have to:
private void button1_Click(object sender, EventArgs e)
{
Button btnAdd = new Button();
btnAdd.BackColor = Color.Gray;
btnAdd.Text = "Remove";
btnAdd.Location = new System.Drawing.Point(240, 25);
btnAdd.Size = new System.Drawing.Size(70, 25);
TextBox txtBox = new TextBox();
txtBox.Text = "";
txtBox.Location = new System.Drawing.Point(150, 25);
txtBox.Size = new System.Drawing.Size(70, 40);
ComboBox combohead = new ComboBox();
combohead.Location = new System.Drawing.Point(10, 25);
panel1.Controls.Add(btnAdd);
panel1.Controls.Add(txtBox);
panel1.Controls.Add(combohead);
}
Now you can remove those private declarations on top of your class.

Categories

Resources