I am fairly new to C#.
I trying to create a Combo Box that displays numbers from 1-100 generated from the method below. I am using an example that found on the internet and modifying it to meet my needs.
I can't get my combo box to display anything. Please offer suggestions or a better way to execute my intended goal.
private void Range()
{
ArrayList arr = new ArrayList();
for (int i = 0; i <= 100; i++)
{
arr.Add(i);
}
}
public void FillInComboBox(ComboBox target, int start, int end)
{
for(int i = start; i <= end; i++)
{
target.Items.Add(i);
}
}
Usage
private void Form2_Load(object sender, EventArgs e)
{
FillInComboBox(comboBox1, 0, 100);
}
Related
I have a basic form with a button and a textbox to insert a number
I want to keep the number inserted in the first index of the array, then if I put another number and I click the button, I want that number to be stored in the second index of the array and so on
I have to make a loop to increment the index but if I use the loop, the first number I put in the box, it's going to be stored in all the indexes of the array
Here's my code (doesn't work)
public partial class EXERCISES_ARRAYS : Form
{
int[] numbers = new int[5];
private void btnAdd_Click(object sender, EventArgs e)
{
int i = 0;
int insertedNumber = Convert.ToInt32(txtInsertedValue.Text);
for (int j = 0; j < 5; j++)
{
numbers[i] = insertedNumber;
i++;
if (i == 5)
{
btnAdd.Enabled = false;
}
}
}
}
You want a List, rather than an array. That will take care of all of this for you:
public partial class EXERCISES_ARRAYS : Form
{
List<int> numbers = new List<int>();
private void btnAdd_Click(object sender, EventArgs e)
{
numbers.Add(int.Parse(txtInsertedValue.Text));
}
}
I don't think you need to add a for loop for this. Try this
int[] numbers = new int[5];
int i = 0;
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
int insertedNumber = Convert.ToInt32(txtInsertedValue.Text);
numbers[i] = insertedNumber;
i++;
if (i == 5)
{
btnAdd.Enabled = false;
}
}
I am trying to make my button take input from a textbox and add it to the index. The problem is that with everything I have tried, I cannot get it to give a unique value to each position in the index.
private void addBtn_Click(object sender, EventArgs e)
{
for (int i = 0; i < nums.Length; i++)
{
if (nums[0] == 0)
{
nums[0] = int.Parse(inputText.Text);
i++;
}
if (nums[1] == 0)
{
nums[1] = int.Parse(inputText.Text);
i++;
}
}
MessageBox.Show(nums[i].ToString());
}
Right now my code inserts the value to both index positions instead of assigning a value to position 0 and then allowing the user to insert a different value into position 1 and so on and so on.
It's not clear what your intent is, but it looks like you might be trying to add numbers to an array. This code will assign the parsed string to the first item in the array that isn't zero.
private void addBtn_Click(object sender, EventArgs e)
{
int i = 0;
for (; i < nums.Length; i++)
{
if (nums[i] == 0)
{
nums[i] = int.Parse(inputText.Text);
break;
}
}
MessageBox.Show(nums[i].ToString());
}
But this would be a better way, because the user might type "0":
private int _lastUsedIndex = -1;
private void addBtn_Click(object sender, EventArgs e)
{
var number = int.Parse(inputText.Text);
// Increment by one
++_lastUsedIndex;
nums[_lastUsedIndex] = number;
MessageBox.Show(number.ToString());
}
But still, arrays aren't a great idea: They can't grow as you add things. First they're bigger than you need, then suddenly they're too small and you crash. We have better options now. Unless your teacher insists that you must use an array, use List<int> instead. In this version, we'll also use a different way to parse the number, which won't crash if the user types "LOLWUT?!?!" instead of a number:
private List<int> nums = new List<int>();
private void addBtn_Click(object sender, EventArgs e)
{
int number;
if (int.TryParse(inputText.Text, out number))
{
nums.Add(number);
MessageBox.Show(number.ToString());
}
else
{
MessageBox.Show(inputText.Text + " isn't a number, smart guy.");
}
}
I want to add dynamic controls
Like the below image I want to do
I am struggling to do that
If I click Add More Experience button I want to display another rows
I tried with user control but it is not working properly.
Below code is working fine but if I add controls then close the browser page and then open the browser again added controls are coming.
I think the problem is static int i=0;
static int i = 0;
protected void addnewtext_Click(object sender, EventArgs e)
{
i++;
for (int j = 0; j <= i; j++)
{
AddVisaControl ac = (AddVisaControl)Page.LoadControl("AddVisaControl.ascx");
placeHolder.Controls.Add(ac);
placeHolder.Controls.Add(new LiteralControl("<BR>"));
}
}
Please provide your ideas? Thanks in advance
As per my comment, when using a static variable within an ASP.Net page, it will be shared amongst all users until the application pool or server is restarted.
Instead you should really be using a ViewState or similar to read/write the value.
private int controlCount
{
get
{
int val = 0;
try
{
val = (int)Page.ViewState["ControlCount"];
}
catch(Exception e)
{
// handle exception, if required.
}
return val;
}
set { Page.ViewState["ControlCount"] = value; }
}
protected void addnewtext_Click(object sender, EventArgs e)
{
int i = controlCount++;
for (int j = 0; j <= i; j++)
{
AddVisaControl ac = (AddVisaControl)Page.LoadControl("AddVisaControl.ascx");
placeHolder.Controls.Add(ac);
placeHolder.Controls.Add(new LiteralControl("<BR>"));
}
}
I'm trying to implement a custom text-editor in C# using the ScintillaNET component. I've got most of it right until now, but stuck at one point. I want to give the user the ability to block comment/uncomment the selected text. I tried a lot, but cannot find any examples online. The only thing I seem to get from the control's Selection object are the Start and End positions, but that isn't much help
private void commentBlockToolStripMenuItem_Click(object sender, EventArgs e)
{
if (txtSQL.Selection.Text.Length > 0)
{
String start = txtSQL.Selection.Start.ToString();
String end = txtSQL.Selection.End.ToString();
MessageBox.Show(start + "::" + end);
}
}
Were any of you able to successfully implement this using the ScintillaNET control?
EDIT:
After some improvization, I'm able to do it somehow, but after block is commented, last line moves out of selection!
private void commentBlockToolStripMenuItem_Click(object sender, EventArgs e)
{
if (txtSQL.Selection.Text.Length > 0)
{
Range range = txtSQL.Selection.Range;
int f = range.StartingLine.Number;
int t = range.EndingLine.Number;
int endpos = txtSQL.Selection.End;
for (int i = f; i <= t; i++)
{
//txtSQL.GoTo.Line(i);
string tstr = txtSQL.Lines[i].Text.Replace(Environment.NewLine, "");
txtSQL.Lines[i].Text = "--" + tstr;
}
}
}
After a bit of experimentation, I found a way to accomplish this. Though I doubt if it is the most elegant of solutions!
private void commentBlockToolStripMenuItem_Click(object sender, EventArgs e)
{
if (txtSQL.Selection.Text.Length > 0)
{
Range range = txtSQL.Selection.Range;
int f = range.StartingLine.Number;
int t = range.EndingLine.Number;
for (int i = f; i <= t; i++)
{
txtSQL.InsertText(txtSQL.Lines[i].StartPosition,"--");
}
txtSQL.Selection.Start = txtSQL.Lines[f].StartPosition;
txtSQL.Selection.End = txtSQL.Lines[t].EndPosition;
}
}
Actually I found a very simple solution to this. To block comment do
scintilla1.Lexing.LineComment();
And to block uncomment do
scintilla1.Lexing.LineUncomment();
I am using three list boxes. I have to invert the selected items in all the list boxes using an invert button.
How can code it using only a single loop? There can be more than 3 list boxes as well.
Hi you could use this function to invert the selection for a given listbox.
/* Windows ListBox
public void InvertSelection(ListBox objLstbox)
{
if(objLstbox == null) return;
for (int i = 0; i < objLstbox.Items.Count; i++)
objLstbox.SetSelected(i, !objLstbox.GetSelected(i));
}
*/
//WebApp listbox
public void InvertSelection(ListBox objLstbox)
{
if (objLstbox == null) return;
for (int i = 0; i < objLstbox.Items.Count; i++)
objLstbox.Items[i].Selected = !objLstbox.Items[i].Selected;
}
private void btnInvert_Click(object sender, EventArgs e)
{
InvertSelection(listBox1);
InvertSelection(listBox2);
InvertSelection(listBox3);
}
public void InvertSelection(ListBox objLstbox)
{
if (objLstbox == null) return;
for (int i = 0; i < objLstbox.Items.Count; i++)
objLstbox.Items[i].Selected = !objLstbox.Items[i].Selected;
}
protected void Button1_Click(object sender, EventArgs e)
{
InvertSelection(ListBox1);
}
I banged my head on this with the rest of you, and finally developed my own function for Inverting
Here's the VB.Net Answer:
Private Function InvertListBoxSelections(ByRef tempListBox As ListBox) As Integer
Dim selectedind(tempListBox.SelectedItems.Count) As Integer
Try
For selind = 0 To tempListBox.SelectedItems.Count - 1
selectedind.SetValue(tempListBox.Items.IndexOf(tempListBox.SelectedItems(selind)), selind)
Next
tempListBox.ClearSelected()
For listitemIndex = 0 To tempListBox.Items.Count
If Array.IndexOf(selectedind, listitemIndex) < 0 Then
tempListBox.SetSelected(listitemIndex, True)
End If
Next
Return 1
Catch ex As Exception
Return 0
End Try
End Function
for (int i = 0; i < listbox.Items.Count; i++)
{
if (listbox.SelectedItems.Contains(listbox.Items[i]))
listbox.SetSelected(i, false);
else
listbox.SetSelected(i, true);
}
Since I got here and got confused by selection i'll leave this here.
This code inverts all checked items by using .SetItemChecked and .GetItemChecked:
private void ButtonInvertChecked_Click(object sender, EventArgs e)
{
for (int i = 0; i < checkedListBox.Items.Count; i++)
checkedListBox.SetItemChecked (i, !checkedListBox.GetItemChecked(i));
}