c# Opening up a form depending on the linklabel name - c#

I have a combobox that dictates the linklabel name, I would like to select the linklabel depending on the name of it. Here is what I have done so far. First part of the if statement works but the second does not.
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if((string)linkLabel1.Text == "Advanced Software Engineering")
{
Form4 g = new Form4();
g.Show();
}
else if((string)linkLabel1.Text == "Web Research")
{
Form5 g1 = new Form5();
g1.Show();
}
}

What exactly is the problem? Your approach would work fine.
However I would create my own FormBase that has a property called "LinkLabel"
Each WinForm would then set the LinkLable its designed to be used by.
Then in your LinkClicked event do something like this
foreach(FormBase _base in _Forms)
{
if(_base.LinkLabel == linkLabel1.Text)
{
_base.Show();
break;
}
}
EDIT: This wouldn't work if the event is not called by clicking on linkLable1

Related

Remove control created at runtime

I wrote some code to create an additional textbox during runtime. I'm using the metro framework, but this shouldn't matter for my question.
When you click a button, a textbox is being created by a private on_click event:
private void BtnAddButton_Click(object sender, EventArgs e)
{
MetroFramework.Controls.MetroTextBox Textbox2 = new MetroFramework.Controls.MetroTextBox
{
Location = new System.Drawing.Point(98, lblHandy.Location.Y - 30),
Name = "Textbox2",
Size = new System.Drawing.Size(75, 23),
TabIndex = 1
};
this.Controls.Add(Textbox2);
}
What I want to do now is to use the click event of another button, to remove the Textbox again. What I am not sure about is, if I have to remove just the controll or also the object itself. Furthermore I can neither access the Textbox2 Control nor the object from another place.
private void BtnRemoveTextbox2_Click(object sender, EventArgs e)
{
this.Controls.Remove(Textbox2);
}
This does not work, since the other form does not know about Textbox2. What would be the best way to achieve my goal? Do I have to make anything public and if so, how do I do that?
You have to find it first before you choose to remove it.
private void BtnRemoveTextbox2_Click(object sender, EventArgs e)
{
MetroFramework.Controls.MetroTextBox tbx = this.Controls.Find("Textbox2", true).FirstOrDefault() as MetroFramework.Controls.MetroTextBox;
if (tbx != null)
{
this.Controls.Remove(tbx);
}
}
Here, Textbox2 is the ID of your textbox. Please make sure you're setting the ID of your textbox control before adding it.
You need to find those controls using Controls.Find method and then remove and dispose them:
this.Controls.Find("Textbox2", false).Cast<Control>().ToList()
.ForEach(c =>
{
this.Controls.Remove(c);
c.Dispose();
});
Since the control was created in another form, the current form has no way of knowing it by its instance name.
To remove it, loop through all controls and look for its Name:
private void BtnRemoveTextbox2_Click(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if (ctrl.Name == "Textbox2")
this.Controls.Remove(ctrl);
}
}

Enabling/disabling button

I am having trouble getting my 'start' button to show when I select either of the radio boxes.
Ideally, when one of the boxes is selected, the 'start' button will enable and allow to be clicked.
Here is my code for the form, as I am relatively new to C# I'm not sure if I'm posting all of the code you need, I'll post more if required.
public partial class mainForm : Form
{
public mainForm() {
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e) {
}
private void mainForm_Load(object sender, EventArgs e) {
title.Font = new Font("Arial", 10, FontStyle.Bold);
}
private void startButton_Click(object sender, EventArgs e) {
if (radioDice.Checked) {
startButton.Enabled = true; //Activates 'start' button
whichDiceGameForm GameForm = new whichDiceGameForm();
GameForm.Show();
}
if (radioCard.Checked) {
startButton.Enabled = true; //Activates 'start' button
whichCardGame GameForm = new whichCardGame();
GameForm.Show();
}
}
}
[Posting for a friend.]
You have placed your enable code in the Button's Click event Handler while you should do it on your checkboxes changed.
Take this code :
if (radioDice.Checked)
{
startButton.Enabled = true;
}
to radioDice checkbox's changed event handler and this one :
if (radioCard.Checked)
{
startButton.Enabled = true; //Activates 'start' button
}
to radioCard checkbox's changed event handler .
Man, seriously?
ANSWER:
You're trying to enable DISABLED button when clicking on that button. You cannot click DISABLED button. What's more - you're duplicating your code.
Button should be always enabled. You only have two choices. Every choice enabled button. So it should be always enabled. No matter the choice. If there is something hidden and button may be disabled, then enable the button in Radio Click event.
Additional information about your code:
Now. About code duplication. Look what you're doing in startButton_Click. You have duplicated code.
You can do something like:
BaseGameForm f = null;
if(radioDice.Checked)
f = new DiceGameForm();
else
if(radioCard.Checked)
f = new CardGameForm();
f.Show();
(BaseGameForm is base form for every game)
But this is not good solution. Better solution is (somewhere in construtor):
radioDice.Tag = new DiceGameForm();
radioCard.Tag = new CardGameForm();
Then in Start button click you look for checked radio:
foreach(Control c in selectGameTypeGroupBox.Controls) //you could do this using LINQ
{
if((c is RadioButton) && ((RadioButton)c).Checked)
{
((Form)c.Tag).Show();
}
}
But this is still not good solution, because you're creating all game forms at startup and this is stupid.
So the better solution would be to keep game form class name in your radio Tag property and then create object of this class using reflection and Activator.
But this is still not the best solution. But I assume that this is one of your first applications so I won't be telling you now about separating gui from logic. If you want to know more - read on the Internet. Or just ask.

Save the state of a form with PictureBox?

I have a small program in winforms; it's just a program where I can have pictures, but I have a problem. When I have a picture, I close the program and I open it again, the pictures don't stay where I have put them, in the PictureBox.
More simply, I want to keep the state when I close the program, like saving.
Here my code :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
OpenFileDialog f = new OpenFileDialog();
f.ShowDialog();
var chemin = f.FileName;
pictureBox1.ImageLocation = chemin;
}
}
}
Please help me, I can't go on with this problem...
The simplest way to do this is to use the Application Settings. Right-click on your Project and select Properties. Then go to Settings. In the right hand side, you will see a panel with a grid with only one line. Change Setting in the Name column to ImageLocation and leave the other three values (Type, Scope and Value) as their defaults (string, user and blank).
In design view of your form under properties double-click the FormClosing event to create a new handler. Now enter:
if (pictureBox1.ImageLocation != null)
{
Properties.Settings.Default.ImageLocation = pictureBox1.ImageLocation;
Properties.Settings.Default.Save();
}
Finally in the constructor for the form enter the following after InitializeComponent():
if (Properties.Settings.Default.ImageLocation != null)
{
pictureBox1.ImageLocation = Properties.Settings.Default.ImageLocation;
}
HTH

Choosing an MSChart-Extension tool from button click

I am using MSChart-Extensions and I would like the option to choose the Zoom, Pan and Select tools from a button, as well as from the ContextMenuStrip. I feel like the easiest way is to simulate a click from the ContextMenuStrip.Items collection
Here is what I've got. In my form I have this
private void zoomButton_Click(object sender, EventArgs e)
{
this.chart.ChangeTool("Zoom");
}
And in MSChartExtensions.cs I have this
public static void ChangeTool(this Chart sender, string option)
{
Chart chart = sender;
foreach(ToolStripItem item in chart.ContextMenuStrip.Items)
{
if (item.Text == option)
{
item.PerformClick();
break;
}
}
}
This successfully chooses the tool from the collection. However, I am getting a System.ArgumentNullException in the SetChartControlState method. I have stepped through the code and I see that when the application enters ChartContext_ItemClicked, the sender's source control is null. I've dug through MSDN, and found this
A Control that represents the control that is displaying the shortcut menu. If no control has displayed the shortcut menu, the property returns a null reference (Nothing in Visual Basic).
So I assume that because no right-click menu (ContextMenuStrip) is shown, the source control is null. Is there a way around this? How can I get this working? Thanks for the help
I figured it out. Change the ChangeTools() method to this
// In MSChartExtensions.cs
public static void ChangeTool(this Chart sender, string option)
{
if (option == "Zoom")
SetChartControlState(sender, MSChartExtensionToolState.Zoom);
else if (option == "Select")
SetChartControlState(sender, MSChartExtensionToolState.Select);
else if (option == "Pan")
SetChartControlState(sender, MSChartExtensionToolState.Pan);
else if (option == "Zoom Out")
{
Chart ptrChart = sender;
WindowMessagesNativeMethods.SuspendDrawing(ptrChart);
ptrChart.ChartAreas[0].AxisX.ScaleView.ZoomReset();
ptrChart.ChartAreas[0].AxisY.ScaleView.ZoomReset();
ptrChart.ChartAreas[0].AxisY2.ScaleView.ZoomReset();
WindowMessagesNativeMethods.ResumeDrawing(ptrChart);
}
}
And then call this method like how I did in the question
// In the form
private void zoomButton_Click(object sender, EventArgs e)
{
this.chart.ChangeTool("Zoom"); // As an example
}
If anyone has a better way of doing this, feel free to let me know

How to hide the tool tips

I am having a treeview with some nodes. I am also having a panel. I have taken some usercontrol forms and i will load those usercontrols when corresponding node is selected from the child hood. Now what i need is have some validations like if i left the text box empty i will have some tooltips displayed to the user. Suppose if i click on first node i will have a user control loaded. With out giving any values if i hit ok i will have some tool tips as follows
Now if i select the second node from the tree still the tooltips getting displayed i would like to hide those
Any Help please
my code for rasing error tooltips is as shown below
public class TestClass
{
public void RequiredText(TextBox txtTemp, ToolTip newtoolTip)
{
if (txtTemp.Text != string.Empty)
{
txtTemp.BackColor = System.Drawing.Color.White;
newtoolTip.Hide(txtTemp);
}
else
{
txtTemp.BackColor = System.Drawing.Color.Tomato;
newtoolTip.Show("Required", txtTemp);
}
}
}
But this was done in the use control form.
I haven't yet mastered the art of reverse-engineering code from a screenshot. I'm guessing that you don't dispose the previous user control when you select a new one. Allowing the tool tip to stay visible. Use code like this:
private UserControl currentView;
public void SelectView(UserControl view) {
if (currentView == view) return;
if (currentView != null) currentView.Dispose();
if (view != null) this.Controls.Add(view);
currentView = view;
}
And call SelectView() from the TreeView's AfterSelect event handler.
Have you tried the Hide method?
http://dotnetperls.com/tooltip
Got the answer just written Usrcntrl_Leave event for every user control as
private void usrcntrlPPD_Leave(object sender, EventArgs e)
{
this.Dispose();
}
This solved my problem :)
private void timer1(object sender, EventArgs e)
{
count++;
if (count == 2)
{
toolTMensaje.SetToolTip(textBox1,"");
toolTMensaje.Hide(textBox1);
count = 0;
timer1.Stop();
}
}

Categories