I have a issue : I have a main form who plays music when loaded : and I made a function who plays a sound when a button is pressed : the next form is minimized and their content too (from config.cs to every form), it's supposed to be at fullscreen and I want to keep it fullscreen and not minimized when playing a sound.
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 System.Media;
using System.IO;
using System.Runtime.InteropServices;
namespace Poker
{
public partial class Main : Form
{
[DllImport("wmp.dll")]
static extern Int32 mciSendString(string command, StringBuilder buffer, int bufferSize, IntPtr hwndCallback);
//LoadForm lform = new LoadForm();
Config config = new Config();
Settings settings = new Settings();
public Main()
{
InitializeComponent();
}
public void Main_Load(object sender, EventArgs e)
{
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
new System.Threading.Thread(() =>
{
try
{
player1.PlayLooping();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,
"Impossible de jouer le morceau.",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}).Start();
}
private void btnJouer_Click_1(object sender, EventArgs e)
{
btnClick();
this.Hide();
config.Show();
}
Sound function :
void btnClick()
{
new System.Threading.Thread(() =>
{
try
{
var _bt = new System.Windows.Media.MediaPlayer();
_bt.Open(new System.Uri(Environment.CurrentDirectory + "\\sfx\\button.wav"));
_bt.Play();
}
catch(Exception xe)
{
MessageBox.Show(xe.Message,
"Impossible de jouer ce son.",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}).Start();
}
Main form code :
namespace Poker
{
partial class Main
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Main));
this.titre = new System.Windows.Forms.TableLayoutPanel();
this.label1 = new System.Windows.Forms.Label();
this.btnQuitter = new System.Windows.Forms.Button();
this.btnJouer = new System.Windows.Forms.Button();
this.btnPara = new System.Windows.Forms.Button();
this.titre.SuspendLayout();
this.SuspendLayout();
//
// titre
//
this.titre.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.titre.AutoSize = true;
this.titre.BackColor = System.Drawing.Color.Transparent;
this.titre.ColumnCount = 1;
this.titre.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.titre.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.titre.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.titre.Controls.Add(this.label1, 0, 0);
this.titre.Location = new System.Drawing.Point(0, 0);
this.titre.Name = "titre";
this.titre.RowCount = 1;
this.titre.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.titre.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.titre.Size = new System.Drawing.Size(1920, 300);
this.titre.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Segoe UI Semibold", 36F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.ForeColor = System.Drawing.Color.White;
this.label1.Location = new System.Drawing.Point(3, 0);
this.label1.MinimumSize = new System.Drawing.Size(100, 100);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(225, 100);
this.label1.TabIndex = 0;
this.label1.Text = "Poker";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btnQuitter
//
this.btnQuitter.BackColor = System.Drawing.Color.Transparent;
this.btnQuitter.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnQuitter.Font = new System.Drawing.Font("Segoe UI", 16F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnQuitter.ForeColor = System.Drawing.Color.White;
this.btnQuitter.Location = new System.Drawing.Point(258, 754);
this.btnQuitter.Name = "btnQuitter";
this.btnQuitter.Size = new System.Drawing.Size(1361, 95);
this.btnQuitter.TabIndex = 3;
this.btnQuitter.Text = "Quitter";
this.btnQuitter.UseVisualStyleBackColor = false;
this.btnQuitter.Click += new System.EventHandler(this.btnQuitter_Click);
//
// btnJouer
//
this.btnJouer.BackColor = System.Drawing.Color.Transparent;
this.btnJouer.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnJouer.Font = new System.Drawing.Font("Segoe UI", 16F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnJouer.ForeColor = System.Drawing.Color.White;
this.btnJouer.Location = new System.Drawing.Point(258, 489);
this.btnJouer.Name = "btnJouer";
this.btnJouer.Size = new System.Drawing.Size(1361, 95);
this.btnJouer.TabIndex = 4;
this.btnJouer.Text = "Jouer";
this.btnJouer.UseVisualStyleBackColor = false;
this.btnJouer.Click += new System.EventHandler(this.btnJouer_Click_1);
//
// btnPara
//
this.btnPara.BackColor = System.Drawing.Color.Transparent;
this.btnPara.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnPara.Font = new System.Drawing.Font("Segoe UI", 16F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnPara.ForeColor = System.Drawing.Color.White;
this.btnPara.Location = new System.Drawing.Point(258, 621);
this.btnPara.Name = "btnPara";
this.btnPara.Size = new System.Drawing.Size(1361, 95);
this.btnPara.TabIndex = 5;
this.btnPara.Text = "Paramètres";
this.btnPara.UseVisualStyleBackColor = false;
this.btnPara.Click += new System.EventHandler(this.btnPara_Click);
//
// Main
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ClientSize = new System.Drawing.Size(1920, 1046);
this.Controls.Add(this.btnPara);
this.Controls.Add(this.btnJouer);
this.Controls.Add(this.btnQuitter);
this.Controls.Add(this.titre);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "Main";
this.Text = "Poker";
this.Load += new System.EventHandler(this.Main_Load);
this.titre.ResumeLayout(false);
this.titre.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TableLayoutPanel titre;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnQuitter;
private System.Windows.Forms.Button btnJouer;
private System.Windows.Forms.Button btnPara;
}
}
Target form : (config.cs)
namespace Poker
{
partial class Config
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(525, 270);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(113, 65);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Config
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Config";
this.Text = "Configuration";
this.Load += new System.EventHandler(this.Config_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
}
}
The problem is you creating a new thread, and the Program process switches to the second thread.
How to solve: Create one object of MediaPlayer, and switch a song, when you need.
Or: Create two separated MediaPlayers and pause one when you need.
Related
I am trying to create a winform application. Accidentally I found that Radiobutton.Checked event is fired when we programmatically assign true to RadioButton.Checked. But it is not fired when we programmatically assign false to Radiobutton.Checked. But it fires, when we manually checked/unchecked it. Here is a sample Code. Copy it,compile it and run it to check
using System.Windows.Forms;
using System;
using System.ComponentModel;
using System.Windows;
namespace Temp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//radioButton1.Checked = true;
//radioButton2.Checked = true;
radioButton1.Checked = false;
radioButton2.Checked = false;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
System.Windows.Forms.RadioButton tempRB = (System.Windows.Forms.RadioButton)sender;
MessageBox.Show(tempRB.Name + " : " + (tempRB.Checked ? "Checked" : "Unchecked"));
//radioButton2.Checked = !radioButton1.Checked;
}
}
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(173, 84);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.radioButton2);
this.groupBox1.Controls.Add(this.radioButton1);
this.groupBox1.Location = new System.Drawing.Point(385, 56);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(200, 100);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(21, 28);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(85, 17);
this.radioButton1.TabIndex = 0;
this.radioButton1.Text = "radioButton1";
this.radioButton1.UseVisualStyleBackColor = true;
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// radioButton2
//
this.radioButton2.AutoSize = true;
this.radioButton2.Location = new System.Drawing.Point(21, 52);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(85, 17);
this.radioButton2.TabIndex = 1;
this.radioButton2.Text = "radioButton2";
this.radioButton2.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(663, 261);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton1;
}
static class Temp
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Can anybody give an explanation about this. And also help me on how to fire RadioButton.Checked event when it got assigned false programmatically.
Note: I am using .net 3.5,Windows 10.
EDIT:
I am adding slightly modified code. In this "Uncheck" button didn't trigger anything. But "Check Both" triggers two times CheckedChanged event. Check this code
using System.Windows.Forms;
using System;
using System.ComponentModel;
using System.Windows;
namespace Temp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
radioButton1.Checked = false;
radioButton2.Checked = false;
}
private void button2_Click(object sender, EventArgs e)
{
radioButton1.Checked = true;
radioButton2.Checked = true;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
System.Windows.Forms.RadioButton tempRB = (System.Windows.Forms.RadioButton)sender;
MessageBox.Show(tempRB.Name + " : " + (tempRB.Checked ? "Checked" : "Unchecked"));
//radioButton2.Checked = !radioButton1.Checked;
}
}
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.button2 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(173, 84);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "UnCheck";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(173, 132);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "Check Both";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.radioButton2);
this.groupBox1.Controls.Add(this.radioButton1);
this.groupBox1.Location = new System.Drawing.Point(385, 56);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(200, 100);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(21, 28);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(85, 17);
this.radioButton1.TabIndex = 0;
this.radioButton1.Text = "radioButton1";
this.radioButton1.UseVisualStyleBackColor = true;
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// radioButton2
//
this.radioButton2.AutoSize = true;
this.radioButton2.Location = new System.Drawing.Point(21, 52);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(85, 17);
this.radioButton2.TabIndex = 1;
this.radioButton2.Text = "radioButton2";
this.radioButton2.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(663, 261);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.button2);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton1;
}
static class Temp
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
I've created a form that I call a ListChooser. It looks like so:
It consists of a System.Windows.Forms.Button and .TextBox.
Clicking on the Button (shown above with the text "Fruits") causes a dialog to popup and some selections are made and the TextBox is populated, like so:
My problem is I can't figure out how to set a System.Windows.Forms.Binding to this TextBox. I think the complication is that it's not just a TextBox, it's 2 components in one. (I know how to data bind with just a standalone TextBox). If I go to the TextBox > Properties > DataBindings > Advanced > ..., All entries listed under Property are not selectable:
I've tried this which has no effect:
private System.Windows.Forms.BindingSource myBindingSource;
this.myBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.myBindingSource.DataSource = typeof(Foods) // Fruits is a Property of Foods;
myListChooser.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.myBindingSource, "Fruits", true));
How do I do this?
ListChooser.Designer.cs:
partial class ListChooser: UserControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnPopup = new System.Windows.Forms.Button();
this.textBox = new System.Windows.Forms.TextBox();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.SuspendLayout();
//
// btnPopup
//
this.btnPopup.AutoSize = true;
this.btnPopup.Dock = System.Windows.Forms.DockStyle.Fill;
this.btnPopup.Location = new System.Drawing.Point(0, 0);
this.btnPopup.Margin = new System.Windows.Forms.Padding(0);
this.btnPopup.Name = "btnPopup";
this.btnPopup.Size = new System.Drawing.Size(75, 21);
this.btnPopup.TabIndex = 0;
this.btnPopup.Text = "Type";
this.btnPopup.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.btnPopup.UseVisualStyleBackColor = true;
this.btnPopup.Click += new System.EventHandler(this.btnPopup_Click);
//
// txtList
//
this.textBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox.BackColor = System.Drawing.SystemColors.Window;
this.textBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBox.Location = new System.Drawing.Point(0, 0);
this.textBox.Name = "txtList";
this.textBox.ReadOnly = true;
this.textBox.Size = new System.Drawing.Size(297, 20);
this.textBox.TabIndex = 1;
this.textBox.TabStop = false;
this.textBox.TextChanged += new System.EventHandler(this.txtList_TextChanged);
//this.textBox.Leave += new System.EventHandler(this.txtList_Leave);
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Margin = new System.Windows.Forms.Padding(0);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.btnPopup);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.textBox);
this.splitContainer1.Size = new System.Drawing.Size(375, 21);
this.splitContainer1.SplitterDistance = 75;
this.splitContainer1.SplitterWidth = 3;
this.splitContainer1.TabIndex = 2;
//
// ListChooser
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.WhiteSmoke;
this.Controls.Add(this.splitContainer1);
this.MaximumSize = new System.Drawing.Size(1200, 21);
this.Name = "ListChooser";
this.Size = new System.Drawing.Size(375, 21);
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.Panel2.PerformLayout();
this.splitContainer1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button btnPopup;
private System.Windows.Forms.TextBoxBase textBox;
private SplitContainer splitContainer1;
}
I have this VB +WinForms Application . The UI is captured in the image below
The Add/Edit Material Window has its own unique Aqua blue color border .
The VB code behind is
Option Strict Off
Option Explicit On
Friend Class FrmAddMaterial
Inherits System.Windows.Forms.Form
Friend ErrorFlag As ErrorFlagType
Friend SavePath As String
Public Event UpdateMaterialFile()
Private Sub CmdAdd_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CmdAdd.Click
Dim AddEditMat As New AddEditMaterialDialog
With AddEditMat
.TxtMaterialName.Text = ""
.TxtWetRefractiveIndex.Text = ""
.TxtDryRefractiveIndex.Text = ""
.TxtLinearExpansion.Text = ""
.TxtRadialExpansion.Text = ""
.txtMDPrefix.Text = ""
.TxtMaterialNumber.Text = Me.LstMaterials.Items.Count.ToString
.Text = "Add Material"
.SavePath = SavePath
.Show()
End With
If LstMaterials.SelectedIndex > -1 Then
UpdateMaterialScreen(Me, LstMaterials.SelectedIndex, ErrorFlag)
Else
UpdateMaterialScreen(Me, 0, ErrorFlag)
End If
End Sub
Private Sub CmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CmdEdit.Click
Dim MaterialData As MaterialType
MaterialData = GetMaterialData(Me.LstMaterials.SelectedIndex, _
SavePath, _
ErrorFlag)
Dim MatRef As New AddEditMaterialDialog
With MatRef
.TxtMaterialName.Text = MaterialData.MaterialName
.TxtWetRefractiveIndex.Text = MaterialData.WetRefractiveIndex.ToString
.TxtDryRefractiveIndex.Text = MaterialData.DryRefractiveIndex.ToString
.TxtLinearExpansion.Text = MaterialData.LinearExpansion.ToString
.TxtRadialExpansion.Text = MaterialData.RadiusExpansion.ToString
.txtMDPrefix.Text = MaterialData.MDPrefix
.TxtMaterialNumber.Text = MaterialData.Index.ToString
.Text = "Edit Material"
.SavePath = SavePath
.Show()
End With
End Sub
Private Sub CmdRemoveMaterial_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CmdRemoveMaterial.Click
Dim Msg As String
Dim MsgVal As Short
Msg = "Are you sure that you want to remove this material?"
MsgVal = MsgBox(Msg, MsgBoxStyle.YesNo, InfoMsg)
If MsgVal = MsgBoxResult.Yes Then
If LstMaterials.SelectedIndex <> -1 Then
RemoveMaterialData(LstMaterials.SelectedIndex, _
SavePath, _
ErrorFlag)
Me.LstMaterials.Items.RemoveAt((LstMaterials.SelectedIndex))
If LstMaterials.Items.Count > 0 Then
LstMaterials.SelectedIndex = 0
End If
Call UpdateMaterialScreen(Me, LstMaterials.SelectedIndex, ErrorFlag)
End If
End If
End Sub
Private Sub FrmAddMaterial_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Enter
If LstMaterials.SelectedIndex > -1 Then
UpdateMaterialScreen(Me, LstMaterials.SelectedIndex, ErrorFlag)
Else
UpdateMaterialScreen(Me, 0, ErrorFlag)
End If
End Sub
Private Sub FrmAddMaterial_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Me.Width = 278
Me.Height = 289
SavePath = System.IO.Path.Combine(ConfigSoftData.DirectoryData.AppPath, "Mold\lib")
End Sub
Private Sub LstMaterials_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles LstMaterials.SelectedIndexChanged
Dim MaterialData As MaterialType
MaterialData = GetMaterialData(LstMaterials.SelectedIndex, _
SavePath, _
ErrorFlag)
With Me
.TxtWetRefractiveIndex.Text = MaterialData.WetRefractiveIndex.ToString
.TxtMDPrefix.Text = MaterialData.MDPrefix
End With
End Sub
Private Sub FrmAddMaterial_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
RaiseEvent UpdateMaterialFile()
End Sub
End Class
I tried to convert the Application UI to C# and matched all the properties set in the VB forms but I am getting something like this
The C# code behind is
using Microsoft.VisualBasic;
using Mold_Power_Suite.Model;
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 Mold_Power_Suite.View
{
public partial class FrmAddMaterial : Form
{
public FrmAddMaterial()
{
InitializeComponent();
//FormClosed += FrmAddMaterial_FormClosed;
//Load += FrmAddMaterial_Load;
//Enter += FrmAddMaterial_Enter;
}
internal Mold_Power_Suite.Model.FrontEndStructures.ErrorFlagType ErrorFlag;
internal string SavePath;
public event UpdateMaterialFileEventHandler UpdateMaterialFile;
public delegate void UpdateMaterialFileEventHandler();
private void CmdAdd_Click(System.Object eventSender, System.EventArgs eventArgs)
{
AddEditMaterialDialog AddEditMat = new AddEditMaterialDialog();
//var _with1 = AddEditMat;
AddEditMat.TxtMaterialName.Text = "";
AddEditMat.TxtWetRefractiveIndex.Text = "";
AddEditMat.TxtDryRefractiveIndex.Text = "";
AddEditMat.TxtLinearExpansion.Text = "";
AddEditMat.TxtRadialExpansion.Text = "";
AddEditMat.txtMDPrefix.Text = "";
AddEditMat.TxtMaterialNumber.Text = this.LstMaterials.Items.Count.ToString();
AddEditMat.Text = "Add Material";
AddEditMat.SavePath = SavePath;
AddEditMat.Show();
if (LstMaterials.SelectedIndex > -1) {
ModSoftOutput.UpdateMaterialScreen(this, LstMaterials.SelectedIndex,ref ErrorFlag);
} else {
ModSoftOutput.UpdateMaterialScreen(this, 0, ref ErrorFlag);
}
}
private void CmdEdit_Click(System.Object eventSender, System.EventArgs eventArgs)
{
Mold_Power_Suite.Model.FrontEndStructures.MaterialType MaterialData = default(Mold_Power_Suite.Model.FrontEndStructures.MaterialType);
short RIN=Convert.ToInt16( this.LstMaterials.SelectedIndex);
MaterialData = ModSoftInputMod.GetMaterialData(ref RIN, ref SavePath, ref ErrorFlag);
AddEditMaterialDialog MatRef = new AddEditMaterialDialog();
//var _with2 = MatRef;
MatRef.TxtMaterialName.Text = MaterialData.MaterialName;
MatRef.TxtWetRefractiveIndex.Text = MaterialData.WetRefractiveIndex.ToString();
MatRef.TxtDryRefractiveIndex.Text = MaterialData.DryRefractiveIndex.ToString();
MatRef.TxtLinearExpansion.Text = MaterialData.LinearExpansion.ToString();
MatRef.TxtRadialExpansion.Text = MaterialData.RadiusExpansion.ToString();
MatRef.txtMDPrefix.Text = MaterialData.MDPrefix;
MatRef.TxtMaterialNumber.Text = MaterialData.Index.ToString();
MatRef.Text = "Edit Material";
MatRef.SavePath = SavePath;
MatRef.Show();
}
private void CmdRemoveMaterial_Click(System.Object eventSender, System.EventArgs eventArgs)
{
string Msg = null;
//short MsgVal = 0; // Commented this line and made MsgVal as var
Msg = "Are you sure that you want to remove this material?";
var MsgVal = Interaction.MsgBox(Msg, MsgBoxStyle.YesNo,ModSoftFrontEndGlobalVariables. InfoMsg);
if (MsgVal == MsgBoxResult.Yes)
{
if (LstMaterials.SelectedIndex != -1)
{
short value = Convert.ToInt16((LstMaterials.SelectedIndex));
ModSoftOutput. RemoveMaterialData(ref value, SavePath,ref ErrorFlag);
this.LstMaterials.Items.RemoveAt((LstMaterials.SelectedIndex));
if (LstMaterials.Items.Count > 0) {
LstMaterials.SelectedIndex = 0;
}
ModSoftOutput.UpdateMaterialScreen(this, LstMaterials.SelectedIndex,ref ErrorFlag);
}
}
}
//This event is not being fired up . The code below has been pasted into FrmAddMaterial_Load()
private void FrmAddMaterial_Enter(object sender, System.EventArgs e)
{
if (LstMaterials.SelectedIndex > -1) {
ModSoftOutput.UpdateMaterialScreen(this, LstMaterials.SelectedIndex,ref ErrorFlag);
} else {
ModSoftOutput.UpdateMaterialScreen(this, 0,ref ErrorFlag);
}
}
private void FrmAddMaterial_Load(System.Object eventSender, System.EventArgs eventArgs)
{
this.Width = 278;
this.Height = 289;
SavePath = System.IO.Path.Combine(ModSoftFrontEndGlobalVariables.ConfigSoftData.DirectoryData.AppPath, "Mold\\lib");
/*if (LstMaterials.SelectedIndex > -1)
{
ModSoftOutput.UpdateMaterialScreen(this, LstMaterials.SelectedIndex, ref ErrorFlag);
}
else
{
ModSoftOutput.UpdateMaterialScreen(this, 0, ref ErrorFlag);
}*/
}
private void LstMaterials_SelectedIndexChanged(System.Object eventSender, System.EventArgs eventArgs)
{
Mold_Power_Suite.Model.FrontEndStructures.MaterialType MaterialData = default(Mold_Power_Suite.Model.FrontEndStructures.MaterialType);
short RINumber= Convert.ToInt16(LstMaterials.SelectedIndex);
MaterialData = ModSoftInputMod.GetMaterialData(ref RINumber,ref SavePath,ref ErrorFlag);
// var _with3 = this;
this.TxtWetRefractiveIndex.Text = MaterialData.WetRefractiveIndex.ToString();
this.TxtMDPrefix.Text = MaterialData.MDPrefix;
}
private void FrmAddMaterial_FormClosed(object sender, FormClosedEventArgs e)
{
if (UpdateMaterialFile != null) {
UpdateMaterialFile();
}
}
}
}
I have made sure to add these lines in my Program.cs file in C# code
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Mold_Power_Suite.View.frmMain mainForm = new frmMain();
// WindowsFormsApplication3.view.Form1 abc = new view.Form1();
Application.Run(mainForm);
}
}
Here is the Designer.cs
namespace Mold_Power_Suite.View
{
partial class FrmAddMaterial
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.ToolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.TxtMDPrefix = new System.Windows.Forms.TextBox();
this.CmdRemoveMaterial = new System.Windows.Forms.Button();
this.CmdReturn = new System.Windows.Forms.Button();
this.CmdEdit = new System.Windows.Forms.Button();
this.CmdAdd = new System.Windows.Forms.Button();
this.TxtWetRefractiveIndex = new System.Windows.Forms.TextBox();
this.LstMaterials = new System.Windows.Forms.ListBox();
this.Label2 = new System.Windows.Forms.Label();
this.Label1 = new System.Windows.Forms.Label();
this.toolTip2 = new System.Windows.Forms.ToolTip(this.components);
this.SuspendLayout();
//
// TxtMDPrefix
//
this.TxtMDPrefix.AcceptsReturn = true;
this.TxtMDPrefix.BackColor = System.Drawing.SystemColors.Window;
this.TxtMDPrefix.Cursor = System.Windows.Forms.Cursors.IBeam;
this.TxtMDPrefix.Enabled = false;
this.TxtMDPrefix.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.TxtMDPrefix.ForeColor = System.Drawing.SystemColors.WindowText;
this.TxtMDPrefix.Location = new System.Drawing.Point(152, 80);
this.TxtMDPrefix.MaxLength = 0;
this.TxtMDPrefix.Name = "TxtMDPrefix";
this.TxtMDPrefix.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.TxtMDPrefix.Size = new System.Drawing.Size(105, 20);
this.TxtMDPrefix.TabIndex = 2;
//
// CmdRemoveMaterial
//
this.CmdRemoveMaterial.BackColor = System.Drawing.SystemColors.Control;
this.CmdRemoveMaterial.Cursor = System.Windows.Forms.Cursors.Default;
this.CmdRemoveMaterial.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.CmdRemoveMaterial.ForeColor = System.Drawing.SystemColors.ControlText;
this.CmdRemoveMaterial.Location = new System.Drawing.Point(152, 219);
this.CmdRemoveMaterial.Name = "CmdRemoveMaterial";
this.CmdRemoveMaterial.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.CmdRemoveMaterial.Size = new System.Drawing.Size(105, 25);
this.CmdRemoveMaterial.TabIndex = 5;
this.CmdRemoveMaterial.Text = "&Remove Material";
this.CmdRemoveMaterial.UseVisualStyleBackColor = false;
this.CmdRemoveMaterial.Click += new System.EventHandler(this.CmdRemoveMaterial_Click);
//
// CmdReturn
//
this.CmdReturn.BackColor = System.Drawing.SystemColors.Control;
this.CmdReturn.Cursor = System.Windows.Forms.Cursors.Default;
this.CmdReturn.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.CmdReturn.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.CmdReturn.ForeColor = System.Drawing.SystemColors.ControlText;
this.CmdReturn.Location = new System.Drawing.Point(89, 291);
this.CmdReturn.Name = "CmdReturn";
this.CmdReturn.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.CmdReturn.Size = new System.Drawing.Size(105, 25);
this.CmdReturn.TabIndex = 15;
this.CmdReturn.Text = "&Close";
this.CmdReturn.UseVisualStyleBackColor = false;
//
// CmdEdit
//
this.CmdEdit.BackColor = System.Drawing.SystemColors.Control;
this.CmdEdit.Cursor = System.Windows.Forms.Cursors.Default;
this.CmdEdit.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.CmdEdit.ForeColor = System.Drawing.SystemColors.ControlText;
this.CmdEdit.Location = new System.Drawing.Point(152, 187);
this.CmdEdit.Name = "CmdEdit";
this.CmdEdit.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.CmdEdit.Size = new System.Drawing.Size(105, 25);
this.CmdEdit.TabIndex = 4;
this.CmdEdit.Text = "&Edit Material";
this.CmdEdit.UseVisualStyleBackColor = false;
this.CmdEdit.Click += new System.EventHandler(this.CmdEdit_Click);
//
// CmdAdd
//
this.CmdAdd.BackColor = System.Drawing.SystemColors.Control;
this.CmdAdd.Cursor = System.Windows.Forms.Cursors.Default;
this.CmdAdd.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.CmdAdd.ForeColor = System.Drawing.SystemColors.ControlText;
this.CmdAdd.Location = new System.Drawing.Point(152, 155);
this.CmdAdd.Name = "CmdAdd";
this.CmdAdd.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.CmdAdd.Size = new System.Drawing.Size(105, 25);
this.CmdAdd.TabIndex = 3;
this.CmdAdd.Text = "&Add Material";
this.CmdAdd.UseVisualStyleBackColor = false;
this.CmdAdd.Click += new System.EventHandler(this.CmdAdd_Click);
//
// TxtWetRefractiveIndex
//
this.TxtWetRefractiveIndex.AcceptsReturn = true;
this.TxtWetRefractiveIndex.BackColor = System.Drawing.SystemColors.Window;
this.TxtWetRefractiveIndex.Cursor = System.Windows.Forms.Cursors.IBeam;
this.TxtWetRefractiveIndex.Enabled = false;
this.TxtWetRefractiveIndex.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.TxtWetRefractiveIndex.ForeColor = System.Drawing.SystemColors.WindowText;
this.TxtWetRefractiveIndex.Location = new System.Drawing.Point(152, 32);
this.TxtWetRefractiveIndex.MaxLength = 0;
this.TxtWetRefractiveIndex.Name = "TxtWetRefractiveIndex";
this.TxtWetRefractiveIndex.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.TxtWetRefractiveIndex.Size = new System.Drawing.Size(105, 20);
this.TxtWetRefractiveIndex.TabIndex = 1;
//
// LstMaterials
//
this.LstMaterials.BackColor = System.Drawing.SystemColors.Window;
this.LstMaterials.Cursor = System.Windows.Forms.Cursors.Default;
this.LstMaterials.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.LstMaterials.ForeColor = System.Drawing.SystemColors.WindowText;
this.LstMaterials.ItemHeight = 14;
this.LstMaterials.Location = new System.Drawing.Point(16, 16);
this.LstMaterials.Name = "LstMaterials";
this.LstMaterials.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.LstMaterials.Size = new System.Drawing.Size(121, 228);
this.LstMaterials.TabIndex = 0;
this.LstMaterials.SelectedIndexChanged += new System.EventHandler(this.LstMaterials_SelectedIndexChanged);
//
// Label2
//
this.Label2.BackColor = System.Drawing.SystemColors.Control;
this.Label2.Cursor = System.Windows.Forms.Cursors.Default;
this.Label2.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Label2.ForeColor = System.Drawing.SystemColors.ControlText;
this.Label2.Location = new System.Drawing.Point(152, 64);
this.Label2.Name = "Label2";
this.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Label2.Size = new System.Drawing.Size(81, 17);
this.Label2.TabIndex = 8;
this.Label2.Text = "Data File Prefix";
//
// Label1
//
this.Label1.BackColor = System.Drawing.SystemColors.Control;
this.Label1.Cursor = System.Windows.Forms.Cursors.Default;
this.Label1.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Label1.ForeColor = System.Drawing.SystemColors.ControlText;
this.Label1.Location = new System.Drawing.Point(152, 16);
this.Label1.Name = "Label1";
this.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Label1.Size = new System.Drawing.Size(105, 17);
this.Label1.TabIndex = 7;
this.Label1.Text = "Wet Refractive Index";
//
// FrmAddMaterial
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 19F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.CmdReturn;
this.ClientSize = new System.Drawing.Size(272, 255);
this.Controls.Add(this.TxtMDPrefix);
this.Controls.Add(this.CmdRemoveMaterial);
this.Controls.Add(this.CmdReturn);
this.Controls.Add(this.CmdEdit);
this.Controls.Add(this.CmdAdd);
this.Controls.Add(this.TxtWetRefractiveIndex);
this.Controls.Add(this.LstMaterials);
this.Controls.Add(this.Label2);
this.Controls.Add(this.Label1);
this.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Location = new System.Drawing.Point(3, 14);
this.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "FrmAddMaterial";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultBounds;
this.Text = " Add/Edit Material";
this.Activated += new System.EventHandler(this.FrmAddMaterial_Enter);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.FrmAddMaterial_FormClosed);
this.Load += new System.EventHandler(this.FrmAddMaterial_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
public System.Windows.Forms.ToolTip ToolTip1;
public System.Windows.Forms.TextBox TxtMDPrefix;
public System.Windows.Forms.Button CmdRemoveMaterial;
public System.Windows.Forms.Button CmdReturn;
public System.Windows.Forms.Button CmdEdit;
public System.Windows.Forms.Button CmdAdd;
public System.Windows.Forms.TextBox TxtWetRefractiveIndex;
public System.Windows.Forms.ListBox LstMaterials;
public System.Windows.Forms.Label Label2;
public System.Windows.Forms.Label Label1;
public System.Windows.Forms.ToolTip toolTip2;
}
}
How to I make the application and all the corresponding popups look exactly the way they look in VB ?
The nee Windows ,when not active tend to go behind the application. I want them not to go outside the Parent Window.
How can I fix these?
Instead of
MatRef.Show();
write
MatRef.ShowDialog();
If that not working than add one more line before that
MatRef.TopLevel = false;
Sample Code:
ParentForm.Designer.cs
namespace MyPro
{
partial class ParentForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(231, 193);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(103, 44);
this.button1.TabIndex = 0;
this.button1.Text = "Show Dialog";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// ParentForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1059, 719);
this.Controls.Add(this.button1);
this.Name = "ParentForm";
this.Text = "Parent Form";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
}
}
ParentForm.cs
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 MyPro
{
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form f = new DialogWindow();
f.StartPosition = FormStartPosition.CenterScreen;
f.ShowDialog();
}
}
}
DialogWindow.Designer.cs
namespace MyPro
{
partial class DialogWindow
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// DialogWindow
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(282, 255);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "DialogWindow";
this.Text = "DialogWindow";
this.ResumeLayout(false);
}
#endregion
}
}
I wrote a GUI Form in c#. When I executed the program, white color becomes transparent.
It's suppose to be like this:
But when I run, it's like that:
You can see that white color became black like the picture behind.
using System;
using System.Drawing;
namespace Taki_Client
{
partial class SignIn
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnMenu = new System.Windows.Forms.Button();
this.picSignIn = new System.Windows.Forms.PictureBox();
this.btnSubmit = new System.Windows.Forms.Button();
this.tbUsername = new System.Windows.Forms.TextBox();
this.tbPassword = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.picSignIn)).BeginInit();
this.SuspendLayout();
//
// btnMenu
//
this.btnMenu.BackgroundImage = global::Taki_Client.Properties.Resources.Menu_Button;
this.btnMenu.Location = new System.Drawing.Point(314, 476);
this.btnMenu.Name = "btnMenu";
this.btnMenu.Size = new System.Drawing.Size(157, 57);
this.btnMenu.TabIndex = 4;
this.btnMenu.UseVisualStyleBackColor = true;
this.btnMenu.MouseClick += new System.Windows.Forms.MouseEventHandler(this.btnExit_MouseClick);
//
// picSignIn
//
this.picSignIn.BackColor = System.Drawing.Color.Transparent;
this.picSignIn.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.picSignIn.ErrorImage = null;
this.picSignIn.Image = global::Taki_Client.Properties.Resources.SignIn;
this.picSignIn.ImageLocation = "";
this.picSignIn.Location = new System.Drawing.Point(272, 12);
this.picSignIn.Name = "picSignIn";
this.picSignIn.Size = new System.Drawing.Size(288, 161);
this.picSignIn.TabIndex = 0;
this.picSignIn.TabStop = false;
this.picSignIn.Click += new System.EventHandler(this.pictureBox1_Click);
//
// btnSubmit
//
this.btnSubmit.BackgroundImage = global::Taki_Client.Properties.Resources.Submit_Button;
this.btnSubmit.Location = new System.Drawing.Point(314, 347);
this.btnSubmit.Name = "btnSubmit";
this.btnSubmit.Size = new System.Drawing.Size(157, 57);
this.btnSubmit.TabIndex = 5;
this.btnSubmit.UseVisualStyleBackColor = true;
this.btnSubmit.MouseClick += new System.Windows.Forms.MouseEventHandler(this.btnSubmit_MouseClick);
//
// tbUsername
//
this.tbUsername.BackColor = System.Drawing.SystemColors.HotTrack;
this.tbUsername.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
this.tbUsername.ForeColor = System.Drawing.SystemColors.Menu;
this.tbUsername.Location = new System.Drawing.Point(314, 223);
this.tbUsername.Multiline = true;
this.tbUsername.Name = "tbUsername";
this.tbUsername.Size = new System.Drawing.Size(201, 44);
this.tbUsername.TabIndex = 6;
this.tbUsername.Text = "Username";
//
// tbPassword
//
this.tbPassword.BackColor = System.Drawing.SystemColors.HotTrack;
this.tbPassword.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
this.tbPassword.ForeColor = System.Drawing.SystemColors.Menu;
this.tbPassword.Location = new System.Drawing.Point(314, 284);
this.tbPassword.Multiline = true;
this.tbPassword.Name = "tbPassword";
this.tbPassword.Size = new System.Drawing.Size(201, 44);
this.tbPassword.TabIndex = 7;
this.tbPassword.Text = "Password";
//
// SignIn
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.BackgroundImage = global::Taki_Client.Properties.Resources.Taki_main_screen;
this.ClientSize = new System.Drawing.Size(804, 611);
this.Controls.Add(this.tbPassword);
this.Controls.Add(this.tbUsername);
this.Controls.Add(this.btnSubmit);
this.Controls.Add(this.btnMenu);
this.Controls.Add(this.picSignIn);
this.Name = "SignIn";
this.Text = "Taki Client";
this.TransparencyKey = System.Drawing.Color.Transparent;
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.picSignIn)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.PictureBox picSignIn;
private System.Windows.Forms.Button btnMenu;
private System.Windows.Forms.Button btnSubmit;
private System.Windows.Forms.TextBox tbUsername;
private System.Windows.Forms.TextBox tbPassword;
}
}
By setting the TransparencyKey of the form, transparency is enabled. Default color for transparency is white.
Remove this line from the InitializeComponent () function
this.TransparencyKey = System.Drawing.Color.Transparent;
and the white on your form will no longer be transparent.
I have created a Form in c# windows. My form contains a button and a text box and it 's name is textbox2 here and it contains code like this in Form1.Designer.cs :
this.textBox2.Location = new System.Drawing.Point(683, 14);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 22;
Now i have created another class called Programss.cs which will do some task and as the task finished i want to print on my form something like this:
textBox2.txt= "Task finished"; //it should print on the textbox of my Form1.cs
How to access this textBox2(which can only be accessed currently in Form1.Designer.cs) in Programss.cs and my other classes ?
EDIT: After Philip Stuyck's comments:
Here is my Form1.cs code :
namespace S
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//please see here
public string TextMessage
{
get { return textBox3.Text; }
set { textBox3.Text = value; }
}
private void btnStart_Click(object sender, EventArgs e)
{
btnStart.Enabled = false;
// StartServer();
}
private void btnClose_Click(object sender, EventArgs e)
{
}
}
}
And Program.cs is :
namespace Senter code here
{
class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
/////////////////////////////////////////////////////////////////////////////////////
// OtherImportantClasses.Programs prgms = new OtherImportantClasses.Programs();
Form1 theForm = new Form1();
theForm.TextMessage = "Task finished";
}
}
}
Form1.Designer.cs is:
namespace Shekhar
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
public void InitializeComponent()
{
this.btnClose = new System.Windows.Forms.Button();
this.btnStart = new System.Windows.Forms.Button();
this.txtPort = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnClose
//
this.btnClose.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnClose.Location = new System.Drawing.Point(313, 11);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(75, 23);
this.btnClose.TabIndex = 21;
this.btnClose.Text = "Close";
this.btnClose.UseVisualStyleBackColor = true;
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// btnStart
//
this.btnStart.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnStart.Location = new System.Drawing.Point(227, 12);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 23);
this.btnStart.TabIndex = 20;
this.btnStart.Text = "Start";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(140, 12);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(79, 20);
this.txtPort.TabIndex = 19;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(78, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(55, 18);
this.label1.TabIndex = 18;
this.label1.Text = "Port : ";
OtherImportantClasses.Programs prog = new OtherImportantClasses.Programs();
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.Location = new System.Drawing.Point(400, 20);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(276, 18);
this.label2.TabIndex = 18;
this.label2.Text = "Total Number of device connected :";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(683, 14);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 22;
// this.textBox2.Text = "testbox2";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(81, 60);
this.textBox3.Multiline = true;
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(702, 243);
this.textBox3.TabIndex = 23;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(818, 315);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Shekhar\'s GT06";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
/*
public string TextMessage
{
get
{
return textBox2.Text;
}
set
{
textBox2.Text = value;
}
}
*/
#endregion
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
}
}
Form1.Designer.cs is :
namespace S`enter code here`
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
public void InitializeComponent()
{
this.btnClose = new System.Windows.Forms.Button();
this.btnStart = new System.Windows.Forms.Button();
this.txtPort = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnClose
//
this.btnClose.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnClose.Location = new System.Drawing.Point(313, 11);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(75, 23);
this.btnClose.TabIndex = 21;
this.btnClose.Text = "Close";
this.btnClose.UseVisualStyleBackColor = true;
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// btnStart
//
this.btnStart.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnStart.Location = new System.Drawing.Point(227, 12);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 23);
this.btnStart.TabIndex = 20;
this.btnStart.Text = "Start";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(140, 12);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(79, 20);
this.txtPort.TabIndex = 19;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(78, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(55, 18);
this.label1.TabIndex = 18;
this.label1.Text = "Port : ";
OtherImportantClasses.Programs prog = new OtherImportantClasses.Programs();
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.Location = new System.Drawing.Point(400, 20);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(276, 18);
this.label2.TabIndex = 18;
this.label2.Text = "Total Number of device connected :";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(683, 14);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 22;
// this.textBox2.Text = "testbox2";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(81, 60);
this.textBox3.Multiline = true;
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(702, 243);
this.textBox3.TabIndex = 23;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(818, 315);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Shekhar\'s GT06";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
/*
public string TextMessage
{
get
{
return textBox2.Text;
}
set
{
textBox2.Text = value;
}
}
*/
#endregion
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
}
}
It dont give any error but it do not print anything in textBox3 .
NOTE: Please note that first i run the winform code i click a button and when i click the button some proccess will run which will further print the text in the textbox. (I feel like my textmessage is not intialized by my message passed to it).
Make sure that textBox2 is public.(can be done in the designer)
Keep a reference to Form1 when you create it and access textBox2 via that reference.
Form1 theForm = new Form1();
... //do stuff
theForm.textBox2.Text = "Task finished";
This is not the most elegant approach to this, but it should work.
An alternative is like this :
public partial class Form1{
public string TextMessage{
get{
return textBox2.Text;
}
set{
textBox2.Text = value;
}
}
}
and in program.cs
Form1 theForm = new Form1();
... //do stuff
theForm.TextMessage = "Task finished";
your code will not work because
Form1 theForm = new Form1();
theForm.TextMessage = "Task finished";
is never executed.
Application.Run(new Form1());
returns only when the form is closed and then the application will end.
Following will work:
Form1 theForm = new Form1();
theForm.TextMessage = "Task finished";
Application.Run(theForm);
But I have the feeling you don't quite understand the framework.
Application.run, will make your application enter into a message loop which makes the application receive window messages.
One way you could do this is to have a method accessible in your Form1 that sets the textBox name to whatever you want. For instance:
public static void SetText (string message)
{
TextBox2.Text = message;
}
You would then call this from your Program.cs class:
Form1.SetText("Whatever text you want the text on the Form1 - TextBox2 to say");
This is the poor man's way of changing the text. There is a more detailed explanation on this other post if you need more details.
As I understood you want to print end result on textBox2.txt textbox, right? So you want to execute some methods on Programss.cs and once execution is finished you want to show "Task finished" on textBox2.txt.
If I am not wrong, what you have to do is return a value from a method from Programss.cs class. You have to call this method from Form1.Designer.cs class. Based on return value you can do whatever you want.
Hope this will be helpful.
You are creating two separate forms, setting text on the second, and expecting it to appear on the first.
Do this:
Form1 theForm = new Form1();
theForm.TextMessage = "Task finished";
Application.Run(theForm);