PictureBoxSizeMode is error? - c#

Getting error when its casting in Listview:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
int a = 1;
string theimage = textBox1.Text + #"\allimages\";
foreach (ListViewItem item in listView1.SelectedItems)
{
// 39 zero's + "1"
string initValue = new String('0', 3) + "0";
// convert to int and add 1
int newValue = Int32.Parse(initValue) + a;
// convert back to string with leading zero's
string newValueString = newValue.ToString().PadLeft(4, '0');
string imageslist = "product" + newValueString + "img";
string[] images = Directory.GetFiles(theimage, imageslist + "*.jpg");
// Cast the Picturebox
PictureBox myPicBox = new PictureBox();
myPicBox.Location = new Point(7, 240);
myPicBox.Size = new System.Drawing.Size(140, 140);
myPicBox.SizeMode = PictureBoxSizeMode.AutoSize;
myPicBox.Margin = new Padding(3,3,3,3);
myPicBox.Visible = true;
myPicBox.Image = new Bitmap(images[1]);
Controls.Add(myPicBox);
System.Diagnostics.Debugger.Break();
//List<PictureBox> pictureBoxList = new List<PictureBox>();
}
}
Its my error:
Error 1 'test.Form1.PictureBoxSizeMode()' is a 'method', which is not valid in the given context C:\Users\radiaku\Documents\Visual Studio 2008\Projects\test\test\Form1.cs 428 37 test
the code above working fine when I using button_click handler..

It sounds like your form has a method called PictureBoxSizeMode. You could either change the method name, or change the property setter to:
myPicBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
Changing the method name would be cleaner though.

That error can only occur if you have a method named PictureBoxSizeMode in your code. You should rename that method to something else. A scenario like:
private void UserInput_KeyPress(object sender, KeyPressEventArgs e)
{
PictureBox myPicBox = new PictureBox();
myPicBox.Location = new Point(7, 240);
myPicBox.Size = new System.Drawing.Size(140, 140);
myPicBox.SizeMode = PictureBoxSizeMode.AutoSize;
myPicBox.Margin = new Padding(3, 3, 3, 3);
myPicBox.Visible = true;
}
private void PictureBoxSizeMode()
{
}
Or qualify it with the Namespace like.
myPicBox.SizeMode = myPicBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;

Related

Check which Radio Button is checked in C#, with dynamically created radiobuttons

I can't find any solution or hint on this problem. Problem described after this code.
I must create one picturebox and radiobutton for every folder found on a specific path:
{
InitializeComponent();
string pathtocircuits = "../../tracks";
string[] allfiles = Directory.GetDirectories(pathtocircuits, "*.*", SearchOption.TopDirectoryOnly);
int imgx = 387;
int imgy = 153;
int radx = 428;
int rady = 259;
String track = "";
String pici = "";
String pic = "pictureBox";
String rad = "radiobutton";
String radr = "";
String picr = "";
foreach (String file in allfiles)
{
track = Path.GetFileName(file);
pici = "../../tracks/" + track + "/p_" + track + ".png";
picr = pic + element.ToString();
radr = rad + element.ToString();
PictureBox pb = new PictureBox();
pb.Location = new System.Drawing.Point(imgx, imgy); ;
pb.Image = Image.FromFile(pici);
pb.Width = 100;
pb.Height = 100;
pb.SizeMode = PictureBoxSizeMode.StretchImage;
pb.Name = picr;
Controls.Add(pb);
RadioButton rdo = new RadioButton();
rdo.Name = radr;
rdo.Text = "";
rdo.Tag = track;
rdo.Location = new Point(radx, rady);
this.Controls.Add(rdo);
element += 1;
imgx += 110;
radx += 110;
}
}
With this part I get to create the elements I need (it works).
My problem is when I press a button to reach Form2. How can I check which radiobutton is selected and store its Tag value in a String?
for(int i = 0; i<element; i++)
{
if( ??? .Checked == true )
{
globalstring = ??? .Tag;
}
}
If I try to use the name of a created radiobutton instead of a ??? it gives me an error like 'element ??? does not have a Checked or Tag attribute'
Add method below
Add to For loop : rdo.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton button = sender as RadioButton;
string name = button.Text;
}
RadioButtons as any other control are stored in the Controls collection of their container. If you add them directly to the form you can retrieve them using this code.
protected void Button1_Click(object sender, EventArgs e)
{
var radio = this.Controls.OfType<RadioButton>().FirstOrDefault(x => x.Checked);
if(radio != null)
{
string tag = radio.Tag.ToString();
.....
// Form2 = new Form2(tag);
}
}
The radiobuttons are added to a same group by default, so you can get the checked radiobutton as followed.
List<RadioButton> radioButtons = this.Controls.OfType<RadioButton>().ToList();
RadioButton rb = radioButtons
.Where(r => r.Checked)
.Single();
string tag = rb.Tag.ToString();

Can't find a specific Control using Controls.Find()

I'm using Controls.Find() method to find a ListBox that i created dynamically. Name of those ListBoxes come from Directories that i created and named uniquely. Everytime i use this method it says the reference is null. How do i get the actual ListBox that i want?
Here is code:
public DosyaTakipAraci()
{
InitializeComponent();
string anaDizin = Properties.Settings.Default.anaDizin;
if (anaDizin == "" || anaDizin == null)
{
dizinSecici.Description = "Lütfen dosyalarınızın kaydedileceği bir dizin seçiniz.";
dizinSecici.ShowDialog();
Properties.Settings.Default.anaDizin = dizinSecici.SelectedPath + #"\";
Properties.Settings.Default.Save();
}
string[] dizinler = Directory.GetDirectories(anaDizin);
if (dizinler.Length > 0)
DizinleriYukle(dizinler);
}
This method loads all Directories
private void DizinleriYukle(string[] dizinler)
{
for (int i = 0; i < dizinler.GetLength(0); i++)
{
DirectoryInfo dizin = new DirectoryInfo(dizinler[i]);
DizinYukleyici(dizin.Name);
}
}
This method is belongs to Directory icon that is created for each Directory
private void DizinSimge_DragDrop(object sender, DragEventArgs e)
{
string[] dosyalar = (string[])e.Data.GetData(DataFormats.FileDrop);
PictureBox simge = (PictureBox)sender;
string dizinAdi = simge.Name;
string anaDizin = Properties.Settings.Default.anaDizin;
foreach (string dosya in dosyalar)
{
string dosyaAdi = Path.GetFileName(dosya);
if (File.Exists($#"{anaDizin}{dizinAdi}\{dosyaAdi}"))
return;
File.Copy(dosya, $#"{anaDizin}{dizinAdi}\{dosyaAdi}", true);
DosyaYukleyici(Panel.Controls.Find(dizinAdi, true).FirstOrDefault() as ListBox);
}
}
This method loads the Directory and creates Controls related to the Directory
private void DizinYukleyici(string dizinAdi)
{
#region Kontrol tanımları
PictureBox dizinSimge = new PictureBox
{
Name = dizinAdi,
Image = Properties.Resources.folder,
Size = new Size(32, 32),
Location = new Point(this.X, this.Y),
AllowDrop = true
};
TextBox dizinBaslik = new TextBox
{
Name = dizinAdi,
Text = dizinAdi,
Location = new Point(this.X + dizinSimge.Width + 5, this.Y + 8),
Font = new Font("Segou UI", 8.25f),
BackColor = this.BackColor,
BorderStyle = BorderStyle.None,
ReadOnly = true
};
ListBox Dosyalar = new ListBox
{
Name = dizinAdi,
Text = null,
Location = new Point(this.X, this.Y + dizinSimge.Height + 5),
BorderStyle = BorderStyle.None,
BackColor = this.BackColor,
Width = dizinSimge.Width + dizinBaslik.Width + 5
};
#endregion
#region Olaylar
dizinSimge.DragEnter += new DragEventHandler(DizinSimge_DragEnter);
dizinSimge.DragDrop += new DragEventHandler(DizinSimge_DragDrop);
dizinBaslik.DoubleClick += new EventHandler(Baslik_DoubleClick);
dizinBaslik.KeyPress += new KeyPressEventHandler(Baslik_Press);
Dosyalar.DragEnter += new DragEventHandler(DizinSimge_DragEnter);
Dosyalar.DragDrop += new DragEventHandler(Dosyalar_DragDrop);
#endregion
Panel.Controls.Add(dizinSimge);
Panel.Controls.Add(dizinBaslik);
Panel.Controls.Add(Dosyalar);
DosyaYukleyici(Dosyalar);
this.X += (short)(dizinSimge.Width + dizinBaslik.Width + 5);
}
This method Loads files of a Directory and adds them to related ListBox
private void DosyaYukleyici(ListBox Liste)
{
string dizinAdi = Liste.Name;
Liste.Items.Clear();
string anaDizin = Properties.Settings.Default.anaDizin;
string dizin = Path.Combine(anaDizin, dizinAdi);
string[] dosyalar = Directory.GetFiles(dizin);
foreach (string dosya in dosyalar)
{
Liste.Items.Add(Path.GetFileName(dosya));
}
}
You give the PictureBox, the TextBox and the ListBox the same name dizinAdi. Now when you search a ListBox, you may find a PictureBox or a TextBox first. Then you try to cast it with as ListBox which yields null if it is not the ListBox.
Filter the by type:
Panel.Controls.Find(dizinAdi, true).OfType<ListBox>().FirstOrDefault()
alternatively, you could give different names to different controls like
"lst" + dizinAdi
"txt" + dizinAdi
"pic" + dizinAdi
or
dizinAdi + "ListBox"
dizinAdi + "TextBox"
dizinAdi + "PictureBox"

Message Box unable to call local variable

I'm going to preface this by saying I'm sure the question has already been asked but I've tried all the suggestions and nothing seems to work for me. It just keeps returning a blank message box. If the answer were just a nice snippet of code I can add to my answer that would be fantastic.
private string Text1;
public void Button_Click(object sender, RoutedEventArgs e)
{
TextBox txtbx = new TextBox();
txtbx.Height = 50;
txtbx.Width = 200;
txtbx.Margin = new Thickness(771, 282, 0, 0);
txtbx.Background = new SolidColorBrush(Colors.White);
txtbx.Foreground = new SolidColorBrush(Colors.Black);
Text1 = txtbx.Text;
if (EditChecked == true)
{
LayoutRoot.Children.Add(txtbx);
Button Save = new Button();
Save.Height = 25;
Save.Width = 50;
Save.Content = "Save";
Save.Margin = new Thickness(771, 382, 0, 0);
LayoutRoot.Children.Add(Save);
txtbx.Text = Text1;
Save.Click += delegate
{
txtbx.Visibility = Visibility.Collapsed;
Save.Visibility = Visibility.Collapsed;
};
}
else if (ViewChecked == true)
{
MessageBox.Show(Text1);
}
}
Edit: Thanks to mjwills I moved the "Text1 = txtbx.Text;" line so this is what ended up working fyi:
private string Text1;
public void Button_Click(object sender, RoutedEventArgs e)
{
TextBox txtbx = new TextBox();
txtbx.Height = 50;
txtbx.Width = 200;
txtbx.Margin = new Thickness(771, 282, 0, 0);
txtbx.Background = new SolidColorBrush(Colors.White);
txtbx.Foreground = new SolidColorBrush(Colors.Black);
if (EditChecked == true)
{
LayoutRoot.Children.Add(txtbx);
Button Save = new Button();
Save.Height = 25;
Save.Width = 50;
Save.Content = "Save";
Save.Margin = new Thickness(771, 382, 0, 0);
LayoutRoot.Children.Add(Save);
txtbx.Text = Text1;
Save.Click += delegate
{
txtbx.Visibility = Visibility.Collapsed;
Save.Visibility = Visibility.Collapsed;
Text1 = txtbx.Text;
};
}
You declare:
private string Text1;
However you only assign it on the chance:
EditChecked = True
And you only display the message if
EditChecked = False
So, you need to assign Text1 on if the EditChecked = False. Perhaps on the declaration:
private string Text1 = "My default message";

why do my elements = 0 after inputting a number into array?

I'm trying to learn arrays and I cannot understand why after inputting all my arrays when I get it to show in my text box it says that they are all 0 ?
Here is my code
public Form1()
{
InitializeComponent();
}
int[] a = new int[10]; //global int!!!
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void btn_Enter_Click(object sender, EventArgs e)
{
//all elements show as 0 but why ??? come back to this later tonight!
string input;
for (int place = 0; place < 10; place++)
{
input = null;
My_Dialogs.InputBox("User Input Request!", "Please enter a number to be stored in element " + place + " : ", ref input);
Int32.TryParse(input, out a[place]);
}
}
private void btn_display_Click(object sender, EventArgs e)
{
for (int place = 0; place < 10; place++)
textBox1.AppendText("Element" + place + " of the array contains " + a[place] + "\n");
}
private void btn_quit_Click(object sender, EventArgs e)
{
Close();
}
Here is the mydialogs code
class My_Dialogs
{
public static string InputBox(string promptText)
{
string default_value = "";
return InputBox("", promptText, ref default_value);
}
public static string InputBox(string title, string promptText)
{
string default_value = "";
return InputBox(title, promptText, ref default_value);
}
public static string InputBox(string title, string promptText, ref string value)
{
Form form = new Form();
Label label = new Label();
TextBox textBox = new TextBox();
Button buttonOk = new Button();
Button buttonCancel = new Button();
form.Text = title;
label.Text = promptText;
textBox.Text = value;
buttonOk.Text = "OK";
buttonCancel.Text = "Cancel";
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;
label.SetBounds(9, 20, 372, 13);
textBox.SetBounds(12, 36, 372, 20);
buttonOk.SetBounds(228, 72, 75, 23);
buttonCancel.SetBounds(309, 72, 75, 23);
label.AutoSize = true;
textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
form.ClientSize = new Size(396, 107);
form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
if (form.ShowDialog() == DialogResult.Cancel)
{
textBox.Text = "";
}
return textBox.Text;
}
}
}
////////////////////////////////////////////////////////////////////////////
EDIT
You need to add the following to your MyDialog class right before the return statement:
value = textBox.Text;
This works just fine for me: (assuming the input is in fact an int.)
Without knowing whats in My_Dialogs.InputBox Its hard to tell what the problem is. What is the value of input immediately following the dialog box?
class Program
{
private static int[] a = new int[10]; //global int!!!
static void Main(string[] args)
{
for (int place = 0; place < a.Length; place++)
{
Console.WriteLine("Please enter a number to be stored in element " + place + " : ");
var input = Console.ReadLine();
Int32.TryParse(input, out a[place]);
}
}
}
Are you sure you got this code for the inputbox from your instructor? It never assigns the inserted text to value thus your input variable will always be null. You should either insert
value = textBox.Text;
in your InputBox code
or use
input = My_Dialogs.InputBox("User Input Request!", "Please enter a number to be stored in element " + place + " : ");
Furthermore you should check the result of TryParse if the conversion succeeded.

how can i store value in button at runtime?

I have a problem i have some dynamic button.and i want to store integer value in that.
and get that value on that click event of that button how can i achieve it
thanks in advance
shashank
DataView dv = new DataView(dtCat, "PK_CATEGORY_ID IN(" + categoryIds.ToString() + "0)", "PK_CATEGORY_ID", DataViewRowState.CurrentRows);
foreach (DataRowView rr in dv)
{
//MessageBox.Show(rr[0].ToString() + "------------" + rr[1].ToString());
Button b2 = new Button();
//b2.Name = rr[0].ToString();
b2.Name = "";
b2.Height = 200;
b2.Width = 200;
b2.Margin = new Thickness(0, -100, 0, 0);
b2.HorizontalAlignment = HorizontalAlignment.Left;
b2.Content = rr[1].ToString();
b2.Background = System.Windows.Media.Brushes.Pink;
b2.Click += new RoutedEventHandler(b2_Click);
btncanvas.Children.Add(b2);
Canvas.SetLeft(b2, b2.Width * i);
i = i + 1;
MessageBox.Show(rr[0].ToString());
b2.Tag = rr[0].ToString();
}
void b2_Click(object sender, RoutedEventArgs e)
{
Button clicked = (Button)sender;
categoryname = clicked.Name;
}
The Tag property is probably what you want.
You are already using it in your example, but just have:
b2.Tag = integerValue;
Then in your click handler use Convert.ToInt32(object) method to get the integer value back:
int retrievedValue = Convert.ToInt32(clicked.Tag);

Categories