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 WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("Sunday");
comboBox1.Items.Add("Monday");
comboBox1.Items.Add("Tuesday");
comboBox1.Items.Add("Wednesday");
comboBox1.Items.Add("Thursday");
comboBox1.Items.Add("Friday");
comboBox1.Items.Add("Saturday");
comboBox1.SelectedIndex = comboBox1.FindStringExact("Sunday");
}
private void button1_Click(object sender, EventArgs e)
{
string var;
var = comboBox1.Text;
MessageBox.Show(var);
}
}
}
This is the code but when i try to run it, its fine but my drop down list did not show any values. I still new in this C# language, forgive me if i did not realized any mistake in the code. Somebody, please help and explain to me what is the error and the solution for it. I really need it. Much appreciate.
First off, you don't need to load the items in the "form load" event, you can do so in Form1() itself after InitializeComponent(), and secondly you could try using List and ItemsSource:
public Form1()
{
InitializeComponent();
List<string> items = new List<string> { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
comboBox1.ItemsSource = items;
comboBox1.SelectedIndex = 0; //since you know "Sunday" is at index 0
}
EDIT: Since as John comments, the code you provided already works, my answer will remain a suggestion to shorten & simplify your code.
Related
I'm trying to use the FlowLayoutPanel with linkLabels in Visual Studio for a quick project. I've selected "TopDown" for direction and wrapping to false. When I launch the program; however, the direction always shows left to right. Is there a box or something that I haven't checked? Or is there any reason a linklabel would ignore the flow direction?
Here's my code and some screenshots of what I see.
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 myProject
{
public partial class Form1 : Form
{
FlowLayoutPanel panel = new FlowLayoutPanel();
public Form1()
{
InitializeComponent();
linkLabel1.LinkClicked += linkLabel1_LinkClicked;
linkLabel2.LinkClicked += linkLabel2_LinkClicked;
linkLabel3.LinkClicked += linkLabel3_LinkClicked;
Controls.Add(panel);
panel.Controls.Add(linkLabel1);
panel.Controls.Add(linkLabel2);
panel.Controls.Add(linkLabel3);
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
panel.Controls.SetChildIndex(linkLabel1, 0);
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
panel.Controls.SetChildIndex(linkLabel2, 0);
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
panel.Controls.SetChildIndex(linkLabel3, 0);
}
}
}
This is the control view before I've started the program.
This is what I see when I run the program - marked with the red arrow.
Because you are initializing your FlowLayoutPanel in the code-behind, you have to set the FlowDirection property of this new instance of FlowLayoutPanel in the same code-behind:
FlowLayoutPanel panel = new FlowLayoutPanel();
public Form1()
{
InitializeComponent();
panel.FlowDirection = FlowDirection.TopDown;
The FlowLayoutPanel that you declare in your code-behind is separate from the one you have in your layout, so the FlowDirection property is not set the same. I tested the code above and I believe it does what you were looking for.
I created a form with just 2 textboxes and a button. In the first one I type a temperature in Fahrenheit and when I press the button "Convert", the program calculates and puts the temperature in Celsius in the other TextBox. It's working fine.
Now I want the program to clear the second TextBox when I start typing in the first TextBox. Below I show just a part of the code, which didn't work. Can anybody help me?
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 Conv_Temp
{
public partial class Frm_Principal : Form
{
public Frm_Principal()
{
InitializeComponent();
}
public event EventHandler Leave;
private void Tb_Temp_Leave(object sender, EventArgs e)
{
MessageBox.Show("Leaving TB Tb_Temp");
Tb_Result.Text="";
}
}
}
I think you are almost there.
Try adding this under InitializeComponent();
this.Tb_Temp.TextChanged += new System.EventHandler(this.Tb_Temp_Leave);
Add new Event handler in your form designer code.
this.textBox1.TextChanged += new System.EventHandler(this.ModifyTextBox1);
and implement this event in above form.cs file(Form_Principal )
private void ModifyTextBox1(object sender, EventArgs e)
{
textBox2.Text = String.Empty;
}
Please follow good convention for writing codes this is just a demo.
Good morning guys I'm new to this place and have been having a lot of issues getting any of the findings to work.
I'm currently using C# inside visual studio community and was using a you tube video as reference but I'm stuck getting it to work efficiently (i think its in a loop) and to do other actions.
Im trying to get a list of urls from a text file in desktop or by user inputting it to a field:
EG: program opens HE puts IP in one text box and hits submit program runs and iots done.
or if it pulls a list of IP's from Desktop C: then it will add it to the website while using a loop?
eg:
my current code is similar to this as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HTTPcollect_Windows_Form_App
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<string> IPs = new List<string>();
WebClient web = new WebClient();
String html = web.DownloadString("http://www.ipvoid.com/scan/8.8.8.8/");
MatchCollection m1 = Regex.Matches(html, #"IP Address.*<strong>(.+?) <\/strong>", RegexOptions.Singleline);
foreach (Match m in m1)
{
string IP = m.Groups[1].Value;
IPs.Add(IP);
}
listBox1.DataSource = IPs;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
I am trying to figure out, for an assignment, a way to display a message that basically says "combo box is empty" after I remove all items from a combo box in C#. The assignment is very simple. I write a windows form in C# that is populated with fifteen states in a ComboBox and when I select an item from that list it is removed. I have it working but once all the items are gone It just sits there and I have to manually exit. Can someone point me in the right direction to get this to work, I am thinking maybe if statement is in order? Here is my code so far...
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 Chapter_15_Ex._15._3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Remove(comboBox1.SelectedItem);
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Thank You
if (comboBox1.Items.Count == 0)
{
MessageBox.Show("Your combo is empty");
}
Check the number of items left, after you remove one. You can Count on the Items collection to see how many items are left in the ComboBox.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Remove(comboBox1.SelectedItem);
if (comboBox1.Items.Count == 0)
MessageBox.Show("All Gone!");
}
You can check for the condition below
if (comboBox1.Items.Count == 0)
{
MessageBox.Show("Empty");
}
I am adding a TextBox to my form at runtime, and this is a brand new project, so this is the only code I have so far, so I am 100% positive that this is not my own doing:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TextBox box = new TextBox();
box.Location = new Point(2, 2);
this.Controls.Add(box);
}
}
}
Why won't the TextBox display? There's nothing at all. I set breakpoints all over the place, but none of them how anything that could help me. All seems normal, but isn't.
The code is very simple, the only reason I can think of is you have some other control added before (wide enough to cover the added TextBox), try this:
private void button1_Click(object sender, EventArgs e)
{
TextBox box = new TextBox();
box.Location = new Point(2, 2);
this.Controls.Add(box);
box.BringToFront();
}
Also check the event handler ControlAdded, I guess the form has some code for this event handler and discard the control added if it's type of TextBox, something like this:
private void form_ControlAdded(object sender, ControlEventArgs e) {
if(e.Control is TextBox) Controls.Remove(e.Control);
}
The code that adds the textbox to the form is in button1_Click event handler. If you move it to the constructor, it will work just fine.
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TextBox box = new TextBox();
box.Location = new Point(2, 2);
this.Controls.Add(box);
}
}
}
I had a similar problem.
Looking at the code I found out that the textboxes that DO show were of type System.Windows.Forms.TextBox while those that DID NOT show were of type VisualJS.Web.TextBox. Perhaps your problem is similar.