I have a UserControl named "UserControl1" with a label inside it and a custom property:
[Browsable(true)]
public new string Text
{
get { return label1.Text; }
set { label1.Text = value; }
}
public UserControl1()
{
InitializeComponent();
}
This UserControl is used in a form named "Form1".
In the designer appears the property but when I write some text and build the application the text is cleared. I can see, the property isn't written in the Form1.Designer.cs.
If I change the property name to some other word all is ok.. Note the "new" keyword to override the base variable..
I have found a similar question here but there is no solution.
Greetings!
EDIT: There is no hardcoded value:
UserControl1.Designer.cs:
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(64, 63);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 0;
//
// UserControl1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.label1);
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(181, 136);
this.ResumeLayout(false);
this.PerformLayout();
Form1.Designer.cx:
//
// userControl11
//
this.userControl11.Location = new System.Drawing.Point(35, 43);
this.userControl11.Name = "userControl11";
this.userControl11.Size = new System.Drawing.Size(181, 136);
this.userControl11.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.userControl11);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
To reproduce the problem just create a new windows form application, create an user control with a label inside it and with the property named "Text".
Try using override instead of new for the Text property and include the DesignerSerializationVisibility attribute:
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text {
get { return label1.Text; }
set { label1.Text = value; }
}
Look inside InitializeComponent(). It is most likely hard-coding a default value when you place the control on the design surface.
Related
When I create transparent applications with WebView2 c# form,
It is not possible to click on visible elements.
When I click, it clicks on the window behind.
I don't have the problem with WebBrowser
Do you have a solution ?
Thank you
//
// webView21
//
this.webView21.CreationProperties = null;
this.webView21.DefaultBackgroundColor = System.Drawing.Color.Transparent;
this.webView21.Location = new System.Drawing.Point(462, 12);
this.webView21.Name = "webView21";
this.webView21.Size = new System.Drawing.Size(337, 429);
this.webView21.Source = new System.Uri("https://***", System.UriKind.Absolute);
this.webView21.TabIndex = 2;
this.webView21.ZoomFactor = 1D;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.webBrowser1);
this.Controls.Add(this.webView21);
this.Name = "Form1";
this.Text = "Form1";
this.TransparencyKey = System.Drawing.Color.Transparent;
((System.ComponentModel.ISupportInitialize)(this.webView21)).EndInit();
this.ResumeLayout(false);
I am making a form system where i need to find a panel to change it's background using string imploration. I have done something similar for a label using the code below where numberEntered is a seperate interger in the program.
Label label = Controls.Find($"Num{numberEntered}", true).OfType<Label>().FirstOrDefault();
label.Text = "Text"
How can I do something similar for a panel where where I can find a panel using a seperate variable in the name? Such as $"Panel{number}"
I have already tried this:
Panel panel = Controls.Find($"Ans{answerNum}Panel", true).OfType<Panel>().FirstOrDefault();
panel.BackgroundImage = Programming_Project.Properties.Resources.MutliChoiceCorrectSelected;
However it throws a NullReferenceException. Any help is much appreciated!
Try it this way please:
String ctlName = $"Ans{answerNum}Panel";
Panel panel = this.Controls.Find(ctlName, true).FirstOrDefault() as Panel;
if (panel != null) {
panel.BackgroundImage = Programming_Project.Properties.Resources.MutliChoiceCorrectSelected;
}
else {
MessageBox.Show("Unable to find " + ctlName);
}
You can try to open Form1.Designer.cs where Form1 is your form name.
Then find the panel code and modify it.
That's how I tried and it worked.
It should look something like this:`
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
**this.panel1.BackColor = System.Drawing.SystemColors.ActiveCaption;**
this.panel1.Location = new System.Drawing.Point(169, 41);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(427, 190);
this.panel1.TabIndex = 0;
this.panel1.Paint += new
System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
I have Windows Form Application. There are 2 standart controls on a form as linklabel and reportviewer. Data represented on reportViewer control sometimes so long and there possible some scrolling to keep all data for reading. It's normal situation.
But I need simple feature, when user is scrolling up or down, linkLabel should move depend scrolling values to up or down, synchronously reportViewer content. LinkLabel location must be fixed comparatively reportViewer
InitializeComponent method included:
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(HRCard));
this.reportViewer1 = new Microsoft.Reporting.WinForms.ReportViewer();
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.SuspendLayout();
//
// reportViewer1
//
this.reportViewer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.reportViewer1.LocalReport.ReportEmbeddedResource = "Kadr_Azerenerji.Report1.rdlc";
this.reportViewer1.Location = new System.Drawing.Point(0, 0);
this.reportViewer1.Name = "reportViewer1";
this.reportViewer1.Size = new System.Drawing.Size(1020, 730);
this.reportViewer1.TabIndex = 0;
this.reportViewer1.RenderingComplete += new Microsoft.Reporting.WinForms.RenderingCompleteEventHandler(this.reportViewer1_RenderingComplete);
this.reportViewer1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.reportViewer1_Scroll);
//
// linkLabel1
//
this.linkLabel1.AutoSize = true;
this.linkLabel1.Location = new System.Drawing.Point(160, 314);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new System.Drawing.Size(45, 15);
this.linkLabel1.TabIndex = 2;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "Diplom";
this.linkLabel1.Visible = false;
this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
//
// HRCard
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(1020, 730);
this.Controls.Add(this.linkLabel1);
this.Controls.Add(this.reportViewer1);
this.Font = new System.Drawing.Font("Times New Roman", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "HRCard";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "Azərenerji Kadr";
this.TransparencyKey = System.Drawing.Color.LavenderBlush;
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.HRCard_FormClosing);
this.Load += new System.EventHandler(this.HRCard_Load);
this.Scroll += new System.Windows.Forms.ScrollEventHandler(this.HRCard_Scroll);
this.ResumeLayout(false);
this.PerformLayout();
How can I do this ?
I have created a Password field in C# as
public System.Windows.Forms.TextBox passwordBox;
other setting made for this field are
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.CornflowerBlue;
this.ClientSize = new System.Drawing.Size(381, 199);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.Controls.Add(this.label1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Progpassword";
this.Text = "Programmer Password";
this.TransparencyKey = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
this.Load += new System.EventHandler(this.Progpassword_Load);
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
I am not able to read the text entered in the TextBox
I am reading the value using following line
string text = PasswordBox.Text;
Is this the right way to read the password field.
Yes text = passwordBox.Text; is only that way how you can get it!
I'm new to C# and WinForms so please excuse me is this is a bit of a newbie question.
I'm trying to add a tooltip to my TrackBar control which shows the current value of the bar as you drag it. I've instantiated a ToolTip object and tried the following handler code but it doesn't show any tooltip:
private void trackBar1_Scroll(object sender, EventArgs e)
{
toolTip1.SetToolTip(trackBar1, trackBar1.Value.ToString());
}
Adam I've just implemented a very simple version of this and it works exactly as expected...
Here's the init code for comparison
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.trackBar1 = new System.Windows.Forms.TrackBar();
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
this.SuspendLayout();
//
// trackBar1
//
this.trackBar1.Location = new System.Drawing.Point(12, 166);
this.trackBar1.Name = "trackBar1";
this.trackBar1.Size = new System.Drawing.Size(268, 42);
this.trackBar1.TabIndex = 1;
this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.trackBar1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
toolTip1.SetToolTip(trackBar1, trackBar1.Value.ToString());
}
And it works as I move the ticker to each additional increment...
How did you initialise the toolTip1 class? The way you set the tool tip text looks ok, maybe you have so set some general properties before the component does the job?
MSDN says
// Create the ToolTip and associate with the Form container.
ToolTip toolTip1 = new ToolTip();
// Set up the delays for the ToolTip.
toolTip1.AutoPopDelay = 5000;
toolTip1.InitialDelay = 1000;
toolTip1.ReshowDelay = 500;
// Force the ToolTip text to be displayed whether or not the form is active.
toolTip1.ShowAlways = true;